gesha_core/conversions/
error.rs

1use gesha_collections::partial_result::PartialResult;
2use gesha_collections::tracking::{ContextAppendable, ContextBindable};
3use std::fmt::Debug;
4
5pub type Result<A> = std::result::Result<A, Error>;
6
7pub type Output<A> = PartialResult<A, Error>;
8
9#[derive(Debug)]
10pub enum Error {
11    Enclosed {
12        key: String,
13        cause: Box<Error>,
14    },
15
16    /// Cause: Client
17    /// - `rustfmt` is missing.
18    ///
19    /// Cause: Internal
20    /// - The generated code is malformed.
21    FormatFailed {
22        detail: String,
23    },
24
25    /// Cause: Client
26    /// - Invalid character or unrecognized symbol in the token
27    InvalidToken {
28        target: String,
29    },
30
31    Multiple(Vec<Error>),
32
33    /// Cause: Client
34    /// - References a schema that does not exist.
35    ReferenceObjectNotFound(String),
36
37    /// Cause: Internal
38    /// - a shape that has not been processed correctly.
39    TransformBroken {
40        detail: String,
41    },
42
43    /// Cause: Internal
44    /// - The property is not supported.
45    /// - The property is unimplemented in the current version.
46    Unimplemented {
47        message: String,
48    },
49}
50
51impl ContextBindable<String> for Error {
52    fn bind(key: impl Into<String>, causes: Vec<Self>) -> Self {
53        Self::Enclosed {
54            key: key.into(),
55            cause: Box::new(causes.into()),
56        }
57    }
58}
59
60impl ContextAppendable<String> for Error {
61    fn append(key: impl Into<String>, cause: Self) -> Self {
62        Self::Enclosed {
63            key: key.into(),
64            cause: Box::new(cause),
65        }
66    }
67}
68
69impl From<Vec<Error>> for Error {
70    fn from(mut causes: Vec<Error>) -> Self {
71        if causes.len() == 1 {
72            causes.remove(0)
73        } else {
74            Error::Multiple(causes)
75        }
76    }
77}
78
79#[macro_export]
80macro_rules! broken {
81    ($shape: expr) => {
82        $crate::conversions::Error::TransformBroken {
83            detail: format!(
84                "unprocessed shape found:\n  at {file}:{line}\n{shape:#?}",
85                file = file!(),
86                line = line!(),
87                shape = $shape,
88            ),
89        }
90    };
91}
92
93#[macro_export]
94macro_rules! broken_defs {
95    ($name: expr) => {
96        $crate::conversions::Error::TransformBroken {
97            detail: format!(
98                "unprocessed defs found: {name}\n  at {file}:{line}",
99                name = $name,
100                file = file!(),
101                line = line!(),
102            ),
103        }
104    };
105}