use fixed_decimal for decimal formatting

korrat
Sep 29, 2022, 5:50 PM
M7VINXOFAKFTEKQZR3RFO3WJQ3RXE2TWD4YAVKTPLMJWC4LISTIQC

Dependencies

  • [2] SJ6AFVZL remove const configuration in favor of runtime config
  • [3] KWGWHKJR introduce ensure_column and use it for padding account names
  • [4] QRIJE4AQ add a simple pretty printer for beancount directives
  • [*] ND7GASJ4 track current column position when writing
  • [*] I2P2FTLE add basic parser for german decimals
  • [*] ONRIF4V7 add basic snapshot test for pretty printer
  • [*] YDK6X6PP add a library of important types for beancount
  • [*] D6UTHZA4 add a simple writer for saving a set of directives to a tree of files

Change contents

  • edit in common/beancount-pretty-printer/src/lib.rs at line 8
    [6.26]
    [3.274]
    use fixed_decimal::FixedDecimal;
    use fixed_decimal::Sign;
  • edit in common/beancount-pretty-printer/src/lib.rs at line 90
    [3.1716][3.1716:1760]()
    // TODO prevent trailing whitespace
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 106
    [3.2307][3.2307:2359]()
    self.inner.write_all(b"\n\n")?;
    [3.2307]
    [3.2359]
    self.inner.write_all(b"\n")?;
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 109
    [3.2378][3.2378:2426]()
    self.print_directive(directive)
    [3.2378]
    [3.2426]
    self.print_directive(directive)?;
    self.inner.write_all(b"\n")
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 168
    [3.4001][2.2047:2096](),[2.2096][3.50:51](),[3.50][3.50:51](),[3.51][2.2097:2187](),[2.2187][3.4113:4114](),[3.4113][3.4113:4114](),[3.4114][2.2188:2224]()
    let decimals = decimal.scale() as usize;
    let mut buffer = [b' '; 64];
    write!(buffer.as_mut_slice(), "{decimal}")?;
    let buffer = buffer.trim();
    [3.4001]
    [3.4248]
    let decimal = fixed_decimal_from(decimal);
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 171
    [2.2340][2.2340:2415]()
    let integral_digits = buffer.graphemes().count() - (decimals + 1);
    [2.2340]
    [2.2415]
    let sign_width = matches!(decimal.sign(), Sign::Negative) as usize;
    let integral_width = decimal.nonzero_magnitude_start().try_into().unwrap_or(0) + 1;
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 175
    [2.2434][2.2434:2544]()
    .ensure_column(decimal_separator_column - integral_digits)?;
    self.inner.write_all(buffer)
    [2.2434]
    [3.4320]
    .ensure_column(decimal_separator_column - (sign_width + integral_width))?;
    write!(self.inner, "{decimal}")
  • edit in common/beancount-pretty-printer/src/lib.rs at line 256
    [6.915]
    fn fixed_decimal_from(decimal: &Decimal) -> FixedDecimal {
    let scale: i16 = decimal.scale().try_into().unwrap();
    FixedDecimal::from(decimal.mantissa()).multiplied_pow10(-scale)
    }
  • edit in common/beancount-pretty-printer/Cargo.toml at line 14
    [6.1033]
    [6.1033]
    fixed_decimal.workspace = true
  • edit in Cargo.toml at line 11
    [6.1079]
    [8.2350]
    fixed_decimal = "0.5.0"
  • edit in Cargo.lock at line 62
    [6.1089]
    [8.2495]
    "fixed_decimal",
  • edit in Cargo.lock at line 270
    [9.28291]
    [9.28291]
    name = "displaydoc"
    version = "0.2.3"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "3bf95dc3f046b9da4f2d51833c0d3547d8564ef6910f5c1ed130306a75b92886"
    dependencies = [
    "proc-macro2",
    "quote",
    "syn",
    ]
    [[package]]
  • edit in Cargo.lock at line 315
    [9.28892]
    [9.28892]
    ]
    [[package]]
    name = "fixed_decimal"
    version = "0.5.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "ceab5dbd7a3a08385160503f6b6636420dbd72d29babdf8c894d4534304191c3"
    dependencies = [
    "displaydoc",
    "smallvec",
    "static_assertions",
    "writeable",
  • edit in Cargo.lock at line 1069
    [10.15689]
    [9.39189]
    name = "writeable"
    version = "0.5.0"
    source = "registry+https://github.com/rust-lang/crates.io-index"
    checksum = "f8e6ab4f5da1b24daf2c590cfac801bacb27b15b4f050e84eb60149ea726f06b"
    [[package]]