remove const configuration in favor of runtime config

korrat
Sep 29, 2022, 5:28 PM
SJ6AFVZL5HEXG5ZUV5STIGGIGLU56WGAQCSZBFFHQOVHAILBOS2QC

Dependencies

  • [2] KWGWHKJR introduce ensure_column and use it for padding account names
  • [3] ND7GASJ4 track current column position when writing
  • [4] QRIJE4AQ add a simple pretty printer for beancount directives
  • [5] D6UTHZA4 add a simple writer for saving a set of directives to a tree of files
  • [6] ONRIF4V7 add basic snapshot test for pretty printer

Change contents

  • edit in common/beancount-tree-writer/src/lib.rs at line 1
    [3.83]
    [3.84]
    use beancount_pretty_printer::Config as PrettyPrinterConfig;
  • edit in common/beancount-tree-writer/src/lib.rs at line 26
    [3.708]
    [3.708]
    pub pretty_printer: PrettyPrinterConfig,
  • replacement in common/beancount-tree-writer/src/lib.rs at line 97
    [3.2575][3.2575:2667]()
    PrettyPrinter::<_, 2, 2, 100>::buffered(file).print_directives(&directives)
    [3.2575]
    [3.2667]
    PrettyPrinter::buffered(self.config.pretty_printer, file)
    .print_directives(&directives)
  • edit in common/beancount-pretty-printer/tests/snapshotting.rs at line 1
    [3.59]
    [3.60]
    use beancount_pretty_printer::Config;
  • replacement in common/beancount-pretty-printer/tests/snapshotting.rs at line 14
    [3.429][3.429:507]()
    let mut printer = PrettyPrinter::<_, 2, 2, 100>::unbuffered(&mut buffer);
    [3.429]
    [3.507]
    let mut printer = PrettyPrinter::unbuffered(Config::default(), &mut buffer);
  • edit in common/beancount-pretty-printer/tests/snapshots/snapshotting__pretty_printed_transaction.snap at line 3
    [3.1842]
    [3.1842]
    assertion_line: 48
  • replacement in common/beancount-pretty-printer/tests/snapshots/snapshotting__pretty_printed_transaction.snap at line 7
    [3.1881][3.1881:2139]()
    Assets:Checking -100.00 EUR
    Assets:Wallet 100.00 EUR
    [3.1881]
    Assets:Checking -100.00 EUR
    Assets:Wallet 100.00 EUR
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 13
    [3.367][3.367:420]()
    // TODO consider reimplementation based on pretty.rs
    [3.367]
    [3.420]
    #[derive(Clone, Copy, Debug)]
    pub struct AmountConfig {
    pub decimal_separator_column: usize,
    pub commodity_column: usize,
    }
    impl Default for AmountConfig {
    fn default() -> Self {
    Self {
    decimal_separator_column: 110,
    commodity_column: 116,
    }
    }
    }
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 28
    [3.421][3.421:553]()
    pub struct PrettyPrinter<
    W,
    const FLAG_COLUMN: usize,
    const ACCOUNT_COLUMN: usize,
    const AMOUNT_COLUMN: usize,
    > {
    [3.421]
    [3.27]
    #[derive(Clone, Copy, Debug)]
    pub struct Config {
    pub account_column: usize,
    pub amount: AmountConfig,
    pub flag_column: usize,
    }
    impl Default for Config {
    fn default() -> Self {
    Self {
    account_column: 2,
    amount: Default::default(),
    flag_column: 2,
    }
    }
    }
    pub struct PrettyPrinter<W> {
    config: Config,
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 53
    [3.570][3.570:726]()
    impl<W, const FLAG_COLUMN: usize, const ACCOUNT_COLUMN: usize, const AMOUNT_COLUMN: usize>
    PrettyPrinter<W, FLAG_COLUMN, ACCOUNT_COLUMN, AMOUNT_COLUMN>
    [3.570]
    [3.726]
    impl<W> PrettyPrinter<W>
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 57
    [3.748][3.748:790]()
    pub fn unbuffered(inner: W) -> Self {
    [3.748]
    [3.58]
    pub fn unbuffered(config: Config, inner: W) -> Self {
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 59
    [3.106][3.790:813](),[3.790][3.790:813]()
    Self { inner }
    [3.106]
    [3.813]
    Self { config, inner }
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 63
    [3.822][3.822:989]()
    impl<W, const FLAG_COLUMN: usize, const ACCOUNT_COLUMN: usize, const AMOUNT_COLUMN: usize>
    PrettyPrinter<BufWriter<W>, FLAG_COLUMN, ACCOUNT_COLUMN, AMOUNT_COLUMN>
    [3.822]
    [3.989]
    impl<W> PrettyPrinter<BufWriter<W>>
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 67
    [3.1011][3.1011:1051](),[3.1051][3.2140:2188]()
    pub fn buffered(inner: W) -> Self {
    Self::unbuffered(BufWriter::new(inner))
    [3.1011]
    [3.1117]
    pub fn buffered(config: Config, inner: W) -> Self {
    Self::unbuffered(config, BufWriter::new(inner))
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 72
    [3.1126][3.1126:1282]()
    impl<W, const FLAG_COLUMN: usize, const ACCOUNT_COLUMN: usize, const AMOUNT_COLUMN: usize>
    PrettyPrinter<W, FLAG_COLUMN, ACCOUNT_COLUMN, AMOUNT_COLUMN>
    [3.1126]
    [3.1282]
    impl<W> PrettyPrinter<W>
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 83
    [3.1478][3.1478:1632]()
    let account_width = AMOUNT_COLUMN - 19;
    write!(self.inner, "{date} balance {account:account_width$}")?;
    self.print_amount(amount)
    [3.1478]
    [3.1632]
    write!(self.inner, "{date} balance {account}")?;
    self.print_amount(self.config.amount, amount)
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 137
    [3.3219][3.3219:3375]()
    impl<W, const FLAG_COLUMN: usize, const ACCOUNT_COLUMN: usize, const AMOUNT_COLUMN: usize>
    PrettyPrinter<W, FLAG_COLUMN, ACCOUNT_COLUMN, AMOUNT_COLUMN>
    [3.3219]
    [3.3375]
    impl<W> PrettyPrinter<W>
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 141
    [3.3397][3.3397:3461]()
    fn print_amount(&mut self, amount: &Amount) -> Result<()> {
    [3.3397]
    [3.3461]
    fn print_amount(&mut self, config: AmountConfig, amount: &Amount) -> Result<()> {
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 144
    [3.3513][3.3513:3613]()
    self.print_decimal_aligned(amount, 15, 4)?;
    write!(self.inner, " {commodity:12}")?;
    [3.3513]
    [3.3613]
    self.print_decimal_aligned(config.decimal_separator_column, amount)?;
    self.inner.ensure_column(config.commodity_column)?;
    write!(self.inner, "{commodity}")?;
  • edit in common/beancount-pretty-printer/src/lib.rs at line 163
    [3.3899]
    [3.3899]
    decimal_separator_column: usize,
  • edit in common/beancount-pretty-printer/src/lib.rs at line 165
    [3.3926][3.3926:3979]()
    total_width: usize,
    decimals: usize,
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 166
    [3.4001][2.0:50]()
    self.inner.ensure_column(AMOUNT_COLUMN)?;
    [3.4001]
    [2.50]
    let decimals = decimal.scale() as usize;
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 168
    [2.51][3.4001:4113](),[3.4001][3.4001:4113]()
    // TODO handle this with icu or similar (e.g. for digit grouping)?
    let mut buffer = [b' '; 32];
    [2.51]
    [3.4113]
    let mut buffer = [b' '; 64];
    write!(buffer.as_mut_slice(), "{decimal}")?;
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 171
    [3.4114][3.4114:4248]()
    let width = total_width - (decimals - (decimal.scale() as usize));
    write!(&mut buffer[..width], "{decimal:width$}")?;
    [3.4114]
    [3.4248]
    let buffer = buffer.trim();
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 173
    [3.4249][3.4249:4320]()
    self.inner.write_all(&buffer[..total_width])?;
    Ok(())
    [3.4249]
    [3.4320]
    // We can ignore the part right of the decimal separator when aligning the amount on the decimal separator
    let integral_digits = buffer.graphemes().count() - (decimals + 1);
    self.inner
    .ensure_column(decimal_separator_column - integral_digits)?;
    self.inner.write_all(buffer)
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 193
    [3.4805][3.4805:4845]()
    self.print_amount(amount)?;
    [3.4805]
    [3.4845]
    self.print_amount(self.config.amount, amount)?;
  • replacement in common/beancount-pretty-printer/src/lib.rs at line 228
    [2.265][2.265:325]()
    self.inner.write_fmt(format_args!("{:shift$}", ""))
    [2.265]
    [2.325]
    self.write_fmt(format_args!("{:shift$}", ""))