use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq, Hash)]
pub enum ErrorKind {
Timeout,
Remote,
RateLimited,
PermanentFailure,
Deserialization,
Validation,
Domain,
#[serde(other)]
Unknown,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub enum ProcessingStatus {
#[default]
Success,
Error {
message: String,
#[serde(skip_serializing_if = "Option::is_none")]
kind: Option<ErrorKind>,
},
}
impl ProcessingStatus {
pub fn success() -> Self {
ProcessingStatus::Success
}
pub fn error(msg: impl Into<String>) -> Self {
ProcessingStatus::Error {
message: msg.into(),
kind: None,
}
}
pub fn error_with_kind(msg: impl Into<String>, kind: Option<ErrorKind>) -> Self {
ProcessingStatus::Error {
message: msg.into(),
kind,
}
}
pub fn kind(&self) -> Option<&ErrorKind> {
match self {
ProcessingStatus::Error { kind, .. } => kind.as_ref(),
_ => None,
}
}
pub fn is_terminal(&self) -> bool {
matches!(self, Self::Success | Self::Error { .. })
}
pub fn is_success(&self) -> bool {
matches!(self, Self::Success)
}
}