use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum LoadErrorKind {
Invalid,
Code,
UnknownKey,
Unsupported,
InvalidTarget,
MissingImplementation,
InvalidParams,
DuplicateId,
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadError {
pub kind: LoadErrorKind,
pub path: String,
pub message: String,
}
impl LoadError {
pub(crate) fn new(kind: LoadErrorKind, path: impl Into<String>, message: impl Into<String>) -> Self {
Self { kind, path: path.into(), message: message.into() }
}
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{} at {}", self.message, self.path)
}
}
impl std::error::Error for LoadError {}
pub(crate) enum CodeValue<'a> {
Str(&'a str),
Raw(&'a str),
}
pub(crate) fn xstate_error(code: u32, values: &[CodeValue<'_>]) -> String {
let mut message = format!("XState error E{code}");
if !values.is_empty() {
let values: Vec<String> = values
.iter()
.map(|v| match v {
CodeValue::Str(s) => serde_json::Value::String((*s).to_owned()).to_string(),
CodeValue::Raw(s) => (*s).to_owned(),
})
.collect();
message.push_str(&format!(" ({})", values.join(", ")));
}
message.push_str(&format!(". See https://github.com/statelyai/xstate/blob/next/docs/errors.md#e{code}"));
message
}