소스 검색

chore: evaluate error context lazily (#447)

hybo 2 년 전
부모
커밋
94f647b24a
2개의 변경된 파일14개의 추가작업 그리고 11개의 파일을 삭제
  1. 4 4
      src-tauri/src/config/profiles.rs
  2. 10 7
      src-tauri/src/utils/help.rs

+ 4 - 4
src-tauri/src/config/profiles.rs

@@ -138,9 +138,9 @@ impl IProfiles {
             let path = dirs::app_profiles_dir()?.join(&file);
             let path = dirs::app_profiles_dir()?.join(&file);
 
 
             fs::File::create(path)
             fs::File::create(path)
-                .context(format!("failed to create file \"{}\"", file))?
+                .with_context(|| format!("failed to create file \"{}\"", file))?
                 .write(file_data.as_bytes())
                 .write(file_data.as_bytes())
-                .context(format!("failed to write to file \"{}\"", file))?;
+                .with_context(|| format!("failed to write to file \"{}\"", file))?;
         }
         }
 
 
         if self.items.is_none() {
         if self.items.is_none() {
@@ -207,9 +207,9 @@ impl IProfiles {
                         let path = dirs::app_profiles_dir()?.join(&file);
                         let path = dirs::app_profiles_dir()?.join(&file);
 
 
                         fs::File::create(path)
                         fs::File::create(path)
-                            .context(format!("failed to create file \"{}\"", file))?
+                            .with_context(|| format!("failed to create file \"{}\"", file))?
                             .write(file_data.as_bytes())
                             .write(file_data.as_bytes())
-                            .context(format!("failed to write to file \"{}\"", file))?;
+                            .with_context(|| format!("failed to write to file \"{}\"", file))?;
                     }
                     }
 
 
                     break;
                     break;

+ 10 - 7
src-tauri/src/utils/help.rs

@@ -11,19 +11,21 @@ pub fn read_yaml<T: DeserializeOwned>(path: &PathBuf) -> Result<T> {
     }
     }
 
 
     let yaml_str = fs::read_to_string(&path)
     let yaml_str = fs::read_to_string(&path)
-        .context(format!("failed to read the file \"{}\"", path.display()))?;
+        .with_context(|| format!("failed to read the file \"{}\"", path.display()))?;
 
 
-    serde_yaml::from_str::<T>(&yaml_str).context(format!(
-        "failed to read the file with yaml format \"{}\"",
-        path.display()
-    ))
+    serde_yaml::from_str::<T>(&yaml_str).with_context(|| {
+        format!(
+            "failed to read the file with yaml format \"{}\"",
+            path.display()
+        )
+    })
 }
 }
 
 
 /// read mapping from yaml fix #165
 /// read mapping from yaml fix #165
 pub fn read_merge_mapping(path: &PathBuf) -> Result<Mapping> {
 pub fn read_merge_mapping(path: &PathBuf) -> Result<Mapping> {
     let mut val: Value = read_yaml(path)?;
     let mut val: Value = read_yaml(path)?;
     val.apply_merge()
     val.apply_merge()
-        .context(format!("failed to apply merge \"{}\"", path.display()))?;
+        .with_context(|| format!("failed to apply merge \"{}\"", path.display()))?;
 
 
     Ok(val
     Ok(val
         .as_mapping()
         .as_mapping()
@@ -45,7 +47,8 @@ pub fn save_yaml<T: Serialize>(path: &PathBuf, data: &T, prefix: Option<&str>) -
     };
     };
 
 
     let path_str = path.as_os_str().to_string_lossy().to_string();
     let path_str = path.as_os_str().to_string_lossy().to_string();
-    fs::write(path, yaml_str.as_bytes()).context(format!("failed to save file \"{path_str}\""))
+    fs::write(path, yaml_str.as_bytes())
+        .with_context(|| format!("failed to save file \"{path_str}\""))
 }
 }
 
 
 const ALPHABET: [char; 62] = [
 const ALPHABET: [char; 62] = [