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
impl CurrencyOps
Sourcepub fn get_rate(
&self,
from: Currency,
to: Currency,
) -> Result<Option<ExchangeRate>>
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");
}Sourcepub fn get_rates_for(&self, base: Currency) -> Result<Vec<ExchangeRate>>
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);
}Sourcepub fn list_rates(
&self,
filter: ExchangeRateFilter,
) -> Result<Vec<ExchangeRate>>
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()
})?;Sourcepub fn set_rate(&self, input: SetExchangeRate) -> Result<ExchangeRate>
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);Sourcepub fn set_rates(
&self,
rates: Vec<SetExchangeRate>,
) -> Result<Vec<ExchangeRate>>
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());Sourcepub fn delete_rate(&self, id: Uuid) -> Result<()>
pub fn delete_rate(&self, id: Uuid) -> Result<()>
Sourcepub fn convert(&self, input: ConvertCurrency) -> Result<ConversionResult>
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);Sourcepub fn convert_amount(
&self,
amount: Decimal,
from: Currency,
to: Currency,
) -> Result<Decimal>
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);Sourcepub fn get_settings(&self) -> Result<StoreCurrencySettings>
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);Sourcepub fn update_settings(
&self,
settings: StoreCurrencySettings,
) -> Result<StoreCurrencySettings>
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);Sourcepub fn set_base_currency(
&self,
currency: Currency,
) -> Result<StoreCurrencySettings>
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)?;Sourcepub fn enable_currencies(
&self,
currencies: Vec<Currency>,
) -> Result<StoreCurrencySettings>
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,
])?;Sourcepub fn is_enabled(&self, currency: Currency) -> Result<bool>
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");
}Sourcepub fn base_currency(&self) -> Result<Currency>
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);