Skip to content

Commit 2574ec8

Browse files
authored
refactor(store-example): replace load_from_store with TryFrom impl in example (#3277)
1 parent 2971289 commit 2574ec8

2 files changed

Lines changed: 17 additions & 25 deletions

File tree

plugins/store/examples/AppSettingsManager/src-tauri/src/app/settings.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -10,10 +10,8 @@ pub struct AppSettings {
1010
pub theme: String,
1111
}
1212

13-
impl AppSettings {
14-
pub fn load_from_store<R: tauri::Runtime>(
15-
store: &Store<R>,
16-
) -> Result<Self, Box<dyn std::error::Error>> {
13+
impl<R: tauri::Runtime> From<&Store<R>> for AppSettings {
14+
fn from(store: &Store<R>) -> Self {
1715
let launch_at_login = store
1816
.get("appSettings.launchAtLogin")
1917
.and_then(|v| v.as_bool())
@@ -24,9 +22,9 @@ impl AppSettings {
2422
.and_then(|v| v.as_str().map(String::from))
2523
.unwrap_or_else(|| "dark".to_owned());
2624

27-
Ok(AppSettings {
25+
AppSettings {
2826
launch_at_login,
2927
theme,
30-
})
28+
}
3129
}
3230
}

plugins/store/examples/AppSettingsManager/src-tauri/src/main.rs

Lines changed: 13 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -18,28 +18,22 @@ fn main() {
1818
.setup(|app| {
1919
// Init store and load it from disk
2020
let store = app.store("settings.json")?;
21+
2122
app.listen("store://change", |event| {
2223
dbg!(event);
2324
});
24-
let app_settings = AppSettings::load_from_store(&store);
25-
match app_settings {
26-
Ok(app_settings) => {
27-
let theme = app_settings.theme;
28-
let launch_at_login = app_settings.launch_at_login;
29-
30-
println!("theme {theme}");
31-
println!("launch_at_login {launch_at_login}");
32-
store.set(
33-
"appSettings",
34-
json!({ "theme": theme, "launchAtLogin": launch_at_login }),
35-
);
36-
}
37-
Err(err) => {
38-
eprintln!("Error loading settings: {err}");
39-
// Handle the error case if needed
40-
return Err(err); // Convert the error to a Box<dyn Error> and return Err(err) here
41-
}
42-
}
25+
26+
let app_settings = AppSettings::from(store.as_ref());
27+
let theme = app_settings.theme;
28+
let launch_at_login = app_settings.launch_at_login;
29+
30+
println!("theme {theme}");
31+
println!("launch_at_login {launch_at_login}");
32+
store.set(
33+
"appSettings",
34+
json!({ "theme": theme, "launchAtLogin": launch_at_login }),
35+
);
36+
4337
Ok(())
4438
})
4539
.run(tauri::generate_context!())

0 commit comments

Comments
 (0)