Expand description
A double entry accounting system/library.
§Optional Features
The doublecount package has the following optional cargo features:
serde-support
- Disabled by default
- Enables support for serialization/de-serialization via
serde
§Usage
use doublecount::{
AccountStatus, EditAccountStatus, Account, Program, Action,
ProgramState, Transaction, TransactionElement, BalanceAssertion,
ActionTypeValue,
};
use commodity::{CommodityType, Commodity};
use chrono::NaiveDate;
use std::rc::Rc;
use std::str::FromStr;
// create a commodity from a currency's iso4317 alphanumeric code
let aud = Rc::from(CommodityType::from_str("AUD", "Australian Dollar").unwrap());
// Create a couple of accounts
let account1 = Rc::from(Account::new_with_id(Some("Account 1"), aud.id, None));
let account2 = Rc::from(Account::new_with_id(Some("Account 2"), aud.id, None));
// create a new program state, with accounts starting Closed
let mut program_state = ProgramState::new(
&vec![account1.clone(), account2.clone()],
AccountStatus::Closed
);
// open account1
let open_account1 = EditAccountStatus::new(
account1.id,
AccountStatus::Open,
NaiveDate::from_str("2020-01-01").unwrap(),
);
// open account2
let open_account2 = EditAccountStatus::new(
account2.id,
AccountStatus::Open,
NaiveDate::from_str("2020-01-01").unwrap(),
);
// create a transaction to transfer some commodity
// from account1 to account2.
let transaction1 = Transaction::new(
Some(String::from("Transaction 1")),
NaiveDate::from_str("2020-01-02").unwrap(),
vec![
TransactionElement::new(
account1.id,
Some(Commodity::from_str("-2.52 AUD").unwrap()),
None,
),
TransactionElement::new(
account2.id,
Some(Commodity::from_str("2.52 AUD").unwrap()),
None,
),
],
);
// create a balance assertion (that will cause the program to return an error
// if it fails), to check that the balance of account1 matches the expected
// value of -1.52 AUD at the start of the date of 2020-01-03
let balance_assertion1 = BalanceAssertion::new(
account1.id,
NaiveDate::from_str("2020-01-03").unwrap(),
Commodity::from_str("-2.52 AUD").unwrap()
);
// create another transaction to transfer commodity from
// account2 to account1, using the simpler syntax.
let transaction2 = Transaction::new_simple(
Some("Transaction 2"),
NaiveDate::from_str("2020-01-03").unwrap(),
account2.id,
account1.id,
Commodity::from_str("1.0 AUD").unwrap(),
None,
);
let balance_assertion2 = BalanceAssertion::new(
account1.id,
NaiveDate::from_str("2020-01-04").unwrap(),
Commodity::from_str("-1.52 AUD").unwrap()
);
let balance_assertion3 = BalanceAssertion::new(
account2.id,
NaiveDate::from_str("2020-01-04").unwrap(),
Commodity::from_str("1.52 AUD").unwrap()
);
let actions: Vec<Rc<ActionTypeValue>> = vec![
Rc::new(open_account1.into()),
Rc::new(open_account2.into()),
Rc::new(transaction1.into()),
Rc::new(balance_assertion1.into()),
Rc::new(transaction2.into()),
Rc::new(balance_assertion2.into()),
Rc::new(balance_assertion3.into()),
];
// create a program from the actions
let program = Program::new(actions);
// run the program
program_state.execute_program(&program).unwrap();
Structs§
- Account
- Details for an account, which holds a Commodity with a type of CommodityType.
- Account
State - Mutable state associated with an Account.
- Action
Order - A way to sort Actions by their date, and then by the priority of their ActionType.
- Balance
Assertion - A type of Action to check and assert the balance of a given Account in its AccountStatus at the beginning of the given date.
- Edit
Account Status - A type of Action to edit the AccountStatus of a given Account’s AccountState.
- Failed
Balance Assertion - Records the failure of a BalanceAssertion when it is evaluated using its implementation of the Action::perform() method.
- Program
- A collection of Actions to be executed in order to mutate some ProgramState.
- Program
State - The state of a Program being executed.
- Transaction
- A movement of Commodity between two or more accounts
on a given
date
. Implements Action so it can be applied to change AccountStates. - Transaction
Element - An element of a Transaction.
Enums§
- Account
Status - The status of an Account stored within an AccountState.
- Accounting
Error - An error associated with functionality in the accounting module.
- Action
Type - A representation of what type of Action is being performed.
- Action
Type Value - An enum to store every possible concrete implementation of
Action in a
Sized
element.
Traits§
- Action
- Represents an action which can modify ProgramState.
- Action
Type For - Obtain the concrete action type for an action.
- Action
Type Value Enum - A trait which represents an enum/sized data structure which is capable of storing every possible concrete implementation of Action for your Program.
Functions§
- sum_
account_ states - Sum the values in all the accounts into a single
Commodity, and use the supplied exchange rate if
required to convert a type of commodity in an account to the
CommidityType associated with the
id
sum_commodity_type_id
.
Type Aliases§
- Account
Category - A way to categorize Accounts.
- AccountID
- The type to use for the id of Accounts.