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
use crate::graph::NodeHandle;
use crate::node::Name;

use std::any::TypeId;
use std::fmt;

/// A generic error type for the crate.
#[derive(Clone, Debug)]
pub enum Error {
    /// A node handle was invalid.
    InvalidNodeHandle(NodeHandle),

    /// An input's name was not valid.
    UnknownInput(Name),

    /// An output's name was not valid.
    UnknownOutput(Name),

    /// An input and an output had incompatible types.
    Incompatible { input: TypeId, output: TypeId },

    /// A node depends on itself.
    Cycle(NodeHandle),
}

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Self::InvalidNodeHandle(handle) => {
                write!(f, "The node handle ({}) is invalid", handle.0)
            }
            Self::UnknownInput(name) => write!(f, "The input {} does not exist", name),
            Self::UnknownOutput(name) => write!(f, "The output {} does not exist", name),
            Self::Incompatible { .. } => write!(f, "The input and the output are not compatible"),
            Self::Cycle(handle) => write!(f, "The node ({}) depends on itself", handle.0),
        }
    }
}

/// The result type for the crate.
pub type Result<T> = std::result::Result<T, Error>;