use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum IztroError {
InvalidDate(String),
InvalidTimeIndex(u8),
Internal(String),
}
impl IztroError {
pub fn code(&self) -> &'static str {
match self {
Self::InvalidDate(_) => "invalid_date",
Self::InvalidTimeIndex(_) => "invalid_time_index",
Self::Internal(_) => "internal",
}
}
}
impl fmt::Display for IztroError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::InvalidDate(msg) => f.write_str(msg),
Self::InvalidTimeIndex(t) => write!(f, "time_index must be 0-12, got {t}"),
Self::Internal(msg) => write!(f, "internal error: {msg}"),
}
}
}
impl std::error::Error for IztroError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct BridgeError {
pub code: &'static str,
pub message: String,
}
impl BridgeError {
pub fn invalid_argument(message: impl Into<String>) -> Self {
Self {
code: "invalid_argument",
message: message.into(),
}
}
pub fn internal(message: impl Into<String>) -> Self {
Self {
code: "internal",
message: message.into(),
}
}
}
impl From<IztroError> for BridgeError {
fn from(e: IztroError) -> Self {
Self {
code: e.code(),
message: e.to_string(),
}
}
}
impl fmt::Display for BridgeError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.message)
}
}
impl std::error::Error for BridgeError {}