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
// This is free and unencumbered software released into the public domain.

use crate::{
    prelude::{fmt, Box, Result, String, ToString},
    PortError,
};

#[cfg(feature = "std")]
extern crate std;

pub type BlockResult<T = ()> = Result<T, BlockError>;

#[derive(Debug)]
pub enum BlockError {
    Terminated,
    PortError(PortError),
    Other(String),
    #[cfg(feature = "std")]
    Panic(Box<dyn std::any::Any + Send>),
}

impl fmt::Display for BlockError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::Terminated => write!(f, "Execution terminated"),
            Self::PortError(e) => write!(f, "{}", e),
            Self::Other(message) => write!(f, "{}", message),
            #[cfg(feature = "std")]
            Self::Panic(e) => write!(f, "Panic: {:?}", e),
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for BlockError {}

#[cfg(feature = "std")]
impl From<std::io::Error> for BlockError {
    fn from(error: std::io::Error) -> Self {
        Self::Other(error.to_string())
    }
}

#[cfg(feature = "std")]
impl From<Box<dyn std::any::Any + Send>> for BlockError {
    fn from(error: Box<dyn std::any::Any + Send>) -> Self {
        Self::Panic(error)
    }
}

impl From<PortError> for BlockError {
    fn from(error: PortError) -> Self {
        Self::PortError(error)
    }
}