1use core::fmt::{Display, Formatter};
23use core::num::{ParseFloatError, ParseIntError};
24
25pub use alloc::string::{String, ToString};
26use irox_units::bounds::GreaterThanEqualToValueError;
27
28pub mod iso8601;
29pub mod rfc3339;
30
31pub trait Format<T> {
34 fn format(&self, date: &T) -> alloc::string::String;
37}
38
39pub trait FormatParser<T> {
43 fn try_from(&self, data: &str) -> Result<T, FormatError>;
46}
47
48#[derive(Debug, Copy, Clone, Eq, PartialEq)]
51pub enum FormatErrorType {
52 IOError,
53 NumberFormatError,
54 OutOfRangeError,
55 Other,
56}
57
58#[derive(Debug)]
61pub struct FormatError {
62 error_type: FormatErrorType,
63 msg: alloc::string::String,
64}
65
66impl Display for FormatError {
67 fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
68 f.write_fmt(format_args!("{:?}{}", self.error_type, self.msg))
69 }
70}
71
72impl core::error::Error for FormatError {}
73
74impl FormatError {
75 #[must_use]
77 pub fn new(error_type: FormatErrorType, msg: alloc::string::String) -> FormatError {
78 FormatError { error_type, msg }
79 }
80
81 pub fn err<T>(msg: alloc::string::String) -> Result<T, Self> {
83 Err(Self::new(FormatErrorType::Other, msg))
84 }
85
86 pub fn err_str<T>(msg: &'static str) -> Result<T, Self> {
88 Err(Self::new(FormatErrorType::Other, msg.to_string()))
89 }
90}
91
92#[cfg(feature = "std")]
93impl From<std::io::Error> for FormatError {
94 fn from(value: std::io::Error) -> Self {
95 FormatError {
96 error_type: FormatErrorType::IOError,
97 msg: value.to_string(),
98 }
99 }
100}
101impl From<ParseIntError> for FormatError {
102 fn from(value: ParseIntError) -> Self {
103 FormatError {
104 error_type: FormatErrorType::NumberFormatError,
105 msg: value.to_string(),
106 }
107 }
108}
109impl From<ParseFloatError> for FormatError {
110 fn from(value: ParseFloatError) -> Self {
111 FormatError {
112 error_type: FormatErrorType::NumberFormatError,
113 msg: value.to_string(),
114 }
115 }
116}
117impl From<GreaterThanEqualToValueError<u8>> for FormatError {
118 fn from(value: GreaterThanEqualToValueError<u8>) -> Self {
119 FormatError {
120 error_type: FormatErrorType::OutOfRangeError,
121 msg: value.to_string(),
122 }
123 }
124}
125
126impl From<GreaterThanEqualToValueError<u16>> for FormatError {
127 fn from(value: GreaterThanEqualToValueError<u16>) -> Self {
128 FormatError {
129 error_type: FormatErrorType::OutOfRangeError,
130 msg: value.to_string(),
131 }
132 }
133}
134
135impl From<GreaterThanEqualToValueError<f64>> for FormatError {
136 fn from(value: GreaterThanEqualToValueError<f64>) -> Self {
137 FormatError {
138 error_type: FormatErrorType::OutOfRangeError,
139 msg: value.to_string(),
140 }
141 }
142}
143
144impl From<GreaterThanEqualToValueError<u32>> for FormatError {
145 fn from(value: GreaterThanEqualToValueError<u32>) -> Self {
146 FormatError {
147 error_type: FormatErrorType::OutOfRangeError,
148 msg: value.to_string(),
149 }
150 }
151}