1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
use std::{fmt, io};

use crate::algebra::error::FnError;

#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Error {
    /// Returned if a concrete function from the protocol fails or term evaluation fails
    Fn(FnError),
    /// Error while evaluating a term
    Term(String),
    /// Error while encoding/reading EvaluatedTerm ->/<- bitstring
    Codec(String),
    /// PUT reported an error
    Put(String),
    /// There was an unexpected IO error. Should never happen because we are not fuzzing on a
    /// network which can fail.
    IO(String),
    /// Some error which was caused because of agents or their names. Like an agent which was not
    /// found.
    Agent(String),
    /// Error while operating on a [`Stream`](crate::stream::Stream)
    Stream(String),
    Extraction(),
    SecurityClaim(&'static str),
}

impl std::error::Error for Error {}

impl From<anyhow::Error> for Error {
    fn from(value: anyhow::Error) -> Self {
        Self::Term(format!("AnyHow Error: {value}"))
    }
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Fn(err) => write!(f, "error executing a function symbol: {err}"),
            Self::Term(err) => write!(f, "error evaluating a term: {err}"),
            Error::Codec(err) => write!(
                f,
                "error encoding/reading an EvaluatedTerm/bitstring: {err}"
            ),
            Self::Put(err) => write!(f, "error in openssl: {err}"),
            Self::IO(err) => write!(f, "error in io of openssl (this should not happen): {err}"),
            Self::Agent(err) => write!(f, "error regarding an agent: {err}"),
            Self::Stream(err) => write!(f, "error in the stream: {err}"),
            Self::Extraction() => write!(f, "error while extracting variable",),
            Self::SecurityClaim(msg) => {
                write!(f, "error because a security violation occurred. msg: {msg}")
            }
        }
    }
}

impl From<FnError> for Error {
    fn from(err: FnError) -> Self {
        Self::Fn(err)
    }
}

impl From<io::Error> for Error {
    fn from(err: io::Error) -> Self {
        Self::IO(err.to_string())
    }
}