Kestrel 2 днів тому
батько
коміт
9a74b7ec06
2 змінених файлів з 32 додано та 26 видалено
  1. 30 21
      src/data.rs
  2. 2 5
      src/data/ledger.rs

+ 30 - 21
src/data.rs

@@ -72,7 +72,7 @@ impl ariadne::Cache<SourceFile> for FilesystemData {
 #[derive(Debug)]
 pub enum DataError {
     IOError(std::io::Error),
-    Report(ariadne::Report<'static, (SourceFile, std::ops::Range<usize>)>),
+    Report(Box<ariadne::Report<'static, (SourceFile, std::ops::Range<usize>)>>),
     Validation(String),
 }
 
@@ -82,6 +82,18 @@ impl std::fmt::Display for DataError {
     }
 }
 
+impl From<std::io::Error> for DataError {
+    fn from(value: std::io::Error) -> Self {
+        Self::IOError(value)
+    }
+}
+
+impl From<ariadne::Report<'static, (SourceFile, std::ops::Range<usize>)>> for DataError {
+    fn from(value: ariadne::Report<'static, (SourceFile, std::ops::Range<usize>)>) -> Self {
+        Self::Report(value.into())
+    }
+}
+
 impl std::error::Error for DataError {}
 
 #[derive(Debug)]
@@ -95,31 +107,28 @@ pub struct Root {
 }
 
 impl Root {
-    pub fn load<'l, 'm>(
-        fsdata: &'l mut FilesystemData,
-        path: &'m std::path::Path,
-    ) -> Result<Self, DataError> {
+    pub fn load(fsdata: &mut FilesystemData, path: &std::path::Path) -> Result<Self, DataError> {
         let sf = SourceFile::new(path.as_os_str());
         let root_data = fsdata.fetch(&sf).unwrap();
 
         match toml::from_str::<spec::RootSpec>(root_data.text()) {
             Ok(mut root_spec) => {
                 let initial_name = AccountName::from("initial");
-                if let Some(_) = root_spec.accounts.get(&initial_name) {
+                if let std::collections::hash_map::Entry::Vacant(ve) =
+                    root_spec.accounts.entry(initial_name)
+                {
+                    ve.insert(spec::AccountSpec {
+                        title: None,
+                        description: None,
+                        annotations: None,
+                        default_unit: None,
+                    });
+                } else {
                     return Err(DataError::Validation(String::from(
                         "cannot define 'initial' account, as it is a built-in",
                     )));
-                } else {
-                    root_spec.accounts.insert(
-                        initial_name,
-                        spec::AccountSpec {
-                            title: None,
-                            description: None,
-                            annotations: None,
-                            default_unit: None,
-                        },
-                    );
                 }
+
                 let mut r = Self {
                     path: path.into(),
                     root_spec,
@@ -143,14 +152,14 @@ impl Root {
                         .with_message("Failed to parse root TOML")
                         .finish();
 
-                Err(DataError::Report(report))
+                Err(report.into())
             }
         }
     }
 
-    fn load_ledger<'s, 'd>(
-        &'s mut self,
-        fsdata: &'d mut FilesystemData,
+    fn load_ledger(
+        &mut self,
+        fsdata: &mut FilesystemData,
         path: &mut std::path::PathBuf,
     ) -> Result<(), DataError> {
         log::debug!("Loading ledger data from {}", path.display());
@@ -177,7 +186,7 @@ impl Root {
         Ok(())
     }
 
-    fn load_ledgers<'s, 'd>(&'s mut self, fsdata: &'d mut FilesystemData) -> Result<(), DataError> {
+    fn load_ledgers(&mut self, fsdata: &mut FilesystemData) -> Result<(), DataError> {
         let mut ledger_path = self.path.to_owned();
         ledger_path.pop();
         ledger_path.push(&self.root_spec.ledger_path);

+ 2 - 5
src/data/ledger.rs

@@ -24,10 +24,7 @@ pub struct LedgerEntry {
 
 impl LedgerEntry {
     pub fn modifies(&self, account: AccountName) -> bool {
-        self.balances
-            .iter()
-            .find(|b| b.account == account)
-            .is_some()
+        self.balances.iter().any(|b| b.account == account)
     }
 
     pub fn balance_for(&self, account: AccountName) -> Option<&Balance> {
@@ -162,7 +159,7 @@ pub fn parse_ledger(
             .with_label(ariadne::Label::new((source, span)).with_message(e.reason()))
             .finish();
 
-        Err(crate::data::DataError::Report(report))
+        Err(report.into())
     } else {
         Ok(presult.unwrap())
     }