35 lines
1016 B
Rust
35 lines
1016 B
Rust
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) => serde_json::from_str::<Settings>(&data).expect("Settings file corrupted"),
|
|
Err(_) => {
|
|
let newSet = Settings { entries: vec![] };
|
|
let payload = serde_json::to_string_pretty(&newSet).unwrap();
|
|
let _ = std::fs::write(config, payload);
|
|
newSet
|
|
}
|
|
}
|
|
}
|
|
|
|
pub fn save(&self, config: &String) {
|
|
let payload = serde_json::to_string(self).unwrap();
|
|
let _ = std::fs::write(config, payload);
|
|
}
|
|
}
|