From c14c49946736bbb5e86d22bf75b2b3594354b46b Mon Sep 17 00:00:00 2001 From: pjht Date: Tue, 30 May 2023 15:10:12 -0500 Subject: [PATCH] Store state in one key instead of a key per state field --- src/main.rs | 7 ++++--- src/roll.rs | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 62 insertions(+), 3 deletions(-) create mode 100644 src/roll.rs diff --git a/src/main.rs b/src/main.rs index 061895f..bdec494 100644 --- a/src/main.rs +++ b/src/main.rs @@ -8,6 +8,7 @@ use eframe::egui; use new_roll::NewRollWindow; use roll::Roll; use roll_view::RollViewWindow; +use serde::{Deserialize, Serialize}; fn main() -> Result<(), eframe::Error> { env_logger::init(); // Log to stderr (if you run with `RUST_LOG=debug`). @@ -22,6 +23,7 @@ fn main() -> Result<(), eframe::Error> { ) } +#[derive(Serialize, Deserialize, Default)] pub struct AppState { pub rolls: Vec, } @@ -34,9 +36,8 @@ struct MyApp { impl MyApp { fn new(cc: &eframe::CreationContext<'_>) -> Self { - let rolls = eframe::get_value(cc.storage.unwrap(), "rolls").unwrap_or_default(); Self { - state: AppState { rolls }, + state: eframe::get_value(cc.storage.unwrap(), "state").unwrap_or_default(), roll_views: Vec::new(), new_roll_window: None, } @@ -45,7 +46,7 @@ impl MyApp { impl eframe::App for MyApp { fn save(&mut self, storage: &mut dyn eframe::Storage) { - eframe::set_value(storage, "rolls", &self.state.rolls); + eframe::set_value(storage, "state", &self.state); } fn update(&mut self, ctx: &egui::Context, _frame: &mut eframe::Frame) { diff --git a/src/roll.rs b/src/roll.rs new file mode 100644 index 0000000..89135e3 --- /dev/null +++ b/src/roll.rs @@ -0,0 +1,58 @@ +use chrono::NaiveDate; +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Exposure { + pub num: u8, + #[serde(default)] + pub printed: bool, + #[serde(default)] + pub damaged: bool, + #[serde(default)] + pub paper: Option, +} + +#[derive(Debug, Serialize, Deserialize, Clone)] +pub struct Roll { + pub id: String, + pub name: String, + #[serde(default)] + pub desc: String, + pub date: NaiveDate, + pub exposures: Vec, +} + +impl Roll { + pub fn new(id: String, name: String, desc: String, date: NaiveDate, num_exposures: u8) -> Self { + let mut exposures = Vec::new(); + for num in 1..=num_exposures { + exposures.push(Exposure { + num, + printed: false, + damaged: false, + paper: None, + }) + } + Self { + id, + name, + desc, + date, + exposures, + } + } + + pub fn short_str(&self) -> String { + format!( + "{}{} on {}, {} exp", + self.id, + if self.name.is_empty() { + String::new() + } else { + format!(" - {}", self.name) + }, + self.date.format("%m/%d/%Y"), + self.exposures.len() + ) + } +}