Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 10 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,13 @@ fn get_saved_locale() -> Option<String> {
fn get_saved_json(preferences: &serde_json::Value) -> serde_json::Value {
let save_path = fix_path(preferences["save_path"].as_str().unwrap());
if Path::new(&save_path).exists() {
read_json(save_path.as_str())
match read_json(save_path.as_str()) {
Ok(v) => v,
Err(e) => {
error!("Could not load saved settings from {save_path}: {e}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error!("Could not load saved settings from {save_path}: {e}");
error!("Could not load saved settings from {save_path}: {e:?}");

json!({"locale": ""})
},
}
} else {
json!({"locale": ""})
}
Expand Down Expand Up @@ -265,7 +271,9 @@ fn on_link1_clicked(param: &[glib::Value]) -> Option<glib::Value> {
fn on_delete_window(_param: &[glib::Value]) -> Option<glib::Value> {
let saved_json = &*G_SAVE_JSON.lock().unwrap();
let preferences = unsafe { G_HELLO_WINDOW.as_ref().unwrap().get_preferences("save_path") };
write_json(preferences.as_str().unwrap(), saved_json);
if let Err(e) = write_json(preferences.as_str().unwrap(), saved_json) {
error!("Could not save settings: {e}");
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
error!("Could not save settings: {e}");
error!("Could not save settings: {e:?}");

}

Some(false.to_value())
}
Expand Down
13 changes: 7 additions & 6 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,16 +26,17 @@ pub fn fix_path(path: &str) -> String {
}

#[inline]
pub fn read_json(path: &str) -> serde_json::Value {
pub fn read_json(path: &str) -> anyhow::Result<serde_json::Value> {
let buf = fix_path(path);
let data = fs::read_to_string(buf).expect("Unable to read file");
serde_json::from_str(&data).expect("Unable to parse")
let data = fs::read_to_string(buf)?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let data = fs::read_to_string(buf)?;
let data = fs::read_to_string(buf).context("Unable to read file")?;

Ok(serde_json::from_str(&data)?)
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Ok(serde_json::from_str(&data)?)
Ok(serde_json::from_str(&data).context("Unable to parse")?)

}

#[inline]
pub fn write_json(path: &str, content: &serde_json::Value) {
let output = File::create(fix_path(path)).expect("Unable to open file for writing");
serde_json::to_writer(output, content).expect("Unable to write json to file");
pub fn write_json(path: &str, content: &serde_json::Value) -> anyhow::Result<()> {
let output = File::create(fix_path(path))?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
let output = File::create(fix_path(path))?;
let output = File::create(fix_path(path)).context("Unable to open file for writing")?;

serde_json::to_writer(output, content)?;
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
serde_json::to_writer(output, content)?;
serde_json::to_writer(output, content).context("Unable to write json to file")?;

Ok(())
}

#[inline]
Expand Down