puffin/
error.rs

1use std::{fmt, io};
2
3use crate::algebra::error::FnError;
4use crate::differential::TraceDifference;
5
6#[derive(Debug, Clone, PartialEq, Eq)]
7pub enum Error {
8    /// Returned if a concrete function from the protocol fails or term evaluation fails
9    Fn(FnError),
10    /// Error while evaluating a term
11    Term(String),
12    /// Error while evaluating a term due to a bug (for debugging)
13    TermBug(String),
14    /// Error while encoding/reading EvaluatedTerm ->/<- bitstring
15    Codec(String),
16    /// PUT reported an error
17    Put(String),
18    /// There was an unexpected IO error. Should never happen because we are not fuzzing on a
19    /// network which can fail.
20    IO(String),
21    /// Some error which was caused because of agents or their names. Like an agent which was not
22    /// found.
23    Agent(String),
24    /// Error while operating on a [`Stream`](crate::stream::Stream)
25    Stream(String),
26    Extraction(),
27    SecurityClaim(&'static str),
28    /// There is a difference between two PUT in differential fuzzing
29    Difference {
30        put1_status: String,
31        put2_status: String,
32        differences: Vec<TraceDifference>,
33    },
34}
35
36impl std::error::Error for Error {}
37
38impl fmt::Display for Error {
39    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
40        match self {
41            Self::Fn(err) => write!(f, "error executing a function symbol: {err}"),
42            Self::Term(err) => write!(f, "error evaluating a term: {err}"),
43            Self::TermBug(err) => write!(f, "critical error evaluating a term due to a bug: {err}"),
44            Error::Codec(err) => write!(
45                f,
46                "error encoding/reading an EvaluatedTerm/bitstring: {err}"
47            ),
48            Self::Put(err) => write!(f, "error in PUT : {err}"),
49            Self::IO(err) => write!(f, "error in io of PUT (this should not happen): {err}"),
50            Self::Agent(err) => write!(f, "error regarding an agent: {err}"),
51            Self::Stream(err) => write!(f, "error in the stream: {err}"),
52            Self::Extraction() => write!(f, "error while extracting variable",),
53            Self::SecurityClaim(msg) => {
54                write!(f, "error because a security violation occurred. msg: {msg}")
55            }
56            Error::Difference {
57                differences: diffs,
58                put1_status: _,
59                put2_status: _,
60            } => {
61                write!(
62                    f,
63                    "difference between two PUTs :\n{}",
64                    diffs
65                        .iter()
66                        .map(|x| x.to_string())
67                        .collect::<Vec<String>>()
68                        .join("\n")
69                )
70            }
71        }
72    }
73}
74
75impl From<FnError> for Error {
76    fn from(err: FnError) -> Self {
77        Self::Fn(err)
78    }
79}
80
81impl From<io::Error> for Error {
82    fn from(err: io::Error) -> Self {
83        Self::IO(err.to_string())
84    }
85}