[][src]Struct commodity::Commodity

pub struct Commodity {
    pub value: Decimal,
    pub type_id: CommodityTypeID,
}

A commodity, which holds a value with an associated CommodityType

Fields

value: Decimal

The value of this commodity

type_id: CommodityTypeID

The id of the type of this commodity

Implementations

impl Commodity[src]

pub fn new<T: Into<CommodityTypeID>>(value: Decimal, type_id: T) -> Commodity[src]

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);

pub fn zero(type_id: CommodityTypeID) -> Commodity[src]

Create a commodity with a value of zero

pub fn add(&self, other: &Commodity) -> Result<Commodity, CommodityError>[src]

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);

pub fn sub(&self, other: &Commodity) -> Result<Commodity, CommodityError>[src]

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);

pub fn neg(&self) -> Commodity[src]

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)

pub fn div_i64(&self, i: i64) -> Commodity[src]

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);

pub fn divide_share(&self, i: i64, dp: u32) -> Vec<Commodity>[src]

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);

pub fn convert(&self, type_id: CommodityTypeID, rate: Decimal) -> Commodity[src]

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);

pub fn compatible_with(&self, other: &Commodity) -> bool[src]

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));

pub fn lt(&self, other: &Commodity) -> Result<bool, CommodityError>[src]

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());

pub fn gt(&self, other: &Commodity) -> Result<bool, CommodityError>[src]

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());

pub fn abs(&self) -> Commodity[src]

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());

pub fn default_epsilon() -> Decimal[src]

The default epsilon to use for comparisons between different Commoditys.

pub fn eq_approx(&self, other: Commodity, epsilon: Decimal) -> bool[src]

Trait Implementations

impl Clone for Commodity[src]

impl Copy for Commodity[src]

impl Debug for Commodity[src]

impl<'de> Deserialize<'de> for Commodity[src]

impl Display for Commodity[src]

impl Eq for Commodity[src]

impl FromStr for Commodity[src]

type Err = CommodityError

The associated error which can be returned from parsing.

fn from_str(commodity_string: &str) -> Result<Commodity, CommodityError>[src]

Construct a Commodity from a string

Example

use std::str::FromStr;
use rust_decimal::Decimal;

let commodity = Commodity::from_str("1.234 USD").unwrap();

assert_eq!(Decimal::from_str("1.234").unwrap(), commodity.value);
assert_eq!(CommodityTypeID::from_str("USD").unwrap(), commodity.type_id);

impl Ord for Commodity[src]

impl PartialEq<Commodity> for Commodity[src]

impl PartialOrd<Commodity> for Commodity[src]

impl Serialize for Commodity[src]

impl StructuralEq for Commodity[src]

impl StructuralPartialEq for Commodity[src]

Auto Trait Implementations

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> DeserializeOwned for T where
    T: for<'de> Deserialize<'de>, 
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T> ToOwned for T where
    T: Clone
[src]

type Owned = T

The resulting type after obtaining ownership.

impl<T> ToString for T where
    T: Display + ?Sized
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.