From 9e77724efff281f0fe6d05440ad65c5ab561f380 Mon Sep 17 00:00:00 2001 From: Jesse Luehrs Date: Sat, 6 Mar 2021 13:18:29 -0500 Subject: switch to thiserror --- src/config.rs | 52 +++++++++++++++++++++++++++++++++++----------------- 1 file changed, 35 insertions(+), 17 deletions(-) (limited to 'src/config.rs') diff --git a/src/config.rs b/src/config.rs index 9bb4696..dbdf759 100644 --- a/src/config.rs +++ b/src/config.rs @@ -41,15 +41,20 @@ impl Config { pub fn load() -> Result { let file = crate::dirs::config_file(); - let mut fh = std::fs::File::open(&file).with_context(|| { - crate::error::LoadConfig { file: file.clone() } + let mut fh = std::fs::File::open(&file).map_err(|source| { + Error::LoadConfig { + source, + file: file.clone(), + } })?; let mut json = String::new(); - fh.read_to_string(&mut json).with_context(|| { - crate::error::LoadConfig { file: file.clone() } - })?; + fh.read_to_string(&mut json) + .map_err(|source| Error::LoadConfig { + source, + file: file.clone(), + })?; let mut slf: Self = serde_json::from_str(&json) - .context(crate::error::LoadConfigJson { file })?; + .map_err(|source| Error::LoadConfigJson { source, file })?; if slf.lock_timeout == 0 { log::warn!("lock_timeout must be greater than 0"); slf.lock_timeout = default_lock_timeout(); @@ -60,15 +65,21 @@ impl Config { pub async fn load_async() -> Result { let file = crate::dirs::config_file(); let mut fh = - tokio::fs::File::open(&file).await.with_context(|| { - crate::error::LoadConfigAsync { file: file.clone() } + tokio::fs::File::open(&file).await.map_err(|source| { + Error::LoadConfigAsync { + source, + file: file.clone(), + } })?; let mut json = String::new(); - fh.read_to_string(&mut json).await.with_context(|| { - crate::error::LoadConfigAsync { file: file.clone() } + fh.read_to_string(&mut json).await.map_err(|source| { + Error::LoadConfigAsync { + source, + file: file.clone(), + } })?; let mut slf: Self = serde_json::from_str(&json) - .context(crate::error::LoadConfigJson { file })?; + .map_err(|source| Error::LoadConfigJson { source, file })?; if slf.lock_timeout == 0 { log::warn!("lock_timeout must be greater than 0"); slf.lock_timeout = default_lock_timeout(); @@ -80,20 +91,27 @@ impl Config { 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(file.parent().unwrap()).with_context( - || crate::error::SaveConfig { file: file.clone() }, + std::fs::create_dir_all(file.parent().unwrap()).map_err( + |source| Error::SaveConfig { + source, + file: file.clone(), + }, )?; - let mut fh = std::fs::File::create(&file).with_context(|| { - crate::error::SaveConfig { file: file.clone() } + let mut fh = std::fs::File::create(&file).map_err(|source| { + Error::SaveConfig { + source, + file: file.clone(), + } })?; fh.write_all( serde_json::to_string(self) - .with_context(|| crate::error::SaveConfigJson { + .map_err(|source| Error::SaveConfigJson { + source, file: file.clone(), })? .as_bytes(), ) - .context(crate::error::SaveConfig { file })?; + .map_err(|source| Error::SaveConfig { source, file })?; Ok(()) } -- cgit v1.2.3-54-g00ecf