Add a crate for Beancount close directive

korrat
Apr 9, 2024, 4:39 PM
OUMCH6BSHQ6ROU3JNCXYX54FSNOCT52GOVDW5FFLUQAYQMTYYAAAC

Dependencies

  • [2] JFZHDHYO Add a type for Beancount open directives
  • [3] OAOH3QOZ Extract metadata into separate crate
  • [4] 576M5IPA Add type for raw price specs
  • [*] YDK6X6PP add a library of important types for beancount
  • [*] XWHISGCP Extract Amount into separate crate
  • [*] WS3UUOV3 Extract commodity type into separate crate
  • [*] NG5QXPZH Extract Account into separate crate
  • [*] I2P2FTLE add basic parser for german decimals

Change contents

  • edit in common/beancount-types/src/lib.rs at line 8
    [7.1753]
    [8.1752]
    pub use beancount_close::Close;
  • edit in common/beancount-types/Cargo.toml at line 29
    [3.573]
    [3.573]
    beancount-close.path = "../../beancount/close"
  • edit in common/beancount-types/Cargo.toml at line 32
    [3.319][2.485:536]()
    beancount-open.path = "../../beancount/open"
  • edit in common/beancount-types/Cargo.toml at line 33
    [3.374]
    [3.629]
    beancount-open.path = "../../beancount/open"
  • file addition: close (d--r------)
    [9.1]
  • file addition: src (d--r------)
    [0.155]
  • file addition: lib.rs (----------)
    [0.172]
    // SPDX-FileCopyrightText: 2024 Markus Haug (Korrat)
    //
    // SPDX-License-Identifier: EUPL-1.2
    use core::fmt::Display;
    use core::hash::Hash;
    use beancount_account::Account;
    use beancount_metadata::MetadataKey;
    use beancount_metadata::MetadataMap;
    use beancount_metadata::MetadataValue;
    use time::Date;
    use time::OffsetDateTime;
    use time_tz::PrimitiveDateTimeExt;
    #[derive(Clone, Debug, Eq, Hash, PartialEq)]
    pub struct Close {
    pub date: Date,
    pub account: Account,
    pub meta: MetadataMap,
    }
    impl Close {
    pub fn new(date: Date, account: impl Into<Account>) -> Self {
    let (account, meta) = (account.into(), MetadataMap::default());
    Self {
    date,
    account,
    meta,
    }
    }
    }
    impl Close {
    #[inline]
    pub fn add_meta(
    &mut self,
    key: impl Into<MetadataKey>,
    value: impl Into<MetadataValue>,
    ) -> &mut Self {
    self.meta.insert(key.into(), value.into());
    self
    }
    #[inline]
    pub fn clear_meta(&mut self) -> &mut Self {
    self.meta.clear();
    self
    }
    #[inline]
    pub fn set_account(&mut self, account: impl Into<Account>) -> &mut Self {
    self.account = account.into();
    self
    }
    #[inline]
    pub fn set_date(&mut self, date: Date) -> &mut Self {
    self.date = date;
    self
    }
    #[inline]
    pub fn set_meta(&mut self, meta: impl Into<MetadataMap>) -> &mut Self {
    self.meta = meta.into();
    self
    }
    }
    impl Close {
    #[inline]
    #[must_use]
    pub fn timestamp(&self) -> Option<OffsetDateTime> {
    // Balance statements occur first thing in the day
    let local_timestamp = self.date.midnight();
    let timezone = time_tz::system::get_timezone().ok()?;
    local_timestamp.assume_timezone(timezone).take_first()
    }
    }
    impl Display for Close {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
    let Self {
    date,
    account,
    meta,
    } = self;
    write!(f, "{date} close {account}")?;
    for (key, value) in meta {
    write!(f, "\n {key}: {value}")?;
    }
    Ok(())
    }
    }
  • file addition: Cargo.toml (----------)
    [0.155]
    # SPDX-FileCopyrightText: 2024 Markus Haug (Korrat)
    #
    # SPDX-License-Identifier: EUPL-1.2
    [package]
    name = "beancount-close"
    authors.workspace = true
    edition.workspace = true
    publish.workspace = true
    rust-version.workspace = true
    version.workspace = true
    [dependencies]
    # Inherited dependencies
    time.workspace = true
    time-tz.workspace = true
    # Workspace dependencies
    beancount-account.path = "../account"
    beancount-metadata.path = "../metadata"
    [lints]
    workspace = true
  • edit in Cargo.lock at line 227
    [7.2633]
    [7.2633]
    ]
    [[package]]
    name = "beancount-close"
    version = "0.0.0-dev.0"
    dependencies = [
    "beancount-account",
    "beancount-metadata",
    "time 0.3.34",
    "time-tz",
  • edit in Cargo.lock at line 438
    [7.2670]
    [8.2540]
    "beancount-close",