Struct Commodity

Source
pub struct Commodity {
    pub value: Decimal,
    pub type_id: CommodityTypeID,
}
Expand description

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§

Source§

impl Commodity

Source

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

pub fn zero(type_id: CommodityTypeID) -> Commodity

Create a commodity with a value of zero

Source

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

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

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

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

pub fn divide_share(&self, i: i64, dp: u32) -> Vec<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 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);
Source

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

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

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

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

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

pub fn default_epsilon() -> Decimal

The default epsilon to use for comparisons between different Commoditys.

Source

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

Trait Implementations§

Source§

impl Clone for Commodity

Source§

fn clone(&self) -> Commodity

Returns a duplicate of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Commodity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for Commodity

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for Commodity

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl FromStr for Commodity

Source§

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

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

type Err = CommodityError

The associated error which can be returned from parsing.
Source§

impl Ord for Commodity

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

impl PartialEq for Commodity

Source§

fn eq(&self, other: &Commodity) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl PartialOrd for Commodity

Source§

fn partial_cmp(&self, other: &Commodity) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · Source§

fn lt(&self, other: &Rhs) -> bool

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · Source§

fn le(&self, other: &Rhs) -> bool

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · Source§

fn gt(&self, other: &Rhs) -> bool

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · Source§

fn ge(&self, other: &Rhs) -> bool

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl Serialize for Commodity

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Copy for Commodity

Source§

impl Eq for Commodity

Source§

impl StructuralPartialEq for Commodity

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

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