behavior_tree_lite/
error.rs

1use std::fmt::{self, Display, Formatter};
2
3#[derive(Debug)]
4pub enum LoadYamlError {
5    Yaml(serde_yaml::Error),
6    Missing,
7    AddChildError(AddChildError),
8}
9
10impl Display for LoadYamlError {
11    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
12        match self {
13            Self::Yaml(e) => e.fmt(fmt),
14            Self::Missing => write!(fmt, "Missing"),
15            Self::AddChildError(e) => e.fmt(fmt),
16        }
17    }
18}
19
20impl std::error::Error for LoadYamlError {}
21
22impl From<serde_yaml::Error> for LoadYamlError {
23    fn from(err: serde_yaml::Error) -> Self {
24        Self::Yaml(err)
25    }
26}
27
28#[derive(Debug, PartialEq)]
29#[non_exhaustive]
30pub enum AddChildError {
31    TooManyNodes,
32}
33
34impl Display for AddChildError {
35    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
36        match self {
37            Self::TooManyNodes => write!(fmt, "Attempted to add too many nodes"),
38        }
39    }
40}
41
42pub type AddChildResult = Result<(), AddChildError>;
43
44impl std::error::Error for AddChildError {}
45
46#[derive(Debug, PartialEq)]
47#[non_exhaustive]
48pub enum LoadError {
49    MissingTree,
50    MissingNode(String),
51    AddChildError(AddChildError, String),
52    PortUnmatch { node: String, port: String },
53    PortIOUnmatch { node: String, port: String },
54    InfiniteRecursion { node: String },
55}
56
57impl Display for LoadError {
58    fn fmt(&self, fmt: &mut Formatter) -> fmt::Result {
59        match self {
60            Self::MissingTree => write!(fmt, "The main tree does not exist"),
61            Self::MissingNode(node) => {
62                write!(fmt, "Node type or subtree name not found {node:?}")
63            }
64            Self::AddChildError(e, node) => {
65                e.fmt(fmt)?;
66                write!(fmt, " to {node}")
67            }
68            Self::PortUnmatch { node, port } => {
69                write!(fmt, "Port {port:?} was not provided by the node {node:?}")
70            }
71            Self::PortIOUnmatch { node, port } => write!(
72                fmt,
73                "Port {port:?} on node {node:?} has wrong input/output indication"
74            ),
75            Self::InfiniteRecursion { node } => write!(
76                fmt,
77                "Inifinite recusion detected; the same subtree {node:?} was used in itself"
78            ),
79        }
80    }
81}
82
83impl std::error::Error for LoadError {}