diff options
author | Jesse Luehrs <doy@tozt.net> | 2020-05-23 23:02:28 -0400 |
---|---|---|
committer | Jesse Luehrs <doy@tozt.net> | 2020-05-23 23:10:58 -0400 |
commit | de7954483eb0ce6daeba6eb06e8ed91a7b0e1dfb (patch) | |
tree | b2ce010c2ee1f62ee74ea038fe6f849dd0398b7f /src/config.rs | |
parent | ffdae54bae2cc9c2e0f704878ff59255dbd787c0 (diff) | |
download | rbw-de7954483eb0ce6daeba6eb06e8ed91a7b0e1dfb.tar.gz rbw-de7954483eb0ce6daeba6eb06e8ed91a7b0e1dfb.zip |
include filenames in the error messages about file operations
Diffstat (limited to 'src/config.rs')
-rw-r--r-- | src/config.rs | 47 |
1 files changed, 28 insertions, 19 deletions
diff --git a/src/config.rs b/src/config.rs index d5c1659..418da45 100644 --- a/src/config.rs +++ b/src/config.rs @@ -33,13 +33,16 @@ impl Config { } pub fn load() -> Result<Self> { - let mut fh = std::fs::File::open(crate::dirs::config_file()) - .context(crate::error::LoadConfig)?; + let file = crate::dirs::config_file(); + let mut fh = std::fs::File::open(&file).with_context(|| { + crate::error::LoadConfig { file: file.clone() } + })?; let mut json = String::new(); - fh.read_to_string(&mut json) - .context(crate::error::LoadConfig)?; + fh.read_to_string(&mut json).with_context(|| { + crate::error::LoadConfig { file: file.clone() } + })?; let mut slf: Self = serde_json::from_str(&json) - .context(crate::error::LoadConfigJson)?; + .context(crate::error::LoadConfigJson { file })?; if slf.lock_timeout == 0 { log::warn!("lock_timeout must be greater than 0"); slf.lock_timeout = default_lock_timeout(); @@ -48,15 +51,17 @@ impl Config { } pub async fn load_async() -> Result<Self> { - let mut fh = tokio::fs::File::open(crate::dirs::config_file()) - .await - .context(crate::error::LoadConfigAsync)?; + let file = crate::dirs::config_file(); + let mut fh = + tokio::fs::File::open(&file).await.with_context(|| { + crate::error::LoadConfigAsync { file: file.clone() } + })?; let mut json = String::new(); - fh.read_to_string(&mut json) - .await - .context(crate::error::LoadConfigAsync)?; + fh.read_to_string(&mut json).await.with_context(|| { + crate::error::LoadConfigAsync { file: file.clone() } + })?; let mut slf: Self = serde_json::from_str(&json) - .context(crate::error::LoadConfigJson)?; + .context(crate::error::LoadConfigJson { file })?; if slf.lock_timeout == 0 { log::warn!("lock_timeout must be greater than 0"); slf.lock_timeout = default_lock_timeout(); @@ -65,19 +70,23 @@ impl Config { } pub fn save(&self) -> Result<()> { - let filename = crate::dirs::config_file(); + let file = crate::dirs::config_file(); // unwrap is safe here because Self::filename is explicitly // constructed as a filename in a directory - std::fs::create_dir_all(filename.parent().unwrap()) - .context(crate::error::SaveConfig)?; - let mut fh = std::fs::File::create(filename) - .context(crate::error::SaveConfig)?; + std::fs::create_dir_all(file.parent().unwrap()).with_context( + || crate::error::SaveConfig { file: file.clone() }, + )?; + let mut fh = std::fs::File::create(&file).with_context(|| { + crate::error::SaveConfig { file: file.clone() } + })?; fh.write_all( serde_json::to_string(self) - .context(crate::error::SaveConfigJson)? + .with_context(|| crate::error::SaveConfigJson { + file: file.clone(), + })? .as_bytes(), ) - .context(crate::error::SaveConfig)?; + .context(crate::error::SaveConfig { file })?; Ok(()) } |