use std::fmt;
pub type Result<T> = std::result::Result<T, StikkError>;
#[derive(Debug)]
#[non_exhaustive]
pub enum StikkError {
Refusal {
message: String,
},
LockConflict {
message: String,
},
NotReady {
detail: String,
},
IntegrityFinding {
message: String,
},
Limits {
message: String,
},
Environment {
detail: String,
source: Option<Box<dyn std::error::Error + Send + Sync + 'static>>,
},
Internal {
detail: String,
},
Stale {
operation: String,
},
Declined {
detail: String,
},
CrossRef {
message: String,
},
}
impl StikkError {
#[must_use]
pub fn environment(
detail: impl Into<String>,
source: impl std::error::Error + Send + Sync + 'static,
) -> Self {
Self::Environment {
detail: detail.into(),
source: Some(Box::new(source)),
}
}
#[must_use]
pub fn environment_msg(detail: impl Into<String>) -> Self {
Self::Environment {
detail: detail.into(),
source: None,
}
}
#[must_use]
pub const fn class(&self) -> &'static str {
match self {
Self::Refusal { .. } => "refusal",
Self::LockConflict { .. } => "lock-conflict",
Self::NotReady { .. } => "not-ready",
Self::IntegrityFinding { .. } => "integrity-finding",
Self::Limits { .. } => "limits",
Self::Environment { .. } => "environment",
Self::Internal { .. } => "stikk-internal",
Self::Stale { .. } => "stale",
Self::Declined { .. } => "declined",
Self::CrossRef { .. } => "cross-ref",
}
}
#[must_use]
pub const fn is_user_resolved(&self) -> bool {
matches!(
self,
Self::Refusal { .. }
| Self::LockConflict { .. }
| Self::NotReady { .. }
| Self::Stale { .. }
| Self::Declined { .. }
| Self::CrossRef { .. }
)
}
}
impl fmt::Display for StikkError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Refusal { message }
| Self::LockConflict { message }
| Self::IntegrityFinding { message }
| Self::Limits { message }
| Self::CrossRef { message } => write!(f, "{}: {message}", self.class()),
Self::NotReady { detail } | Self::Internal { detail } | Self::Declined { detail } => {
write!(f, "{}: {detail}", self.class())
}
Self::Environment { detail, .. } => write!(f, "environment: {detail}"),
Self::Stale { operation } => {
write!(
f,
"stale: {operation}'s preview no longer matches the repository"
)
}
}
}
}
impl std::error::Error for StikkError {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match self {
Self::Environment {
source: Some(cause),
..
} => Some(cause.as_ref()),
_ => None,
}
}
}
#[cfg(test)]
mod tests;