Skip to content

Commit

Permalink
Renamed argument in sum_account_states() and improved docs
Browse files Browse the repository at this point in the history
  • Loading branch information
kellpossible committed Mar 22, 2020
1 parent a3a7991 commit 5832f0a
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 24 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,10 @@
# Changelog

## v0.6.0

+ Renamed argument in `sum_account_states()`, `sum_currency` to
`sum_commodity_type_id` to better match the recent changes in `commodity` library.

## v0.5.0

+ Updated `commodity` library dependency to `v0.3.0`, renamed some types
Expand Down
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "doublecount"
version = "0.5.0"
version = "0.6.0"
description = "A double entry accounting system/library."
keywords = ["financial", "currency", "accounting", "exchange", "rate"]
categories = ["science", "mathematics"]
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use chrono::NaiveDate;
use std::rc::Rc;
use std::str::FromStr;

// create a currency from its iso4317 alphanumeric code
// create a commodity from a currency's iso4317 alphanumeric code
let aud = Rc::from(CommodityType::from_currency_alpha3("AUD").unwrap());

// Create a couple of accounts
Expand Down
4 changes: 2 additions & 2 deletions src/account.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ pub type AccountID = ArrayString<[u8; ACCOUNT_ID_LENGTH]>;
pub type AccountCategory = String;

