flexible_time/
error.rs

1use std::fmt::Formatter;
2use time::error::{ComponentRange, ParseFromDescription};
3
4impl std::error::Error for Error {}
5impl std::error::Error for ConversionError {}
6impl std::error::Error for ParsingError {}
7impl std::error::Error for ParsedError {}
8
9#[derive(Debug)]
10pub enum ConversionError {
11    OutOfRange(ComponentRange),
12}
13
14impl From<ComponentRange> for ConversionError {
15    fn from(value: ComponentRange) -> Self {
16        Self::OutOfRange(value)
17    }
18}
19
20impl std::fmt::Display for ConversionError {
21    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
22        match self {
23            Self::OutOfRange(err) => write!(f, "out of range: {err}"),
24        }
25    }
26}
27
28#[derive(Copy, Clone, Debug)]
29pub enum ParsingError {
30    /// After parsing the input, information is missing
31    MissingInformation,
32    /// The input was not fully parsed
33    RemainingInformation,
34    /// The input was not fully parsed
35    Format(ParseFromDescription),
36}
37
38impl From<ParseFromDescription> for ParsingError {
39    fn from(value: ParseFromDescription) -> Self {
40        Self::Format(value)
41    }
42}
43
44impl std::fmt::Display for ParsingError {
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        match self {
47            Self::MissingInformation => write!(f, "missing information"),
48            Self::RemainingInformation => write!(f, "remaining input"),
49            Self::Format(err) => write!(f, "format error: {err}"),
50        }
51    }
52}
53
54#[derive(Copy, Clone, Debug)]
55pub enum ParsedError {
56    /// The format is unknown
57    UnknownFormat,
58}
59
60impl std::fmt::Display for ParsedError {
61    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
62        match self {
63            Self::UnknownFormat => write!(f, "unknown format"),
64        }
65    }
66}
67
68#[derive(Debug)]
69pub enum Error {
70    Conversion(ConversionError),
71    // Parsing(ParsingError),
72    Parsed(ParsedError),
73}
74
75impl std::fmt::Display for Error {
76    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
77        match self {
78            //     Self::Parsing(parsing) => write!(f, "parsing error: {parsing}"),
79            Self::Conversion(conversion) => write!(f, "conversion error: {conversion}"),
80            Self::Parsed(parsed) => write!(f, "parse error: {parsed}"),
81        }
82    }
83}
84
85impl From<ConversionError> for Error {
86    fn from(value: ConversionError) -> Self {
87        Self::Conversion(value)
88    }
89}
90
91/*
92impl From<ParsingError> for Error {
93    fn from(value: ParsingError) -> Self {
94        Self::Parsing(value)
95    }
96}*/
97
98impl From<ParsedError> for Error {
99    fn from(value: ParsedError) -> Self {
100        Self::Parsed(value)
101    }
102}