use crate::xml::Location;
pub(crate) const MAX_DEPTH: usize = 128;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reason {
#[non_exhaustive]
UnsupportedElement {
name: String,
},
#[non_exhaustive]
MisplacedElement {
name: String,
parent: String,
},
#[non_exhaustive]
StrayText {
text: String,
},
#[non_exhaustive]
UnexpectedRoot {
name: String,
},
#[non_exhaustive]
MissingAttribute {
element: &'static str,
attribute: &'static str,
},
#[non_exhaustive]
ForeignElement {
name: String,
uri: String,
},
#[non_exhaustive]
UnexpectedAttribute {
element: &'static str,
attribute: String,
},
#[non_exhaustive]
UnsupportedAttribute {
element: &'static str,
attribute: &'static str,
},
#[non_exhaustive]
NestingTooDeep {
name: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct LoweringError {
pub(crate) reason: Reason,
pub(crate) message: String,
pub(crate) location: Location,
}
impl LoweringError {
pub(super) fn unsupported(name: &str, location: Location) -> Self {
LoweringError {
message: format!("unsupported element {name:?}"),
reason: Reason::UnsupportedElement { name: name.into() },
location,
}
}
pub(super) fn misplaced(name: &str, parent: &str, location: Location) -> Self {
LoweringError {
message: format!("element {name:?} is not allowed inside {parent:?}"),
reason: Reason::MisplacedElement {
name: name.into(),
parent: parent.into(),
},
location,
}
}
pub(super) fn stray_text(trimmed: &str, location: Location) -> Self {
let preview: String = trimmed.chars().take(40).collect();
let preview = if trimmed.chars().count() > 40 {
format!("{preview}...")
} else {
preview
};
LoweringError {
message: format!("stray text {preview:?} is not allowed here"),
reason: Reason::StrayText {
text: trimmed.into(),
},
location,
}
}
pub(super) fn unexpected_root(name: &str, location: Location) -> Self {
LoweringError {
message: format!("expected the document root to be <scxml>, got {name:?}"),
reason: Reason::UnexpectedRoot { name: name.into() },
location,
}
}
pub(super) fn missing_attribute(
element: &'static str,
attribute: &'static str,
location: Location,
) -> Self {
LoweringError {
message: format!("element {element:?} is missing required attribute {attribute:?}"),
reason: Reason::MissingAttribute { element, attribute },
location,
}
}
pub(super) fn foreign_element(name: &str, uri: &str, location: Location) -> Self {
LoweringError {
message: format!("element {name:?} is bound to foreign namespace {uri:?}, not SCXML"),
reason: Reason::ForeignElement {
name: name.into(),
uri: uri.into(),
},
location,
}
}
pub(super) fn unexpected_attribute(
element: &'static str,
attribute: &str,
location: Location,
) -> Self {
LoweringError {
message: format!("element {element:?} does not accept attribute {attribute:?}"),
reason: Reason::UnexpectedAttribute {
element,
attribute: attribute.into(),
},
location,
}
}
pub(super) fn unsupported_attribute(
element: &'static str,
attribute: &'static str,
location: Location,
) -> Self {
LoweringError {
message: format!(
"element {element:?}'s {attribute:?} attribute is defined by the spec but not \
implemented by this engine"
),
reason: Reason::UnsupportedAttribute { element, attribute },
location,
}
}
pub(super) fn nesting_too_deep(name: &str, location: Location) -> Self {
LoweringError {
message: format!(
"element {name:?} is nested more than {MAX_DEPTH} elements below the root"
),
reason: Reason::NestingTooDeep { name: name.into() },
location,
}
}
}