Sfoglia il codice sorgente

Address clippy nits.

Kestrel 2 settimane fa
parent
commit
37d8217257
7 ha cambiato i file con 26 aggiunte e 29 eliminazioni
  1. 2 2
      src/check.rs
  2. 2 2
      src/cmd.rs
  3. 1 1
      src/cmd/infer.rs
  4. 10 7
      src/data.rs
  5. 4 2
      src/data/parse.rs
  6. 6 5
      src/import.rs
  7. 1 10
      src/io.rs

+ 2 - 2
src/check.rs

@@ -121,7 +121,7 @@ fn check_balances(root: &data::Hoard) -> Result<(), DataError> {
                 .or_insert_with(|| Spanned::new(data::Decimal::default(), io::Span::default()));
             let last_span = bal.span();
             bal.0 = bal.checked_add(*change.amount).unwrap();
-            bal.1 = change.1;
+            bal.1 = change.source.unwrap();
 
             if let Some(sbal) = change.balance.as_ref() {
                 if **sbal != bal.0 {
@@ -133,7 +133,7 @@ fn check_balances(root: &data::Hoard) -> Result<(), DataError> {
                     let report = if !last_span.is_empty() {
                         report.with_label(
                             ariadne::Label::new(last_span)
-                                .with_message(format!("Last balance is from here")),
+                                .with_message("Last balance is from here"),
                         )
                     } else {
                         report

+ 2 - 2
src/cmd.rs

@@ -114,7 +114,7 @@ impl Command {
 
                 let tt = show::TransactionTable::default();
                 if let Some(ld) = data.ledger_data_for(aname) {
-                    tt.show(Some(&data), aname, ld.iter().map(io::Spanned::as_ref));
+                    tt.show(Some(&data), aname, ld.iter().map(Spanned::as_ref));
                 } else {
                     log::error!("account not found!");
                 }
@@ -139,7 +139,7 @@ impl Command {
                         let Some(txn) = le.as_transaction_mut() else {
                             continue;
                         };
-                        if !txn.get_annotation("id").is_some() {
+                        if txn.get_annotation("id").is_none() {
                             // generated unique ID
                             let mut id = nanoid::nanoid!(10);
                             while used_ids.contains(&id) {

+ 1 - 1
src/cmd/infer.rs

@@ -58,7 +58,7 @@ pub fn do_inference(data: &mut Hoard) -> anyhow::Result<bool> {
             let answer = console::Term::stdout().read_char().unwrap();
             match answer.to_lowercase().next() {
                 Some('y') | Some('\n') | Some('\r') => {
-                    let new_account = candidate.account.0.clone();
+                    let new_account = *candidate.account;
                     println!(
                         "    Changing to account {} ...",
                         console::style(new_account).green()

+ 10 - 7
src/data.rs

@@ -9,13 +9,12 @@ pub use rust_decimal::Decimal;
 
 use crate::prelude::*;
 
-pub mod spec;
-
+mod format;
 mod parse;
-pub use parse::parse_ledger;
+pub mod spec;
 
-mod format;
 pub use format::format_ledger;
+pub use parse::parse_ledger;
 
 pub struct UnitTag;
 impl stringstore::NamespaceTag for UnitTag {
@@ -50,6 +49,8 @@ impl std::fmt::Debug for Datestamp {
 
 #[derive(Clone, Debug, PartialEq, PartialOrd, Ord, Eq)]
 pub struct Change {
+    pub source: Option<io::Span>,
+
     pub account: Spanned<AccountName>,
     pub amount: Spanned<Decimal>,
     pub balance: Option<Spanned<Decimal>>,
@@ -58,10 +59,12 @@ pub struct Change {
 
 #[derive(Clone, Debug, PartialEq, PartialOrd, Eq, Ord)]
 pub struct Transaction {
+    pub source: Option<io::Span>,
+
     pub datestamp: Datestamp,
     pub title: Option<String>,
     pub annotations: Vec<String>,
-    pub changes: Vec<Spanned<Change>>,
+    pub changes: Vec<Change>,
 }
 
 impl Transaction {
@@ -69,14 +72,14 @@ impl Transaction {
         self.changes.iter().any(|b| b.account.as_ref() == &account)
     }
 
-    pub fn change_for(&self, account: AccountName) -> Option<&Spanned<Change>> {
+    pub fn change_for(&self, account: AccountName) -> Option<&Change> {
         self.changes.iter().find(|b| b.account.as_ref() == &account)
     }
 
     pub fn split_changes(
         &self,
         account: AccountName,
-    ) -> Option<(&Spanned<Change>, impl Iterator<Item = &Spanned<Change>>)> {
+    ) -> Option<(&Change, impl Iterator<Item = &Change>)> {
         let index = self
             .changes
             .iter()

+ 4 - 2
src/data/parse.rs

@@ -105,12 +105,13 @@ fn ledger_parser<'a>() -> impl Parser<
             return Err(chumsky::error::Rich::custom(unit.span(), format!("no such unit '{unit}' found")))
         }
 
-        Ok(Spanned::new(Change {
+        Ok(Change {
             account: acc,
             amount,
             balance,
             unit,
-        }, span))
+            source: Some(span),
+        })
     });
 
     let annotation = mark('[')
@@ -145,6 +146,7 @@ fn ledger_parser<'a>() -> impl Parser<
                     title: (!title.is_empty()).then_some(title),
                     annotations,
                     changes,
+                    source: Some(e.span()),
                 },
                 e.span(),
             ))

+ 6 - 5
src/import.rs

@@ -152,8 +152,8 @@ fn import_from_csv(
                     amount: txn_change.unwrap().into(),
                     balance: txn_balance.map(Into::into),
                     unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
-                }
-                .into(),
+                    source: None,
+                },
                 data::Change {
                     account: target.into(),
                     amount: data::Decimal::ZERO
@@ -162,9 +162,10 @@ fn import_from_csv(
                         .into(),
                     balance: None,
                     unit: txn_unit.or(aspec.unit).map(Into::into).unwrap(),
-                }
-                .into(),
+                    source: None,
+                },
             ],
+            source: None,
         });
     }
 
@@ -183,7 +184,7 @@ fn recursive_order_search(
 
     let possibles = remaining.iter().cloned().collect::<Vec<_>>();
 
-    if let Some(last) = order.last().as_deref() {
+    if let Some(last) = order.last() {
         let Some(last_balance) = txns[*last].change_for(account).unwrap().balance.as_deref() else {
             return false;
         };

+ 1 - 10
src/io.rs

@@ -7,7 +7,7 @@ impl stringstore::NamespaceTag for SourceTag {
 /// Canonicalized path to a file
 pub type SourceFile = stringstore::StoredOsString<SourceTag>;
 
-#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
+#[derive(Debug, Default, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
 pub struct Span {
     pub range: (usize, usize),
     pub context: Option<SourceFile>,
@@ -22,15 +22,6 @@ impl Span {
     }
 }
 
-impl Default for Span {
-    fn default() -> Self {
-        Self {
-            range: (0, 0),
-            context: None,
-        }
-    }
-}
-
 impl chumsky::span::Span for Span {
     type Offset = usize;
     type Context = SourceFile;