#![forbid(unsafe_code)]
pub mod check;
mod doc;
pub mod exec;
mod executor;
pub use doc::*;
pub use executor::{BoxFuture, Executor, Swept};
#[derive(Clone, Debug, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Invalid(String),
SpaceNotFound,
SchemaConflict {
current: Option<i64>,
},
RequestIdConflict,
RequestExpired,
Contention,
NotOwner,
RetryLater {
after_ms: u64,
},
Unavailable(String),
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
Error::Invalid(m) => write!(f, "invalid: {m}"),
Error::SpaceNotFound => write!(f, "space not found"),
Error::SchemaConflict { current } => {
write!(f, "schema conflict (current version {current:?})")
}
Error::RequestIdConflict => write!(f, "request id reused with a different program"),
Error::RequestExpired => write!(f, "request expired"),
Error::Contention => write!(f, "contention"),
Error::NotOwner => write!(f, "not owner: this process does not hold the space"),
Error::RetryLater { after_ms } => write!(f, "retry later: after {after_ms} ms"),
Error::Unavailable(m) => write!(f, "unavailable: {m}"),
}
}
}
impl std::error::Error for Error {}
impl Error {
pub fn is_superseded(&self) -> bool {
matches!(self, Error::NotOwner)
}
pub fn is_busy(&self) -> bool {
matches!(self, Error::NotOwner)
}
pub fn is_rate_limited(&self) -> bool {
matches!(self, Error::RetryLater { .. })
}
pub fn is_retryable(&self) -> bool {
matches!(
self,
Error::Contention | Error::RetryLater { .. } | Error::Unavailable(_)
)
}
}
pub type Result<T> = std::result::Result<T, Error>;
pub(crate) fn invalid<T>(msg: impl Into<String>) -> Result<T> {
Err(Error::Invalid(msg.into()))
}
pub fn from_yaml<T: serde::de::DeserializeOwned>(text: &str) -> Result<T> {
let v: serde_json::Value =
serde_norway::from_str(text).map_err(|e| Error::Invalid(e.to_string()))?;
serde_json::from_value(v).map_err(|e| Error::Invalid(e.to_string()))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn errors_classify_the_same_on_every_backend() {
let limited = Error::RetryLater { after_ms: 50 };
let db = Error::Unavailable("08006: connection failure".into());
assert!(Error::NotOwner.is_superseded() && Error::NotOwner.is_busy());
assert!(!Error::NotOwner.is_retryable());
assert!(limited.is_rate_limited() && limited.is_retryable());
assert!(db.is_retryable() && !db.is_superseded() && !db.is_rate_limited());
assert!(Error::Contention.is_retryable());
for e in [
Error::Invalid("x".into()),
Error::SpaceNotFound,
Error::SchemaConflict { current: None },
Error::RequestIdConflict,
Error::RequestExpired,
] {
assert!(!e.is_retryable(), "{e}");
}
}
}