twilight_model/util/datetime/
error.rsuse std::{
error::Error,
fmt::{Display, Formatter, Result as FmtResult},
};
use time::error::{ComponentRange as ComponentRangeError, Parse as ParseError};
#[derive(Debug)]
pub struct TimestampParseError {
kind: TimestampParseErrorType,
source: Option<Box<dyn Error + Send + Sync>>,
}
impl TimestampParseError {
pub(super) const FORMAT: TimestampParseError = TimestampParseError {
kind: TimestampParseErrorType::Format,
source: None,
};
#[must_use = "retrieving the type has no effect if left unused"]
pub const fn kind(&self) -> &TimestampParseErrorType {
&self.kind
}
#[allow(clippy::unused_self)]
#[must_use = "consuming the error and retrieving the source has no effect if left unused"]
pub fn into_source(self) -> Option<Box<dyn Error + Send + Sync>> {
self.source
}
#[must_use = "consuming the error into its parts has no effect if left unused"]
pub fn into_parts(
self,
) -> (
TimestampParseErrorType,
Option<Box<dyn Error + Send + Sync>>,
) {
(self.kind, self.source)
}
pub(super) fn from_component_range(source: ComponentRangeError) -> Self {
Self {
kind: TimestampParseErrorType::Range,
source: Some(Box::new(source)),
}
}
pub(super) fn from_parse(source: ParseError) -> Self {
Self {
kind: TimestampParseErrorType::Parsing,
source: Some(Box::new(source)),
}
}
}
impl Display for TimestampParseError {
fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
match &self.kind {
TimestampParseErrorType::Format => {
f.write_str("provided value is not in an iso 8601 format")
}
TimestampParseErrorType::Parsing => f.write_str("timestamp parsing failed"),
TimestampParseErrorType::Range => {
f.write_str("value of a field is not in an acceptable range")
}
}
}
}
impl Error for TimestampParseError {}
#[derive(Debug)]
pub enum TimestampParseErrorType {
Format,
Parsing,
Range,
}
#[cfg(test)]
mod tests {
use super::{TimestampParseError, TimestampParseErrorType};
use static_assertions::assert_impl_all;
use std::{error::Error, fmt::Debug};
assert_impl_all!(TimestampParseErrorType: Debug, Send, Sync);
assert_impl_all!(TimestampParseError: Error, Send, Sync);
}