use gesha_collections::partial_result::PartialResult;
use gesha_collections::tracking::{ContextAppendable, ContextBindable};
use std::fmt::Debug;
pub type Result<A> = std::result::Result<A, Error>;
pub type Output<A> = PartialResult<A, Error>;
#[derive(Debug)]
pub enum Error {
EnumFormatMismatch {
format: String,
value: String,
},
Enclosed {
key: String,
cause: Box<Error>,
},
FormatFailed {
detail: String,
},
InvalidToken {
target: String,
},
Multiple(Vec<Error>),
ReferenceObjectNotFound(String),
TransformBroken {
detail: String,
},
Unimplemented {
message: String,
},
}
impl ContextBindable<String> for Error {
fn bind(key: impl Into<String>, causes: Vec<Self>) -> Self {
Self::Enclosed {
key: key.into(),
cause: Box::new(causes.into()),
}
}
}
impl ContextAppendable<String> for Error {
fn append(key: impl Into<String>, cause: Self) -> Self {
Self::Enclosed {
key: key.into(),
cause: Box::new(cause),
}
}
}
impl From<Vec<Error>> for Error {
fn from(mut causes: Vec<Error>) -> Self {
if causes.len() == 1 {
causes.remove(0)
} else {
Error::Multiple(causes)
}
}
}
#[macro_export]
macro_rules! broken {
($shape: expr) => {
$crate::conversions::Error::TransformBroken {
detail: format!(
"unprocessed shape found:\n at {file}:{line}\n{shape:#?}",
file = file!(),
line = line!(),
shape = $shape,
),
}
};
}
#[macro_export]
macro_rules! broken_defs {
($name: expr) => {
$crate::conversions::Error::TransformBroken {
detail: format!(
"unprocessed defs found: {name}\n at {file}:{line}",
name = $name,
file = file!(),
line = line!(),
),
}
};
}