fix: add safeguard to avoid saving when init failed

This commit is contained in:
electria 2026-06-25 19:29:45 -07:00
commit 321160461b
Signed by: electria
SSH key fingerprint: SHA256:8LlB3ucPbBHqozqkhsNbaV5oG3SlzzqUj8FZDL6IPQs

View file

@ -54,6 +54,9 @@ enum Message {
struct State {
table: data::Table,
content: widget::text_editor::Content,
/// has the application initialized successfully?
ok: bool,
}
impl State {
fn new() -> Self {
@ -72,6 +75,8 @@ impl State {
}
}
state.ok = true;
state
}
fn view(&self) -> Element<'_, Message, Theme, Renderer> {
@ -236,6 +241,11 @@ impl State {
}
impl Drop for State {
fn drop(&mut self) {
if !self.ok {
eprintln!("app failed to start, not saving data");
return;
}
ron::Options::default()
.to_io_writer(
fs::File::create(data!()).unwrap(),
@ -245,6 +255,8 @@ impl Drop for State {
},
)
.unwrap();
eprintln!("successfully saved data");
}
}