Refactor code to allow edition of archived months

This commit is contained in:
Quentin Boyer 2023-11-12 21:30:12 +01:00
parent e88d1be093
commit c5ff34cd4f

View file

@ -546,7 +546,7 @@ struct Glaurung {
}
#[derive(Serialize, Deserialize, Default)]
struct SaveFile {
struct Report {
#[serde(default)]
recurring: BTreeMap<String, f64>,
#[serde(default)]
@ -559,6 +559,12 @@ struct SaveFile {
earnings_2: f64,
}
#[derive(Serialize, Deserialize, Default)]
struct SaveFile {
#[serde(flatten)]
current: Report,
}
#[derive(Default)]
struct AppConfig {
save: SaveFile,
@ -566,6 +572,29 @@ struct AppConfig {
config: Config,
}
impl Glaurung {
fn report(&self) -> Report {
Report {
savings: self.savings.clone(),
recurring: self.recurring.clone(),
variable: self
.variable
.clone()
.into_iter()
.map(|(k, (e, _))| (k, e))
.collect(),
earnings_1: self.earnings_1,
earnings_2: self.earnings_2,
}
}
fn save(&self) -> SaveFile {
SaveFile {
current: self.report(),
}
}
}
impl Application for Glaurung {
type Message = Message;
type Theme = Theme;
@ -576,10 +605,11 @@ impl Application for Glaurung {
(
Self {
config: config.config,
recurring: config.save.recurring,
savings: config.save.savings,
recurring: config.save.current.recurring,
savings: config.save.current.savings,
variable: config
.save
.current
.variable
.into_iter()
.map(|(k, e)| {
@ -587,8 +617,8 @@ impl Application for Glaurung {
(k, (e, f.ok()))
})
.collect(),
earnings_1: config.save.earnings_1,
earnings_2: config.save.earnings_2,
earnings_1: config.save.current.earnings_1,
earnings_2: config.save.current.earnings_2,
save_file: config.save_file,
},
Command::batch(vec![
@ -625,16 +655,7 @@ impl Application for Glaurung {
.expect("Can't open temp save file");
serde_json::to_writer(
save_file,
&SaveFile {
savings: std::mem::take(&mut self.savings),
recurring: std::mem::take(&mut self.recurring),
variable: std::mem::take(&mut self.variable)
.into_iter()
.map(|(k, (e, _))| (k, e))
.collect(),
earnings_1: self.earnings_1,
earnings_2: self.earnings_2,
},
&self.save()
)
.expect("could not write save file");