fix: add safeguard to avoid saving when init failed
This commit is contained in:
parent
8036728152
commit
321160461b
1 changed files with 12 additions and 0 deletions
12
src/main.rs
12
src/main.rs
|
|
@ -54,6 +54,9 @@ enum Message {
|
||||||
struct State {
|
struct State {
|
||||||
table: data::Table,
|
table: data::Table,
|
||||||
content: widget::text_editor::Content,
|
content: widget::text_editor::Content,
|
||||||
|
|
||||||
|
/// has the application initialized successfully?
|
||||||
|
ok: bool,
|
||||||
}
|
}
|
||||||
impl State {
|
impl State {
|
||||||
fn new() -> Self {
|
fn new() -> Self {
|
||||||
|
|
@ -72,6 +75,8 @@ impl State {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
state.ok = true;
|
||||||
|
|
||||||
state
|
state
|
||||||
}
|
}
|
||||||
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
|
||||||
|
|
@ -236,6 +241,11 @@ impl State {
|
||||||
}
|
}
|
||||||
impl Drop for State {
|
impl Drop for State {
|
||||||
fn drop(&mut self) {
|
fn drop(&mut self) {
|
||||||
|
if !self.ok {
|
||||||
|
eprintln!("app failed to start, not saving data");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
ron::Options::default()
|
ron::Options::default()
|
||||||
.to_io_writer(
|
.to_io_writer(
|
||||||
fs::File::create(data!()).unwrap(),
|
fs::File::create(data!()).unwrap(),
|
||||||
|
|
@ -245,6 +255,8 @@ impl Drop for State {
|
||||||
},
|
},
|
||||||
)
|
)
|
||||||
.unwrap();
|
.unwrap();
|
||||||
|
|
||||||
|
eprintln!("successfully saved data");
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue