use std::{error::Error, fmt};
use crate::BudgetKind;
#[derive(Clone, Copy, Debug, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct ContinuationToken(u64);
impl ContinuationToken {
#[must_use]
pub const fn new(value: u64) -> Self {
Self(value)
}
#[must_use]
pub const fn get(self) -> u64 {
self.0
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum IncrementalError<K> {
UnknownQuery {
key: K,
},
Cycle {
path: Vec<K>,
},
BudgetExceeded {
kind: BudgetKind,
limit: usize,
consumed: usize,
continuation: Option<ContinuationToken>,
},
Cancelled,
UnknownContinuation {
token: ContinuationToken,
},
}
impl<K> IncrementalError<K> {
#[must_use]
pub fn continuation(&self) -> Option<ContinuationToken> {
match self {
Self::BudgetExceeded { continuation, .. } => *continuation,
_ => None,
}
}
}
impl<K: fmt::Debug> fmt::Display for IncrementalError<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::UnknownQuery { key } => write!(f, "unknown query {key:?}"),
Self::Cycle { path } => write!(f, "incremental query cycle {path:?}"),
Self::BudgetExceeded {
kind,
limit,
consumed,
..
} => write!(
f,
"incremental query budget {kind:?} exhausted at {consumed}/{limit}"
),
Self::Cancelled => f.write_str("incremental query cancelled"),
Self::UnknownContinuation { token } => {
write!(f, "unknown continuation token {}", token.get())
}
}
}
}
impl<K: fmt::Debug> Error for IncrementalError<K> {}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum SnapshotError<K> {
DuplicateNode {
key: K,
},
}
impl<K: fmt::Debug> fmt::Display for SnapshotError<K> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::DuplicateNode { key } => write!(f, "duplicate snapshot node {key:?}"),
}
}
}
impl<K: fmt::Debug> Error for SnapshotError<K> {}