Skip to main content

hmrc_rates/
error.rs

1use alloc::boxed::Box;
2
3use crate::types::{Currency, Period, RateType};
4
5/// Why a rate lookup failed.
6#[derive(Debug, Clone, PartialEq, Eq, thiserror::Error)]
7#[non_exhaustive]
8pub enum LookupError {
9    /// The code never appears anywhere in this rate series (or is not a valid code).
10    #[error("currency '{code}' is not published in HMRC {table} rates")]
11    UnknownCurrency { code: Box<str>, table: RateType },
12
13    /// The series has no table for this period; `available` gives the loaded range.
14    #[error("no HMRC {table} rates for {period}{}", available_range(.available))]
15    PeriodNotAvailable {
16        table: RateType,
17        period: Period,
18        available: Option<(Period, Period)>,
19    },
20
21    /// The period exists but this currency is absent from it.
22    #[error("'{currency}' has no HMRC {table} rate for {period}")]
23    NotInPeriod {
24        currency: Currency,
25        table: RateType,
26        period: Period,
27    },
28}
29
30fn available_range(available: &Option<(Period, Period)>) -> alloc::string::String {
31    use alloc::format;
32    match available {
33        Some((first, last)) => format!(" (available {first} to {last})"),
34        None => alloc::string::String::from(" (no data loaded)"),
35    }
36}