/// Details for an account, which holds a [Commodity](Commodity)
/// with a type of [Currency](commodity::Currency).
/// with a type of [CommodityType](commodity::CommodityType).
#[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug, Clone)]
pub struct Account {
Expand All @@ -36,7 +36,7 @@ pub struct Account {
/// The name of this `Account`
pub name: Option<String>,

/// The type of currency to be stored in this account
/// The id of the type of commodity to be stored in this account
pub commodity_type_id: CommodityTypeID,

/// The category that this account part of
Expand Down
21 changes: 11 additions & 10 deletions src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use serde::{Deserialize, Serialize};
/// A representation of what type of [Action](Action) is being performed.
#[derive(PartialEq, Eq, Debug, PartialOrd, Ord, Hash, Clone)]
pub enum ActionType {
/// An [Action](Action) to edit the status of an [Account](Account).
/// An [Action](Action) to edit the status of an [Account](crate::Account).
/// Represented by the [EditAccountStatus](EditAccountStatus) struct.
///
/// This action has the highest priority when being sorted, because
Expand All @@ -24,7 +24,7 @@ pub enum ActionType {
/// a [Program](super::Program) is being executed. Represented by a
/// [BalanceAssertion](BalanceAssertion) struct.
BalanceAssertion,
/// A [Action](Action) to perform a transaction between [Account](Account)s.
/// A [Action](Action) to perform a transaction between [Account](crate::Account)s.
/// Represented by the [Transaction](Transaction) struct.
Transaction,
}
Expand Down Expand Up @@ -188,7 +188,8 @@ impl Transaction {
)
}

/// Get the [TransactionElement](TransactionElement) associated with the given [Account](Account)'s id.
/// Get the [TransactionElement](TransactionElement) associated
/// with the given [Account](crate::Account)'s id.
pub fn get_element(&self, account_id: &AccountID) -> Option<&TransactionElement> {
self.elements.iter().find(|e| &e.account_id == account_id)
}
Expand Down Expand Up @@ -232,7 +233,7 @@ impl Action for Transaction {
}
}

let sum_currency_code = match empty_amount_element {
let sum_commodity_type_id = match empty_amount_element {
Some(empty_i) => {
let empty_element = self.elements.get(empty_i).unwrap();

Expand All @@ -259,7 +260,7 @@ impl Action for Transaction {
}
};

let mut sum = Commodity::new(Decimal::zero(), sum_currency_code);
let mut sum = Commodity::new(Decimal::zero(), sum_commodity_type_id);

let mut modified_elements = self.elements.clone();

Expand All @@ -268,7 +269,7 @@ impl Action for Transaction {
match empty_amount_element {
Some(empty_i) => {
if i != empty_i {
//TODO: perform currency conversion here if required
//TODO: perform commodity type conversion here if required
sum = match sum.add(&element.amount.as_ref().unwrap()) {
Ok(value) => value,
Err(error) => return Err(AccountingError::Commodity(error)),
Expand Down Expand Up @@ -321,7 +322,7 @@ impl Action for Transaction {
_ => Ok(()),
}?;

// TODO: perform the currency conversion using the exchange rate (if present)
// TODO: perform the commodity type conversion using the exchange rate (if present)

let transaction_amount = match &transaction.amount {
Some(amount) => amount,
Expand Down Expand Up @@ -367,7 +368,7 @@ pub struct TransactionElement {
pub amount: Option<Commodity>,

/// The exchange rate to use for converting the amount in this element
/// to a different [Currency](commodity::Currency)
/// to a different [CommodityType](commodity::CommodityType).
pub exchange_rate: Option<ExchangeRate>,
}

Expand All @@ -387,7 +388,7 @@ impl TransactionElement {
}

/// A type of [Action](Action) to edit the
/// [AccountStatus](AccountStatus) of a given [Account](Account)'s
/// [AccountStatus](AccountStatus) of a given [Account](crate::Account)'s
/// [AccountState](super::AccountState).
// #[cfg_attr(feature = "serde-support", derive(Serialize, Deserialize))]
#[derive(Debug)]
Expand Down Expand Up @@ -437,7 +438,7 @@ impl Action for EditAccountStatus {
}

/// A type of [Action](Action) to check and assert the balance of a
/// given [Account](Account) in its [AccountStatus](AccountStatus) at
/// given [Account](crate::Account) in its [AccountStatus](AccountStatus) at
/// the beginning of the given date.
///
/// When running its [perform()](Action::perform()) method, if this
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub enum AccountingError {
DateParseError(#[from] chrono::ParseError),
#[error("invalid transaction {0:?} because {1}")]
InvalidTransaction(Transaction, String),
#[error("failed checksum, the sum of account values in the common currency ({0}) does not equal zero")]
#[error("failed checksum, the sum of account values in the common commodity type ({0}) does not equal zero")]
FailedCheckSum(Commodity),
#[error("no exchange rate supplied, unable to convert commodity {0} to type {1}")]
NoExchangeRateSupplied(Commodity, CommodityTypeID),
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@
//! use std::rc::Rc;
//! use std::str::FromStr;
//!
//! // create a currency from its iso4317 alphanumeric code
//! // create a commodity from a currency's iso4317 alphanumeric code
//! let aud = Rc::from(CommodityType::from_currency_alpha3("AUD").unwrap());
//!
//! // Create a couple of accounts
Expand Down
18 changes: 10 additions & 8 deletions src/program.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,24 +78,26 @@ pub struct ProgramState {
current_action_index: usize,
}

/// Sum the values in all the accounts into a single [Commodity](Commodity), and
/// use the supplied exchange rate if required to convert a currency in an account
/// to the `sum_currency`.
/// Sum the values in all the accounts into a single
/// [Commodity](Commodity), and use the supplied exchange rate if
/// required to convert a type of commodity in an account to the
/// [CommidityType](commodity::CommodityType) associated with the
/// id `sum_commodity_type_id`.
pub fn sum_account_states(
account_states: &HashMap<AccountID, AccountState>,
sum_currency: CommodityTypeID,
sum_commodity_type_id: CommodityTypeID,
exchange_rate: Option<&ExchangeRate>,
) -> Result<Commodity, AccountingError> {
let mut sum = Commodity::zero(sum_currency);
let mut sum = Commodity::zero(sum_commodity_type_id);

for (_, account_state) in account_states {
let account_amount = if account_state.amount.type_id != sum_currency {
let account_amount = if account_state.amount.type_id != sum_commodity_type_id {
match exchange_rate {
Some(rate) => rate.convert(account_state.amount, sum_currency)?,
Some(rate) => rate.convert(account_state.amount, sum_commodity_type_id)?,
None => {
return Err(AccountingError::NoExchangeRateSupplied(
account_state.amount,
sum_currency,
sum_commodity_type_id,
))
}
}
Expand Down

0 comments on commit 5832f0a

Please sign in to comment.