mirror of
https://github.com/arabianq/pipewire-soundpad.git
synced 2026-04-28 06:21:23 +00:00
refactor: replace unsafe unwrap with if let in config saving (#24)
Removed the unsafe `.unwrap()` call when attempting to get the parent directory of `config_path` in `DaemonConfig::save_to_file` and `GuiConfig::save_to_file`. Replaced it with an idiomatic `if let Some(config_dir)` check to improve code safety and maintainability. Co-authored-by: google-labs-jules[bot] <161369871+google-labs-jules[bot]@users.noreply.github.com>
This commit is contained in:
committed by
GitHub
parent
4b50645c93
commit
aa77a8d212
+4
-2
@@ -12,11 +12,12 @@ pub struct DaemonConfig {
|
|||||||
impl DaemonConfig {
|
impl DaemonConfig {
|
||||||
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
|
pub fn save_to_file(&self) -> Result<(), Box<dyn Error>> {
|
||||||
let config_path = get_config_path()?.join("daemon.json");
|
let config_path = get_config_path()?.join("daemon.json");
|
||||||
let config_dir = config_path.parent().unwrap();
|
|
||||||
|
|
||||||
|
if let Some(config_dir) = config_path.parent() {
|
||||||
if !config_path.exists() {
|
if !config_path.exists() {
|
||||||
fs::create_dir_all(config_dir)?;
|
fs::create_dir_all(config_dir)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
let config_json = serde_json::to_string_pretty(self)?;
|
let config_json = serde_json::to_string_pretty(self)?;
|
||||||
fs::write(config_path, config_json.as_bytes())?;
|
fs::write(config_path, config_json.as_bytes())?;
|
||||||
@@ -63,11 +64,12 @@ impl Default for GuiConfig {
|
|||||||
impl GuiConfig {
|
impl GuiConfig {
|
||||||
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
|
pub fn save_to_file(&mut self) -> Result<(), Box<dyn Error>> {
|
||||||
let config_path = get_config_path()?.join("gui.json");
|
let config_path = get_config_path()?.join("gui.json");
|
||||||
let config_dir = config_path.parent().unwrap();
|
|
||||||
|
|
||||||
|
if let Some(config_dir) = config_path.parent() {
|
||||||
if !config_path.exists() {
|
if !config_path.exists() {
|
||||||
fs::create_dir_all(config_dir)?;
|
fs::create_dir_all(config_dir)?;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// Do not save scale factor if user does not want to
|
// Do not save scale factor if user does not want to
|
||||||
if !self.save_scale_factor {
|
if !self.save_scale_factor {
|
||||||
|
|||||||
Reference in New Issue
Block a user