Skip to main content

anathema_widgets/
error.rs

1use std::fmt::Display;
2use std::path::PathBuf;
3
4pub type Result<T, E = Error> = std::result::Result<T, E>;
5
6#[derive(Debug)]
7pub struct Error {
8    pub kind: ErrorKind,
9    pub path: Option<PathBuf>,
10}
11
12impl Error {
13    pub(crate) fn transaction_failed() -> Self {
14        Self {
15            kind: ErrorKind::TreeTransactionFailed,
16            path: None,
17        }
18    }
19}
20
21impl Display for Error {
22    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
23        self.kind.fmt(f)
24    }
25}
26
27#[derive(Debug)]
28pub enum ErrorKind {
29    InvalidElement(String),
30    TreeTransactionFailed,
31    ComponentConsumed(String),
32}
33
34impl Display for ErrorKind {
35    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
36        match self {
37            ErrorKind::InvalidElement(el) => write!(f, "element `{el}` does not exist"),
38            ErrorKind::TreeTransactionFailed => write!(
39                f,
40                "failed to insert into the widget tree (most likely the parent was removed)"
41            ),
42            ErrorKind::ComponentConsumed(name) => write!(f, "`@{name}` has already been used"),
43        }
44    }
45}
46
47impl std::error::Error for Error {}