aboutsummaryrefslogtreecommitdiffstats
path: root/src/config.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/config.rs')
-rw-r--r--src/config.rs47
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(())
}