Skip to main content

ftui_i18n/format/
error.rs

1#![forbid(unsafe_code)]
2
3//! Error types for locale-aware formatting.
4
5use std::fmt;
6
7/// Errors that can occur during locale-aware formatting.
8#[derive(Debug, Clone, PartialEq)]
9pub enum FormattingError {
10    /// The requested locale is not supported and no suitable fallback was found.
11    UnsupportedLocale(String),
12    /// A non-finite floating point value (NaN or infinity) was encountered.
13    NonFinite(f64),
14    /// A date or time component was invalid.
15    InvalidDateTime(DateTimeError),
16    /// An invalid formatting pattern or configuration was provided.
17    InvalidFormat(String),
18    /// A numeric value is out of range for the requested format.
19    OutOfRange(String),
20}
21
22impl fmt::Display for FormattingError {
23    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
24        match self {
25            Self::UnsupportedLocale(loc) => write!(f, "unsupported locale: '{loc}'"),
26            Self::NonFinite(v) => write!(f, "cannot format non-finite float: {v}"),
27            Self::InvalidDateTime(e) => write!(f, "invalid date/time: {e}"),
28            Self::InvalidFormat(msg) => write!(f, "invalid format: {msg}"),
29            Self::OutOfRange(msg) => write!(f, "value out of range: {msg}"),
30        }
31    }
32}
33
34impl std::error::Error for FormattingError {
35    fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
36        match self {
37            Self::InvalidDateTime(e) => Some(e),
38            _ => None,
39        }
40    }
41}
42
43impl From<DateTimeError> for FormattingError {
44    fn from(err: DateTimeError) -> Self {
45        Self::InvalidDateTime(err)
46    }
47}
48
49/// Errors in date or time construction and validation.
50#[derive(Debug, Clone, PartialEq, Eq)]
51pub enum DateTimeError {
52    /// Month is not in the range 1..=12.
53    InvalidMonth(u8),
54    /// Day is not valid for the given month and year.
55    InvalidDay {
56        year: i32,
57        month: u8,
58        day: u8,
59        max_days: u8,
60    },
61    /// Hour is not in the range 0..=23.
62    InvalidHour(u8),
63    /// Minute is not in the range 0..=59.
64    InvalidMinute(u8),
65    /// Second is not in the range 0..=59.
66    InvalidSecond(u8),
67    /// Millisecond is not in the range 0..=999.
68    InvalidMillisecond(u16),
69    /// Timezone offset in minutes is not in the range -720..=840 (-12h..+14h).
70    InvalidTimezoneOffset(i16),
71}
72
73impl fmt::Display for DateTimeError {
74    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75        match self {
76            Self::InvalidMonth(m) => write!(f, "month {m} out of range (1..=12)"),
77            Self::InvalidDay {
78                year,
79                month,
80                day,
81                max_days,
82            } => write!(
83                f,
84                "day {day} out of range for {year:04}-{month:02} (max {max_days})"
85            ),
86            Self::InvalidHour(h) => write!(f, "hour {h} out of range (0..=23)"),
87            Self::InvalidMinute(m) => write!(f, "minute {m} out of range (0..=59)"),
88            Self::InvalidSecond(s) => write!(f, "second {s} out of range (0..=59)"),
89            Self::InvalidMillisecond(ms) => {
90                write!(f, "millisecond {ms} out of range (0..=999)")
91            }
92            Self::InvalidTimezoneOffset(offset) => {
93                write!(f, "timezone offset {offset}m out of range (-720..=840)")
94            }
95        }
96    }
97}
98
99impl std::error::Error for DateTimeError {}