init commit, ui functional
This commit is contained in:
38
src/app/settings.rs
Normal file
38
src/app/settings.rs
Normal file
@@ -0,0 +1,38 @@
|
||||
use crate::app::entry::Entry;
|
||||
use serde::{Deserialize, Serialize};
|
||||
use std::fmt::Display;
|
||||
|
||||
#[derive(Debug, Serialize, Deserialize)]
|
||||
pub struct Settings {
|
||||
pub entries: Vec<Entry>,
|
||||
}
|
||||
|
||||
impl Display for Settings {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "Total Entries: {}", self.entries.len())
|
||||
}
|
||||
}
|
||||
|
||||
impl Settings {
|
||||
pub fn new(config: &String) -> Self {
|
||||
let data = std::fs::read_to_string(config);
|
||||
match data {
|
||||
Ok(data) => {
|
||||
let settings =
|
||||
serde_json::from_str::<Settings>(&data).expect("Settings file corrupted");
|
||||
settings
|
||||
}
|
||||
Err(_) => {
|
||||
let newSet = Settings { entries: vec![] };
|
||||
let payload = serde_json::to_string_pretty(&newSet).unwrap();
|
||||
std::fs::write(config, payload);
|
||||
newSet
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn save(&self, config: &String) {
|
||||
let payload = serde_json::to_string(self).unwrap();
|
||||
std::fs::write(config, payload);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user