pub struct Commodity {
pub value: Decimal,
pub type_id: CommodityTypeID,
}Expand description
A commodity, which holds a value with an associated CommodityType
Fields§
§value: DecimalThe value of this commodity
type_id: CommodityTypeIDThe id of the type of this commodity
Implementations§
Source§impl Commodity
impl Commodity
Sourcepub fn new<T: Into<CommodityTypeID>>(value: Decimal, type_id: T) -> Commodity
pub fn new<T: Into<CommodityTypeID>>(value: Decimal, type_id: T) -> Commodity
Create a new Commodity.
§Example
use commodity::{Commodity, CommodityTypeID};
use std::str::FromStr;
use rust_decimal::Decimal;
let type_id = CommodityTypeID::from_str("USD").unwrap();
let commodity = Commodity::new(Decimal::new(202, 2), type_id);
assert_eq!(Decimal::from_str("2.02").unwrap(), commodity.value);
assert_eq!(type_id, commodity.type_id)Using using the Into trait to accept CommodityType as the type_id:
use commodity::{Commodity, CommodityType};
use std::str::FromStr;
use rust_decimal::Decimal;
let commodity_type = CommodityType::from_currency_alpha3("USD").unwrap();
let commodity = Commodity::new(Decimal::new(202, 2), &commodity_type);Sourcepub fn zero(type_id: CommodityTypeID) -> Commodity
pub fn zero(type_id: CommodityTypeID) -> Commodity
Create a commodity with a value of zero
Sourcepub fn add(&self, other: &Commodity) -> Result<Commodity, CommodityError>
pub fn add(&self, other: &Commodity) -> Result<Commodity, CommodityError>
Add the value of commodity other to self
such that result = self + other.
§Example
use rust_decimal::Decimal;
use std::str::FromStr;
let type_id = CommodityTypeID::from_str("USD").unwrap();
let commodity1 = Commodity::new(Decimal::new(400, 2), type_id);
let commodity2 = Commodity::new(Decimal::new(250, 2), type_id);
// perform the add
let result = commodity1.add(&commodity2).unwrap();
assert_eq!(Decimal::new(650, 2), result.value);
assert_eq!(type_id, result.type_id);Sourcepub fn sub(&self, other: &Commodity) -> Result<Commodity, CommodityError>
pub fn sub(&self, other: &Commodity) -> Result<Commodity, CommodityError>
Subtract the value of commodity other from self
such that result = self - other.
§Example
use rust_decimal::Decimal;
use std::str::FromStr;
let usd = CommodityTypeID::from_str("USD").unwrap();
let commodity1 = Commodity::new(Decimal::new(400, 2), usd);
let commodity2 = Commodity::new(Decimal::new(250, 2), usd);
// perform the subtraction
let result = commodity1.sub(&commodity2).unwrap();
assert_eq!(Decimal::new(150, 2), result.value);
assert_eq!(usd, result.type_id);Sourcepub fn neg(&self) -> Commodity
pub fn neg(&self) -> Commodity
Negate the value of this commodity such that result = -self
§Example
use rust_decimal::Decimal;
let type_id = CommodityTypeID::from_str("USD").unwrap();
let commodity = Commodity::new(Decimal::new(202, 2), type_id);
// perform the negation
let result = commodity.neg();
assert_eq!(Decimal::from_str("-2.02").unwrap(), result.value);
assert_eq!(type_id, result.type_id)Sourcepub fn div_i64(&self, i: i64) -> Commodity
pub fn div_i64(&self, i: i64) -> Commodity
Divide this commodity by the specified integer value
§Example
use rust_decimal::{Decimal};
use std::str::FromStr;
let commodity = Commodity::from_str("4.03 AUD").unwrap();
let result = commodity.div_i64(4);
assert_eq!(Decimal::new(10075, 4), result.value);Divide this commodity by the specified integer value
§Example
use rust_decimal::{Decimal};
use std::str::FromStr;
let commodity = Commodity::from_str("4.03 AUD").unwrap();
let results = commodity.divide_share(4, 2);
assert_eq!(Decimal::new(101, 2), results.get(0).unwrap().value);
assert_eq!(Decimal::new(101, 2), results.get(1).unwrap().value);
assert_eq!(Decimal::new(101, 2), results.get(2).unwrap().value);
assert_eq!(Decimal::new(100, 2), results.get(3).unwrap().value);Sourcepub fn convert(&self, type_id: CommodityTypeID, rate: Decimal) -> Commodity
pub fn convert(&self, type_id: CommodityTypeID, rate: Decimal) -> Commodity
Convert this commodity to a different commodity_type using a conversion rate.
§Example
use rust_decimal::Decimal;
use std::str::FromStr;
let aud = Commodity::from_str("100.00 AUD").unwrap();
let usd = aud.convert(CommodityTypeID::from_str("USD").unwrap(), Decimal::from_str("0.01").unwrap());
assert_eq!(Decimal::from_str("1.00").unwrap(), usd.value);
assert_eq!("USD", usd.type_id);Sourcepub fn compatible_with(&self, other: &Commodity) -> bool
pub fn compatible_with(&self, other: &Commodity) -> bool
Returns true if the currencies of both this commodity, and
the other commodity are compatible for numeric operations.
§Example
use std::str::FromStr;
let aud1 = Commodity::from_str("1.0 AUD").unwrap();
let aud2 = Commodity::from_str("2.0 AUD").unwrap();
let nzd = Commodity::from_str("1.0 NZD").unwrap();
assert!(aud1.compatible_with(&aud2));
assert!(!aud1.compatible_with(&nzd));Sourcepub fn lt(&self, other: &Commodity) -> Result<bool, CommodityError>
pub fn lt(&self, other: &Commodity) -> Result<bool, CommodityError>
Compare whether this commodity has a value less than another commodity.
Will return an error if the commodities have incompatible currencies.
§Example
use std::str::FromStr;
let aud1 = Commodity::from_str("1.0 AUD").unwrap();
let aud2 = Commodity::from_str("2.0 AUD").unwrap();
assert_eq!(true, aud1.lt(&aud2).unwrap());
assert_eq!(false, aud2.lt(&aud1).unwrap());Sourcepub fn gt(&self, other: &Commodity) -> Result<bool, CommodityError>
pub fn gt(&self, other: &Commodity) -> Result<bool, CommodityError>
Compare whether this commodity has a value greater than another commodity.
Will return an error if the commodities have incompatible currencies.
§Example
use std::str::FromStr;
let aud1 = Commodity::from_str("1.0 AUD").unwrap();
let aud2 = Commodity::from_str("2.0 AUD").unwrap();
assert_eq!(false, aud1.gt(&aud2).unwrap());
assert_eq!(true, aud2.gt(&aud1).unwrap());Sourcepub fn abs(&self) -> Commodity
pub fn abs(&self) -> Commodity
Return the absolute value of this commodity (if the value is negative, then make it positive).
§Example
use std::str::FromStr;
let aud1 = Commodity::from_str("-1.0 AUD").unwrap();
assert_eq!(Commodity::from_str("1.0 AUD").unwrap(), aud1.abs());
let aud2 = Commodity::from_str("2.0 AUD").unwrap();
assert_eq!(Commodity::from_str("2.0 AUD").unwrap(), aud2.abs());Sourcepub fn default_epsilon() -> Decimal
pub fn default_epsilon() -> Decimal
The default epsilon to use for comparisons between different Commoditys.