Kestrel 1 өдөр өмнө
parent
commit
46341a3061

+ 6 - 4
src/data.rs

@@ -1,7 +1,6 @@
-#![allow(unused)]
+// #![allow(unused)]
 
 use std::collections::HashMap;
-use std::rc::Rc;
 
 use ariadne::Cache;
 use chumsky::span::Span as CSpan;
@@ -9,8 +8,7 @@ use chumsky::span::Span as CSpan;
 pub use rust_decimal::Decimal;
 
 pub mod ledger;
-mod spec;
-mod unit;
+pub mod spec;
 
 pub struct LocationTag;
 impl stringstore::NamespaceTag for LocationTag {
@@ -346,6 +344,10 @@ impl Root {
         self.spec_root.accounts.get(&aname)
     }
 
+    pub fn unit_spec(&self, unit: UnitName) -> Option<&spec::UnitSpec> {
+        self.spec_root.units.get(&unit)
+    }
+
     pub fn balance(&self, aname: AccountName) -> Option<HashMap<UnitName, Decimal>> {
         let mut running = HashMap::<UnitName, Decimal>::new();
 

+ 2 - 42
src/data/ledger/parse.rs

@@ -8,39 +8,6 @@ use chumsky::{prelude::*, text::inline_whitespace};
 
 type InputWithContext<'a> = chumsky::input::WithContext<Span, &'a str>;
 
-/*
-#[derive(Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
-struct InputWrap<'a>(SourceFile, &'a str);
-
-impl<'a> chumsky::input::Input<'a> for InputWrap<'a> {
-    type Token = char;
-    type Span = Span;
-    type Cache = ();
-    type Cursor = usize;
-    type MaybeToken = char;
-
-    fn begin(self) -> (Self::Cursor, Self::Cache) {
-        (0, ())
-    }
-
-    fn cursor_location(cursor: &Self::Cursor) -> usize {
-        *cursor
-    }
-
-    unsafe fn span(_cache: &mut Self::Cache, range: std::ops::Range<&Self::Cursor>) -> Self::Span {
-        let e : &[u8] = &[];
-        Span::new(stringstore::StoredOsString::from(e), (*range.start..*range.end))
-    }
-
-    unsafe fn next_maybe(
-        cache: &mut Self::Cache,
-        cursor: &mut Self::Cursor,
-    ) -> Option<Self::MaybeToken> {
-        None
-    }
-}
-*/
-
 fn ledger_parser<'a>() -> impl Parser<
     'a,
     InputWithContext<'a>,
@@ -71,7 +38,7 @@ fn ledger_parser<'a>() -> impl Parser<
         .try_map(|s: &str, span| {
             Ok(Spanned::new(
                 Decimal::from_str_exact(s.trim()).map_err(|e| {
-                    Rich::custom(span, format!("Failed to parse '{s}' as a decimal number"))
+                    Rich::custom(span, format!("Failed to parse '{s}' as a decimal number: {e}"))
                 })?,
                 span,
             ))
@@ -79,7 +46,7 @@ fn ledger_parser<'a>() -> impl Parser<
 
     let balance = group((
         mark('-'),
-        none_of(": \n\t").repeated().to_slice().map_with(|v, e| Spanned(stringstore::StoredString::new(v), e.span())),
+        none_of(": \n\t").repeated().to_slice().map_with(|v, e| Spanned(AccountName::new(v), e.span())),
         mark(':'),
         decimal,
         choice((
@@ -113,15 +80,8 @@ fn ledger_parser<'a>() -> impl Parser<
                     unit1
                 }
             }
-
         };
 
-        /*let unit = match unit {
-            Some(sunit) => sunit,
-            None => acc_spec.default_unit.map(|u| Spanned(u, span)).ok_or_else(||
-                chumsky::error::Rich::custom(span, format!("No unit specified and no default unit specified for account '{}'", acc.as_ref())))?
-        };*/
-
         if !spec.units.contains_key(&unit) {
             return Err(chumsky::error::Rich::custom(unit.span(), format!("no such unit '{unit}' found")))
         }

+ 1 - 3
src/data/ledger/print.rs

@@ -4,7 +4,7 @@ use itertools::Itertools;
 
 use crate::data::{Root, SourceFile, Span, Spanned};
 
-use super::{Balance, LedgerEntry};
+use super::LedgerEntry;
 
 fn print_ledger_entry(root: &Root, entry: &LedgerEntry, padding: usize) {
     println!(
@@ -30,7 +30,6 @@ fn print_ledger_entry(root: &Root, entry: &LedgerEntry, padding: usize) {
                 " - {:padding$}: {spacer}{}",
                 bal.account,
                 bal.amount,
-                padding = padding
             );
         } else {
             println!(
@@ -38,7 +37,6 @@ fn print_ledger_entry(root: &Root, entry: &LedgerEntry, padding: usize) {
                 bal.account,
                 bal.amount,
                 bal.unit,
-                padding = padding
             );
         }
     }

+ 1 - 1
src/data/spec.rs

@@ -17,7 +17,7 @@ pub struct AccountSpec {
 #[serde(deny_unknown_fields)]
 pub struct UnitSpec {
     pub name: UnitName,
-    pub format: String,
+    pub precision: Option<u8>,
 }
 
 #[derive(Debug, serde::Deserialize)]

+ 0 - 14
src/data/unit.rs

@@ -1,14 +0,0 @@
-use super::UnitName;
-
-pub struct UnitSpec {}
-
-pub struct UnitValue {
-    amount: rust_decimal::Decimal,
-    unit: UnitName,
-}
-
-impl UnitValue {
-    pub fn new(unit: UnitName, amount: rust_decimal::Decimal) -> Self {
-        Self { unit, amount }
-    }
-}

+ 2 - 2
testdata/root.toml

@@ -1,8 +1,8 @@
 ledger_path = "./ledger"
 
 [units]
-CAD = { name = "Canadian Dollar", format = "CA$" }
-USD = { name = "United States Dollar", format = "US$" }
+CAD = { name = "Canadian Dollar", precision = 2 }
+USD = { name = "United States Dollar", precision = 2 }
 
 [accounts.chequing]
 title = "Chequing"