Files
Steckbrett/src/app/settings.rs
Phani Pavan K 7ba256c96d
All checks were successful
/ Quality Check (push) Successful in 3m40s
/ Build (push) Successful in 3m0s
fix clippy suggestions
2025-08-30 15:11:57 +05:30

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);
}
}