Refactor code to allow edition of archived months
This commit is contained in:
parent
e88d1be093
commit
c5ff34cd4f
1 changed files with 36 additions and 15 deletions
51
src/main.rs
51
src/main.rs
|
|
@ -546,7 +546,7 @@ struct Glaurung {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Serialize, Deserialize, Default)]
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
struct SaveFile {
|
struct Report {
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
recurring: BTreeMap<String, f64>,
|
recurring: BTreeMap<String, f64>,
|
||||||
#[serde(default)]
|
#[serde(default)]
|
||||||
|
|
@ -559,6 +559,12 @@ struct SaveFile {
|
||||||
earnings_2: f64,
|
earnings_2: f64,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#[derive(Serialize, Deserialize, Default)]
|
||||||
|
struct SaveFile {
|
||||||
|
#[serde(flatten)]
|
||||||
|
current: Report,
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Default)]
|
#[derive(Default)]
|
||||||
struct AppConfig {
|
struct AppConfig {
|
||||||
save: SaveFile,
|
save: SaveFile,
|
||||||
|
|
@ -566,6 +572,29 @@ struct AppConfig {
|
||||||
config: Config,
|
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 {
|
impl Application for Glaurung {
|
||||||
type Message = Message;
|
type Message = Message;
|
||||||
type Theme = Theme;
|
type Theme = Theme;
|
||||||
|
|
@ -576,10 +605,11 @@ impl Application for Glaurung {
|
||||||
(
|
(
|
||||||
Self {
|
Self {
|
||||||
config: config.config,
|
config: config.config,
|
||||||
recurring: config.save.recurring,
|
recurring: config.save.current.recurring,
|
||||||
savings: config.save.savings,
|
savings: config.save.current.savings,
|
||||||
variable: config
|
variable: config
|
||||||
.save
|
.save
|
||||||
|
.current
|
||||||
.variable
|
.variable
|
||||||
.into_iter()
|
.into_iter()
|
||||||
.map(|(k, e)| {
|
.map(|(k, e)| {
|
||||||
|
|
@ -587,8 +617,8 @@ impl Application for Glaurung {
|
||||||
(k, (e, f.ok()))
|
(k, (e, f.ok()))
|
||||||
})
|
})
|
||||||
.collect(),
|
.collect(),
|
||||||
earnings_1: config.save.earnings_1,
|
earnings_1: config.save.current.earnings_1,
|
||||||
earnings_2: config.save.earnings_2,
|
earnings_2: config.save.current.earnings_2,
|
||||||
save_file: config.save_file,
|
save_file: config.save_file,
|
||||||
},
|
},
|
||||||
Command::batch(vec![
|
Command::batch(vec![
|
||||||
|
|
@ -625,16 +655,7 @@ impl Application for Glaurung {
|
||||||
.expect("Can't open temp save file");
|
.expect("Can't open temp save file");
|
||||||
serde_json::to_writer(
|
serde_json::to_writer(
|
||||||
save_file,
|
save_file,
|
||||||
&SaveFile {
|
&self.save()
|
||||||
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,
|
|
||||||
},
|
|
||||||
)
|
)
|
||||||
.expect("could not write save file");
|
.expect("could not write save file");
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue