inference_engine/shacl12/
types.rs1use std::{error::Error, fmt, num::NonZeroU64};
2
3#[derive(Clone, Copy, Debug)]
6pub struct Limits {
7 pub(super) iterations: u32,
8 pub(super) work: NonZeroU64,
9 pub(super) inferred: NonZeroU64,
10}
11impl Limits {
12 pub const fn new(iterations: u32, work: NonZeroU64, inferred: NonZeroU64) -> Self {
15 Self {
16 iterations,
17 work,
18 inferred,
19 }
20 }
21}
22#[derive(Default, Debug, Eq, PartialEq)]
24pub struct Report {
25 pub iterations: u64,
27 pub phases: u64,
29 pub executions: u64,
31 pub inferred: u64,
33 pub work: u64,
35}
36#[derive(Debug)]
38pub struct Work {
39 used: u64,
40 maximum: u64,
41 exceeded: bool,
42}
43impl Work {
44 pub(super) const fn new(maximum: NonZeroU64) -> Self {
45 Self {
46 used: 0,
47 maximum: maximum.get(),
48 exceeded: false,
49 }
50 }
51 pub fn charge(&mut self) -> Result<(), WorkExceeded> {
55 if self.exceeded || self.used == self.maximum {
56 self.exceeded = true;
57 return Err(WorkExceeded);
58 }
59 self.used += 1;
60 Ok(())
61 }
62 pub const fn used(&self) -> u64 {
64 self.used
65 }
66 pub(super) const fn exceeded(&self) -> bool {
67 self.exceeded
68 }
69}
70#[derive(Debug)]
72pub struct WorkExceeded;
73impl fmt::Display for WorkExceeded {
74 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
75 f.write_str("SHACL work limit exceeded")
76 }
77}
78impl Error for WorkExceeded {}
79#[derive(Debug, Clone, Copy, Eq, PartialEq)]
81pub enum Limit {
82 Iterations,
84 Work,
86 Inferred,
88}
89#[derive(Debug)]
91pub enum ExecutionError<E> {
92 Backend(E),
94 Limit(Limit),
96 Cancelled,
98}
99impl<E: fmt::Display> fmt::Display for ExecutionError<E> {
100 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
101 match self {
102 Self::Backend(error) => write!(f, "SHACL backend failed: {error}"),
103 Self::Limit(limit) => write!(f, "SHACL {limit:?} limit exceeded"),
104 Self::Cancelled => f.write_str("SHACL attempt cancelled"),
105 }
106 }
107}
108impl<E: Error + 'static> Error for ExecutionError<E> {
109 fn source(&self) -> Option<&(dyn Error + 'static)> {
110 if let Self::Backend(error) = self {
111 Some(error)
112 } else {
113 None
114 }
115 }
116}