Skip to main content

CurrencyOps

Struct CurrencyOps 

Source
pub struct CurrencyOps { /* private fields */ }
Expand description

Currency operations interface.

Provides exchange rate management and currency conversion for multi-currency commerce operations.

§Example

use stateset_embedded::{Commerce, Currency, ConvertCurrency};
use rust_decimal_macros::dec;

let commerce = Commerce::new("./store.db")?;

// Get exchange rate
if let Some(rate) = commerce.currency().get_rate(Currency::USD, Currency::EUR)? {
    println!("USD to EUR: {}", rate.rate);
}

// Convert currency
let result = commerce.currency().convert(ConvertCurrency {
    from: Currency::USD,
    to: Currency::EUR,
    amount: dec!(100.00),
})?;
println!("$100 USD = €{} EUR", result.converted_amount);

Implementations§

Source§

impl CurrencyOps

Source

pub fn get_rate( &self, from: Currency, to: Currency, ) -> Result<Option<ExchangeRate>>

Get exchange rate between two currencies.

Returns the current rate to convert from the base currency to the quote currency. If no direct rate exists, attempts to find an inverse rate.

§Example
// Get USD to EUR rate
if let Some(rate) = commerce.currency().get_rate(Currency::USD, Currency::EUR)? {
    println!("1 USD = {} EUR", rate.rate);
    println!("Rate source: {}", rate.source);
} else {
    println!("No rate found for USD/EUR");
}
Source

pub fn get_rates_for(&self, base: Currency) -> Result<Vec<ExchangeRate>>

Get all exchange rates for a base currency.

§Example
// Get all rates from USD
let rates = commerce.currency().get_rates_for(Currency::USD)?;
for rate in rates {
    println!("1 USD = {} {}", rate.rate, rate.quote_currency);
}
Source

pub fn list_rates( &self, filter: ExchangeRateFilter, ) -> Result<Vec<ExchangeRate>>

List exchange rates with optional filtering.

§Example
// List all rates
let rates = commerce.currency().list_rates(ExchangeRateFilter::default())?;

// Filter by base currency
let usd_rates = commerce.currency().list_rates(ExchangeRateFilter {
    base_currency: Some(Currency::USD),
    ..Default::default()
})?;
Source

pub fn set_rate(&self, input: SetExchangeRate) -> Result<ExchangeRate>

Set an exchange rate.

Creates or updates the exchange rate between two currencies. The rate is also recorded in history for auditing.

§Example
use rust_decimal_macros::dec;

// Set USD to EUR rate
let rate = commerce.currency().set_rate(SetExchangeRate {
    base_currency: Currency::USD,
    quote_currency: Currency::EUR,
    rate: dec!(0.92),
    source: Some("manual".into()),
})?;

println!("Rate set: 1 USD = {} EUR", rate.rate);
Source

pub fn set_rates( &self, rates: Vec<SetExchangeRate>, ) -> Result<Vec<ExchangeRate>>

Set multiple exchange rates at once.

Useful for bulk updates from external rate providers.

§Example
use rust_decimal_macros::dec;

let rates = commerce.currency().set_rates(vec![
    SetExchangeRate {
        base_currency: Currency::USD,
        quote_currency: Currency::EUR,
        rate: dec!(0.92),
        source: Some("api".into()),
    },
    SetExchangeRate {
        base_currency: Currency::USD,
        quote_currency: Currency::GBP,
        rate: dec!(0.79),
        source: Some("api".into()),
    },
])?;

println!("Updated {} exchange rates", rates.len());
Source

pub fn delete_rate(&self, id: Uuid) -> Result<()>

Delete an exchange rate by ID.

§Example
commerce.currency().delete_rate(rate_id)?;
Source

pub fn convert(&self, input: ConvertCurrency) -> Result<ConversionResult>

Convert an amount from one currency to another.

Uses the current exchange rate to convert the amount. If the rate is not found directly, attempts to use the inverse rate.

