Skip to main content

Rates

Struct Rates 

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

All HMRC rate tables: bundled data plus (with the http feature) fetched periods.

Send + Sync: cloning is cheap, bundled data is shared statics.

Start with Rates::new.

Implementations§

Source§

impl Rates

Source

pub fn new() -> Rates

The bundled dataset. Infallible and effectively free. The tables live in the binary’s read-only data, nothing is parsed or allocated.

§Examples
use hmrc_rates::Rates;

let rates = Rates::new();
assert!(rates.months().count() > 100);
Source

pub fn monthly_rate( &self, code: &str, year_month: impl Into<YearMonth>, ) -> Result<Rate, LookupError>

The monthly rate for code, strictly for that month.

Accepts anything convertible to YearMonth, including chrono::NaiveDate. "GBP" (any case) returns the identity rate for any month.

§Examples
use hmrc_rates::Rates;
use rust_decimal::Decimal;

let rates = Rates::new();
let date = chrono::NaiveDate::from_ymd_opt(2025, 8, 15).unwrap();
let rate = rates.monthly_rate("USD", date)?;
let gbp = rate.to_gbp(Decimal::from(100));
Source

pub fn monthly_rate_or_earlier( &self, code: &str, year_month: impl Into<YearMonth>, max_months_back: u32, ) -> Result<Rate, LookupError>

Like Rates::monthly_rate, but walks back to the nearest earlier published month, at most max_months_back steps.

This is the crate’s only fallback, and it is opt-in. Rate::period reveals which month was actually used.

§Examples
use hmrc_rates::{Period, Rates};

let rates = Rates::new();
let next = rates.months().next_back().unwrap().next(); // not published yet
assert!(rates.monthly_rate("USD", next).is_err()); // strict lookup fails
let rate = rates.monthly_rate_or_earlier("USD", next, 1)?;
assert_ne!(rate.period(), Period::YearMonth(next)); // the substitution is visible
Source

pub fn monthly( &self, year_month: impl Into<YearMonth>, ) -> Result<Table<'_>, LookupError>

The whole monthly table for one month.

Source

pub fn spot(&self, period: YearEnd) -> Result<Table<'_>, LookupError>

The spot table for a 31 March / 31 December period.

§Examples
use hmrc_rates::{Rates, YearEnd};

let rates = Rates::new();
let usd = rates.spot(YearEnd::december(2024))?.rate("USD")?;
Source

pub fn average(&self, period: YearEnd) -> Result<Table<'_>, LookupError>

The yearly-average table for a 31 March / 31 December period.

§Examples
use hmrc_rates::{Rates, YearEnd};

let rates = Rates::new();
// Self Assessment style: the average for the year to 31 March 2025.
let eur = rates.average(YearEnd::march(2025))?.rate("EUR")?;
Source

pub fn weekly(&self, date: NaiveDate) -> Result<Table<'_>, LookupError>

The weekly-amendment table whose validity range contains date.

Weekly files list only the currencies HMRC amended that week (series ran 2014-01 to 2016-04, then was discontinued).

§Examples
use hmrc_rates::Rates;

let rates = Rates::new();
let date = chrono::NaiveDate::from_ymd_opt(2014, 1, 10).unwrap();
let lira = rates.weekly(date)?.rate("TRY")?;
Source

pub fn months(&self) -> impl DoubleEndedIterator<Item = YearMonth> + use<'_>

All published months, ascending.

Source

pub fn spot_periods(&self) -> impl DoubleEndedIterator<Item = YearEnd> + use<'_>

All published spot periods, ascending.

Source

pub fn average_periods( &self, ) -> impl DoubleEndedIterator<Item = YearEnd> + use<'_>

All published yearly-average periods, ascending.

Source

pub fn weeks(&self) -> impl DoubleEndedIterator<Item = Period> + use<'_>

All weekly-amendment validity ranges, ascending, as Period::Week items.

Source

pub fn currencies( &self, table: RateType, ) -> impl Iterator<Item = Currency> + use<'_>

Every currency that appears anywhere in the given series, ascending.

Trait Implementations§

Source§

impl Clone for Rates

Source§

fn clone(&self) -> Rates

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

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

Performs copy-assignment from source. Read more
Source§

impl Debug for Rates

Source§

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

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

impl Default for Rates

Available on crate feature bundled only.
Source§

fn default() -> Rates

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

Auto Trait Implementations§

§

impl Freeze for Rates

§

impl RefUnwindSafe for Rates

§

impl Send for Rates

§

impl Sync for Rates

§

impl Unpin for Rates

§

impl UnsafeUnpin for Rates

§

impl UnwindSafe for Rates

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, 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.