use std::{error::Error, fmt, num::NonZeroU64};
#[derive(Clone, Copy, Debug)]
pub struct Limits {
pub(super) iterations: u32,
pub(super) work: NonZeroU64,
pub(super) inferred: NonZeroU64,
}
impl Limits {
pub const fn new(iterations: u32, work: NonZeroU64, inferred: NonZeroU64) -> Self {
Self {
iterations,
work,
inferred,
}
}
}
#[derive(Default, Debug, Eq, PartialEq)]
pub struct Report {
pub iterations: u64,
pub phases: u64,
pub executions: u64,
pub inferred: u64,
pub work: u64,
}
#[derive(Debug)]
pub struct Work {
used: u64,
maximum: u64,
exceeded: bool,
}
impl Work {
pub(super) const fn new(maximum: NonZeroU64) -> Self {
Self {
used: 0,
maximum: maximum.get(),
exceeded: false,
}
}
pub fn charge(&mut self) -> Result<(), WorkExceeded> {
if self.exceeded || self.used == self.maximum {
self.exceeded = true;
return Err(WorkExceeded);
}
self.used += 1;
Ok(())
}
pub const fn used(&self) -> u64 {
self.used
}
pub(super) const fn exceeded(&self) -> bool {
self.exceeded
}
}
#[derive(Debug)]
pub struct WorkExceeded;
impl fmt::Display for WorkExceeded {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("SHACL work limit exceeded")
}
}
impl Error for WorkExceeded {}
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
pub enum Limit {
Iterations,
Work,
Inferred,
}
#[derive(Debug)]
pub enum ExecutionError<E> {
Backend(E),
Limit(Limit),
Cancelled,
}
impl<E: fmt::Display> fmt::Display for ExecutionError<E> {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Backend(error) => write!(f, "SHACL backend failed: {error}"),
Self::Limit(limit) => write!(f, "SHACL {limit:?} limit exceeded"),
Self::Cancelled => f.write_str("SHACL attempt cancelled"),
}
}
}
impl<E: Error + 'static> Error for ExecutionError<E> {
fn source(&self) -> Option<&(dyn Error + 'static)> {
if let Self::Backend(error) = self {
Some(error)
} else {
None
}
}
}