Skip to main content

calandria_sim/script/
error.rs

1//! Ownership-preserving script construction and request failures.
2
3use alloc::vec::Vec;
4use core::{fmt, num::NonZeroUsize};
5
6use calandria::RetainedBytes;
7
8use super::ScriptStep;
9
10/// Why an exact finite script could not retain its supplied steps.
11#[derive(Clone, Copy, Debug, Eq, PartialEq)]
12pub enum ScriptBuildFailure {
13    /// The script contains more steps than its hard count limit.
14    StepCapacity {
15        /// Configured step-count limit.
16        limit: NonZeroUsize,
17        /// Supplied step count.
18        actual: usize,
19    },
20    /// The script contains more response outcomes than its hard count limit.
21    OutcomeCapacity {
22        /// Configured outcome-count limit.
23        limit: NonZeroUsize,
24        /// Supplied outcome count.
25        actual: usize,
26    },
27    /// Response-outcome count accounting overflowed.
28    OutcomeCountOverflow,
29    /// Variable retained-byte accounting overflowed.
30    RetainedByteOverflow,
31    /// The script exceeds its variable retained-byte limit.
32    RetainedByteCapacity {
33        /// Configured retained-byte limit.
34        limit: RetainedBytes,
35        /// Bytes retained by the supplied script.
36        actual: RetainedBytes,
37    },
38}
39
40impl fmt::Display for ScriptBuildFailure {
41    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
42        match self {
43            Self::StepCapacity { limit, actual } => {
44                write!(formatter, "script has {actual} steps but limit is {limit}")
45            }
46            Self::OutcomeCapacity { limit, actual } => {
47                write!(
48                    formatter,
49                    "script has {actual} outcomes but limit is {limit}"
50                )
51            }
52            Self::OutcomeCountOverflow => {
53                formatter.write_str("script outcome-count accounting overflowed")
54            }
55            Self::RetainedByteOverflow => {
56                formatter.write_str("script retained-byte accounting overflowed")
57            }
58            Self::RetainedByteCapacity { limit, actual } => write!(
59                formatter,
60                "script retains {} variable bytes but limit is {}",
61                actual.get(),
62                limit.get()
63            ),
64        }
65    }
66}
67
68impl core::error::Error for ScriptBuildFailure {}
69
70/// Construction failure retaining ownership of every supplied script step.
71#[derive(Debug)]
72pub struct ScriptBuildError<Q, R> {
73    steps: Vec<ScriptStep<Q, R>>,
74    failure: ScriptBuildFailure,
75}
76
77impl<Q, R> ScriptBuildError<Q, R> {
78    pub(super) const fn new(steps: Vec<ScriptStep<Q, R>>, failure: ScriptBuildFailure) -> Self {
79        Self { steps, failure }
80    }
81
82    /// Returns the construction failure.
83    pub const fn failure(&self) -> ScriptBuildFailure {
84        self.failure
85    }
86
87    /// Returns ownership of every rejected step in original order.
88    pub fn into_steps(self) -> Vec<ScriptStep<Q, R>> {
89        self.steps
90    }
91}
92
93impl<Q, R> fmt::Display for ScriptBuildError<Q, R> {
94    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
95        self.failure.fmt(formatter)
96    }
97}
98
99impl<Q: fmt::Debug, R: fmt::Debug> core::error::Error for ScriptBuildError<Q, R> {}
100
101/// Why an exact scripted request did not produce a response plan.
102#[derive(Clone, Copy, Debug, Eq, PartialEq)]
103pub enum ScriptFailure {
104    /// No scripted request remains.
105    Exhausted,
106    /// The request did not match the next exact expected value.
107    Mismatch,
108}
109
110impl fmt::Display for ScriptFailure {
111    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
112        match self {
113            Self::Exhausted => formatter.write_str("script is exhausted"),
114            Self::Mismatch => formatter.write_str("request does not match the next script step"),
115        }
116    }
117}
118
119impl core::error::Error for ScriptFailure {}