columns.rs (1450B)
1 use tracing::{error, info}; 2 3 use crate::column::SerializableColumns; 4 5 use super::{write_file, DataPath, DataPathType, Directory}; 6 7 static COLUMNS_FILE: &str = "columns.json"; 8 9 pub fn save_columns(path: &DataPath, columns: SerializableColumns) { 10 let serialized_columns = match serde_json::to_string(&columns) { 11 Ok(s) => s, 12 Err(e) => { 13 error!("Could not serialize columns: {}", e); 14 return; 15 } 16 }; 17 18 let data_path = path.path(DataPathType::Setting); 19 20 if let Err(e) = write_file(&data_path, COLUMNS_FILE.to_string(), &serialized_columns) { 21 error!("Could not write columns to file {}: {}", COLUMNS_FILE, e); 22 } else { 23 info!("Successfully wrote columns to {}", COLUMNS_FILE); 24 } 25 } 26 27 pub fn load_columns(path: &DataPath) -> Option<SerializableColumns> { 28 let data_path = path.path(DataPathType::Setting); 29 30 let columns_string = match Directory::new(data_path).get_file(COLUMNS_FILE.to_owned()) { 31 Ok(s) => s, 32 Err(e) => { 33 error!("Could not read columns from file {}: {}", COLUMNS_FILE, e); 34 return None; 35 } 36 }; 37 38 match serde_json::from_str::<SerializableColumns>(&columns_string) { 39 Ok(s) => { 40 info!("Successfully loaded columns from {}", COLUMNS_FILE); 41 Some(s) 42 } 43 Err(e) => { 44 error!("Could not deserialize columns: {}", e); 45 None 46 } 47 } 48 }