use core::fmt::{Display, Formatter};
use core::num::{ParseFloatError, ParseIntError};
pub use alloc::string::{String, ToString};
use irox_units::bounds::GreaterThanEqualToValueError;
pub mod iso8601;
pub mod rfc3339;
pub trait Format<T> {
fn format(&self, date: &T) -> alloc::string::String;
}
pub trait FormatParser<T> {
fn try_from(&self, data: &str) -> Result<T, FormatError>;
}
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum FormatErrorType {
IOError,
NumberFormatError,
OutOfRangeError,
Other,
}
#[derive(Debug)]
pub struct FormatError {
error_type: FormatErrorType,
msg: alloc::string::String,
}
impl Display for FormatError {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
f.write_fmt(format_args!("{:?}{}", self.error_type, self.msg))
}
}
impl core::error::Error for FormatError {}
impl FormatError {
#[must_use]
pub fn new(error_type: FormatErrorType, msg: alloc::string::String) -> FormatError {
FormatError { error_type, msg }
}
pub fn err<T>(msg: alloc::string::String) -> Result<T, Self> {
Err(Self::new(FormatErrorType::Other, msg))
}
pub fn err_str<T>(msg: &'static str) -> Result<T, Self> {
Err(Self::new(FormatErrorType::Other, msg.to_string()))
}
}
#[cfg(feature = "std")]
impl From<std::io::Error> for FormatError {
fn from(value: std::io::Error) -> Self {
FormatError {
error_type: FormatErrorType::IOError,
msg: value.to_string(),
}
}
}
impl From<ParseIntError> for FormatError {
fn from(value: ParseIntError) -> Self {
FormatError {
error_type: FormatErrorType::NumberFormatError,
msg: value.to_string(),
}
}
}
impl From<ParseFloatError> for FormatError {
fn from(value: ParseFloatError) -> Self {
FormatError {
error_type: FormatErrorType::NumberFormatError,
msg: value.to_string(),
}
}
}
impl From<GreaterThanEqualToValueError<u8>> for FormatError {
fn from(value: GreaterThanEqualToValueError<u8>) -> Self {
FormatError {
error_type: FormatErrorType::OutOfRangeError,
msg: value.to_string(),
}
}
}
impl From<GreaterThanEqualToValueError<u16>> for FormatError {
fn from(value: GreaterThanEqualToValueError<u16>) -> Self {
FormatError {
error_type: FormatErrorType::OutOfRangeError,
msg: value.to_string(),
}
}
}
impl From<GreaterThanEqualToValueError<f64>> for FormatError {
fn from(value: GreaterThanEqualToValueError<f64>) -> Self {
FormatError {
error_type: FormatErrorType::OutOfRangeError,
msg: value.to_string(),
}
}
}
impl From<GreaterThanEqualToValueError<u32>> for FormatError {
fn from(value: GreaterThanEqualToValueError<u32>) -> Self {
FormatError {
error_type: FormatErrorType::OutOfRangeError,
msg: value.to_string(),
}
}
}