1 unstable release
new 0.2.2 | Feb 1, 2025 |
---|
#2 in #tktax
Used in 10 crates
(5 directly)
160KB
2K
SLoC
tktax-line-item
The tktax-line-item crate defines a LineItem
trait for describing individual transaction-based entries within the broader TKTAX ecosystem. It integrates with the following companion crates:
- tktax_3p
- tktax_money
- tktax_transaction
- tktax_transaction_category
Its core functionality enables modeling of financial line items with precise quantity, pricing, tax category, and transaction references.
Overview
The primary component is:
pub trait LineItem<TxCat: TransactionCategory + 'static>: Debug {
/// Provides the quantity of this line item.
fn quantity(&self) -> Decimal;
/// Provides the price of this line item as a monetary amount.
fn price(&self) -> MonetaryAmount;
/// Provides the category of this line item, e.g., tax or accounting classification.
fn category(&self) -> TxCat;
/// Computes the total transaction amount for this item (e.g., quantity * price).
fn tx_amount(&self) -> MonetaryAmount;
/// Returns an optional reference to the underlying transaction.
fn tx(&self) -> Option<&Transaction>;
/// Identifies the tag to associate with this line item.
fn tag() -> &'static str;
}
This trait allows each implementing type to define relevant financial or tax-oriented fields while maintaining a consistent interface. The generic parameter <TxCat>
is any type implementing TransactionCategory
with static lifetime constraints.
Integration
To use tktax-line-item in your Cargo project, add the following to your Cargo.toml
:
[dependencies]
tktax-line-item = "0.1"
Then import and implement the trait:
use tktax_line_item::LineItem;
use tktax_transaction_category::TransactionCategory;
use tktax_money::{Decimal, MonetaryAmount};
use tktax_transaction::Transaction;
#[derive(Debug)]
struct ExampleCategory; // Suppose this implements TransactionCategory
#[derive(Debug)]
struct ExampleLineItem {
// Provide any domain-specific fields as needed
}
impl TransactionCategory for ExampleCategory {
fn identifier(&self) -> &str {
"EXAMPLE_CATEGORY"
}
// ...additional required methods...
}
impl LineItem<ExampleCategory> for ExampleLineItem {
fn quantity(&self) -> Decimal {
// ...
}
fn price(&self) -> MonetaryAmount {
// ...
}
fn category(&self) -> ExampleCategory {
ExampleCategory
}
fn tx_amount(&self) -> MonetaryAmount {
// ...
}
fn tx(&self) -> Option<&Transaction> {
// ...
}
fn tag() -> &'static str {
"example_line_item"
}
}
Features
- Trait-Based Abstraction: Simplifies working with multiple line-item implementations under a shared trait.
- Price & Quantity Modeling: Relies on
Decimal
(for precision) andMonetaryAmount
(for currency amounts). - Transaction Linkage: Optionally associates each line item with a
Transaction
record. - Categorization: Leverages
TransactionCategory
to provide flexible classification of each line item for auditing or data aggregation.
Contributing
Contributions are welcome. Please submit issues and pull requests on GitHub.
License
Licensed under either of
at your option.
Dependencies
~26–37MB
~643K SLoC