initial column storage

Signed-off-by: kernelkind <kernelkind@gmail.com>
This commit is contained in:
kernelkind
2024-10-29 17:23:40 -04:00
parent 503b7edeb5
commit 56dd88b3a2
11 changed files with 217 additions and 25 deletions

60
src/storage/columns.rs Normal file
View File

@@ -0,0 +1,60 @@
use tracing::{error, info};
use crate::column::SerializableColumns;
use super::{write_file, DataPaths, Directory};
static COLUMNS_FILE: &str = "columns.json";
pub fn save_columns(columns: SerializableColumns) {
let serialized_columns = match serde_json::to_string(&columns) {
Ok(s) => s,
Err(e) => {
error!("Could not serialize columns: {}", e);
return;
}
};
let data_path = match DataPaths::Setting.get_path() {
Ok(s) => s,
Err(e) => {
error!("Could not get data path: {}", e);
return;
}
};
if let Err(e) = write_file(&data_path, COLUMNS_FILE.to_string(), &serialized_columns) {
error!("Could not write columns to file {}: {}", COLUMNS_FILE, e);
} else {
info!("Successfully wrote columns to {}", COLUMNS_FILE);
}
}
pub fn load_columns() -> Option<SerializableColumns> {
let data_path = match DataPaths::Setting.get_path() {
Ok(s) => s,
Err(e) => {
error!("Could not get data path: {}", e);
return None;
}
};
let columns_string = match Directory::new(data_path).get_file(COLUMNS_FILE.to_owned()) {
Ok(s) => s,
Err(e) => {
error!("Could not read columns from file {}: {}", COLUMNS_FILE, e);
return None;
}
};
match serde_json::from_str::<SerializableColumns>(&columns_string) {
Ok(s) => {
info!("Successfully loaded columns from {}", COLUMNS_FILE);
Some(s)
}
Err(e) => {
error!("Could not deserialize columns: {}", e);
None
}
}
}

View File

@@ -1,7 +1,9 @@
mod columns;
#[cfg(any(target_os = "linux", target_os = "macos"))]
mod file_key_storage;
mod file_storage;
pub use columns::{load_columns, save_columns};
pub use file_key_storage::FileKeyStorage;
pub use file_storage::write_file;
pub use file_storage::DataPaths;