Money

Struct Money 

Source
pub struct Money(/* private fields */);
Expand description

Representation of the Postgres ‘money’ type

Implementations§

Source§

impl Money

Source

pub fn parse_str(input: &str) -> Result<Money, Error>

Attempt to parse a &str into a Money.

NOTE: as of this writing, only the Postgres en_US.UTF-8 locale is supported.

For more information about the Postgres money type, please see 8.2. Monetary Types.

For more about how locales work with monetary values, please see lc_monetary.

§Examples

Parse a string of dollars

use postgres_money::Money;
let dollars = "324023040222";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$324023040222.00", money.to_string());

Parse a string of cents

use postgres_money::Money;
let dollars = ".32";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$0.32", money.to_string());

Parse a string with dollars and cents

use postgres_money::Money;
let dollars = "93.32";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$93.32", money.to_string());

Handles parentheses

use postgres_money::Money;
let dollars = "(93.32)";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("-$93.32", money.to_string());

Handles dollar symbols

use postgres_money::Money;
let dollars = "$93.32";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$93.32", money.to_string());

Rounds correctly

use postgres_money::Money;
let dollars = "$123.454";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$123.45", money.to_string());

let dollars = "$123.455";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$123.46", money.to_string());

Handles commas

use postgres_money::Money;
let dollars = "$123,456.78";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$123456.78", money.to_string());

Max value

use postgres_money::Money;
let dollars = "92233720368547758.07";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("$92233720368547758.07", money.to_string());
assert_eq!(Money::max().to_string(), money.to_string());

Min value

use postgres_money::Money;
let dollars = "-92233720368547758.08";
let money = Money::parse_str(dollars).unwrap();

assert_eq!("-$92233720368547758.08", money.to_string());
assert_eq!(Money::min().to_string(), money.to_string());
Source

pub fn from(cents: i64) -> Money

Construct a Money instance from an i64

§Examples
use postgres_money::Money;
let cents = 324023040222;
let money = Money::from(cents);

assert_eq!("$3240230402.22", money.to_string());
Source§

impl Money

Source

pub const fn min() -> Money

Minimum allowable value for Money

Source

pub const fn max() -> Money

Maximum allowable value for Money

Source

pub const fn none() -> Money

Instantiate Money as zero

Source

pub const fn inner(&self) -> i64

Expose the wrapped i64 value

Trait Implementations§

Source§

impl Add for Money

Source§

type Output = Money

The resulting type after applying the + operator.
Source§

fn add(self, rhs: Money) -> Self::Output

Performs the + operation. Read more
Source§

impl Clone for Money

Source§

fn clone(&self) -> Money

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 Money

Source§

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

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

impl Default for Money

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl<'de> Deserialize<'de> for Money

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 Money

Source§

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

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

impl Div<f32> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<f64> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: f64) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<i16> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i16) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<i32> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<i64> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i64) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<i8> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: i8) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<u16> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u16) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<u32> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u32) -> Self::Output

Performs the / operation. Read more
Source§

impl Div<u8> for Money

Source§

type Output = Money

The resulting type after applying the / operator.
Source§

fn div(self, rhs: u8) -> Self::Output

Performs the / operation. Read more
Source§

impl<'a> FromSql<'a> for Money

Source§

fn from_sql(_: &Type, buf: &[u8]) -> Result<Money, Box<dyn Error + Sync + Send>>

Creates a new value of this type from a buffer of data of the specified Postgres Type in its binary format. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be created from the specified Postgres Type.
Source§

fn from_sql_null(ty: &Type) -> Result<Self, Box<dyn Error + Sync + Send>>

Creates a new value of this type from a NULL SQL value. Read more
Source§

fn from_sql_nullable( ty: &Type, raw: Option<&'a [u8]>, ) -> Result<Self, Box<dyn Error + Sync + Send>>

A convenience function that delegates to from_sql and from_sql_null depending on the value of raw.
Source§

impl FromStr for Money

Source§

type Err = Error

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

fn from_str(money_str: &str) -> Result<Self, Self::Err>

Parses a string s to return a value of this type. Read more
Source§

impl Hash for Money

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl Mul<Money> for f32

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for f64

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for i16

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for i32

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for i64

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for i8

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for u16

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for u32

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<Money> for u8

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: Money) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f32> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<f64> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: f64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<i16> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i16) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<i32> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<i64> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i64) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<i8> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: i8) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<u16> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u16) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<u32> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u32) -> Self::Output

Performs the * operation. Read more
Source§

impl Mul<u8> for Money

Source§

type Output = Money

The resulting type after applying the * operator.
Source§

fn mul(self, rhs: u8) -> Self::Output

Performs the * operation. Read more
Source§

impl Ord for Money

Source§

fn cmp(&self, other: &Money) -> 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 Money

Source§

fn eq(&self, other: &Money) -> 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 Money

Source§

fn partial_cmp(&self, other: &Money) -> 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 Money

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 Sub for Money

Source§

type Output = Money

The resulting type after applying the - operator.
Source§

fn sub(self, rhs: Money) -> Self::Output

Performs the - operation. Read more
Source§

impl ToSql for Money

Source§

fn to_sql( &self, _: &Type, w: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

Converts the value of self into the binary format of the specified Postgres Type, appending it to out. Read more
Source§

fn accepts(ty: &Type) -> bool

Determines if a value of this type can be converted to the specified Postgres Type.
Source§

fn to_sql_checked( &self, ty: &Type, out: &mut BytesMut, ) -> Result<IsNull, Box<dyn Error + Sync + Send>>

An adaptor method used internally by Rust-Postgres. Read more
Source§

fn encode_format(&self, _ty: &Type) -> Format

Specify the encode format
Source§

impl Copy for Money

Source§

impl Eq for Money

Source§

impl StructuralPartialEq for Money

Auto Trait Implementations§

§

impl Freeze for Money

§

impl RefUnwindSafe for Money

§

impl Send for Money

§

impl Sync for Money

§

impl Unpin for Money

§

impl UnwindSafe for Money

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> BorrowToSql for T
where T: ToSql,

Source§

fn borrow_to_sql(&self) -> &dyn ToSql

Returns a reference to self as a ToSql trait object.
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> Same for T

Source§

type Output = T

Should always be Self
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<V, T> VZip<V> for T
where V: MultiLane<T>,

Source§

fn vzip(self) -> V

Source§

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

Source§

impl<T> FromSqlOwned for T
where T: for<'a> FromSql<'a>,