§Example
use rust_decimal_macros::dec;

let result = commerce.currency().convert(ConvertCurrency {
    from: Currency::USD,
    to: Currency::EUR,
    amount: dec!(100.00),
})?;

println!("${} USD = €{} EUR", result.original_amount, result.converted_amount);
println!("Rate used: {} (at {})", result.rate, result.rate_at);
Source

pub fn convert_amount( &self, amount: Decimal, from: Currency, to: Currency, ) -> Result<Decimal>

Convert an amount between currencies (convenience method).

§Example
use rust_decimal_macros::dec;

let eur_amount = commerce.currency().convert_amount(
    dec!(100.00),
    Currency::USD,
    Currency::EUR
)?;

println!("€{}", eur_amount);
Source

pub fn get_settings(&self) -> Result<StoreCurrencySettings>

Get store currency settings.

Returns the base currency, enabled currencies, and conversion settings for the store.

§Example
let settings = commerce.currency().get_settings()?;

println!("Base currency: {}", settings.base_currency);
println!("Enabled currencies: {:?}", settings.enabled_currencies);
println!("Auto-convert: {}", settings.auto_convert);
println!("Rounding: {:?}", settings.rounding_mode);
Source

pub fn update_settings( &self, settings: StoreCurrencySettings, ) -> Result<StoreCurrencySettings>

Update store currency settings.

§Example
let settings = commerce.currency().update_settings(StoreCurrencySettings {
    base_currency: Currency::EUR,
    enabled_currencies: vec![Currency::EUR, Currency::USD, Currency::GBP],
    auto_convert: true,
    rounding_mode: RoundingMode::HalfUp,
})?;

println!("Updated base currency to: {}", settings.base_currency);
Source

pub fn set_base_currency( &self, currency: Currency, ) -> Result<StoreCurrencySettings>

Set the store’s base currency.

Convenience method to update just the base currency.

§Example
commerce.currency().set_base_currency(Currency::EUR)?;
Source

pub fn enable_currencies( &self, currencies: Vec<Currency>, ) -> Result<StoreCurrencySettings>

Enable currencies for the store.

§Example
commerce.currency().enable_currencies(vec![
    Currency::USD,
    Currency::EUR,
    Currency::GBP,
    Currency::JPY,
])?;
Source

pub fn is_enabled(&self, currency: Currency) -> Result<bool>

Check if a currency is enabled for the store.

§Example
if commerce.currency().is_enabled(Currency::EUR)? {
    println!("EUR is enabled");
}
Source

pub fn base_currency(&self) -> Result<Currency>

Get the store’s base currency.

§Example
let base = commerce.currency().base_currency()?;
println!("Store base currency: {}", base);
Source

pub fn enabled_currencies(&self) -> Result<Vec<Currency>>

Get all enabled currencies for the store.

§Example
let currencies = commerce.currency().enabled_currencies()?;
for c in currencies {
    println!("Enabled: {} ({})", c.name(), c.code());
}
Source

pub fn format(&self, amount: Decimal, currency: Currency) -> String

Format an amount with currency symbol.

§Example
use rust_decimal_macros::dec;

let formatted = commerce.currency().format(dec!(99.99), Currency::USD);
println!("{}", formatted); // "$99.99"

Trait Implementations§

Source§

impl Debug for CurrencyOps

Source§

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

Formats the value using the given formatter. Read more

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> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
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> PolicyExt for T
where T: ?Sized,

Source§

fn and<P, B, E>(self, other: P) -> And<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow only if self and other return Action::Follow. Read more
Source§

fn or<P, B, E>(self, other: P) -> Or<T, P>
where T: Sized + Policy<B, E>, P: Policy<B, E>,

Create a new Policy that returns Action::Follow if either self or other returns Action::Follow. Read more
Source§

impl<T> Same for T

Source§

type Output = T

Should always be Self
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> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more