use crate::xml::Location;
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DefaultTransitionOwner {
Initial(Option<String>),
History(Option<String>),
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Reason {
#[non_exhaustive]
DuplicateId {
id: String,
},
EmptyId,
#[non_exhaustive]
UnresolvedTarget {
id: String,
},
#[non_exhaustive]
UnresolvedInitial {
id: String,
},
#[non_exhaustive]
InitialNotDescendant {
id: String,
parent_id: Option<String>,
},
#[non_exhaustive]
InitialOnAtomicState {
id: Option<String>,
},
#[non_exhaustive]
InitialAttributeAndElement {
id: Option<String>,
},
#[non_exhaustive]
TransitionCount {
owner: DefaultTransitionOwner,
count: usize,
},
#[non_exhaustive]
TransitionMissingTarget {
owner: DefaultTransitionOwner,
},
#[non_exhaustive]
TransitionForbiddenAttribute {
owner: DefaultTransitionOwner,
attribute: &'static str,
},
#[non_exhaustive]
HistoryBadParent {
id: Option<String>,
parent_kind: &'static str,
},
#[non_exhaustive]
HistoryBadType {
raw: String,
},
#[non_exhaustive]
TransitionBadType {
raw: String,
},
#[non_exhaustive]
ScxmlBadBinding {
raw: String,
},
#[non_exhaustive]
FinalHasStates {
id: Option<String>,
},
#[non_exhaustive]
FinalHasTransitions {
id: Option<String>,
},
#[non_exhaustive]
FinalParentMissingId {
final_id: Option<String>,
},
#[non_exhaustive]
DefaultEntryNotEnterable {
id: Option<String>,
child_kind: &'static str,
},
#[non_exhaustive]
DonedataNotOnFinal {
id: Option<String>,
},
#[non_exhaustive]
DonedataContentAndParams {
id: Option<String>,
},
#[non_exhaustive]
ContentExprAndText {
expr: String,
},
#[non_exhaustive]
ParamExprAndLocation {
name: String,
},
#[non_exhaustive]
ParamNoValue {
name: String,
},
#[non_exhaustive]
BadNamespace {
uri: Option<String>,
},
#[non_exhaustive]
BadVersion {
version: Option<String>,
},
#[non_exhaustive]
ScxmlBadDatamodel {
raw: String,
},
#[non_exhaustive]
DataExprAndSrc {
id: String,
},
#[non_exhaustive]
DataValueAndChildren {
id: String,
},
#[non_exhaustive]
DataReservedId {
id: String,
},
#[non_exhaustive]
DatamodelBadParent {
kind: &'static str,
},
#[non_exhaustive]
AssignExprAndText {
expr: String,
},
IfElseifAfterElse,
IfDuplicateElse,
ScriptNoSrcOrText,
InvokeTypeAndTypeexpr,
InvokeSrcAndSrcexpr,
InvokeSrcAndContent,
InvokeIdAndIdlocation,
InvokeNamelistAndParam,
#[non_exhaustive]
InvokeBadAutoforward {
raw: String,
},
SendEventAndEventexpr,
SendTargetAndTargetexpr,
SendTypeAndTypeexpr,
SendIdAndIdlocation,
SendDelayAndDelayexpr,
SendDelayAndInternalTarget,
SendNamelistAndContent,
SendParamAndContent,
CancelSendidAndSendidexpr,
CancelNoSendid,
#[non_exhaustive]
DuplicateAttribute {
element: String,
attribute: String,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct ValidationError {
pub(crate) reason: Reason,
pub(crate) message: String,
pub(crate) location: Location,
}
impl ValidationError {
pub(super) fn new(reason: Reason, location: Location) -> Self {
ValidationError {
message: message(&reason),
reason,
location,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum WarningReason {
#[non_exhaustive]
FinalizeForbiddenContent {
element: &'static str,
},
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub(crate) struct Warning {
pub(crate) reason: WarningReason,
pub(crate) message: String,
pub(crate) location: Location,
}
impl Warning {
pub(super) fn new(reason: WarningReason, location: Location) -> Self {
let WarningReason::FinalizeForbiddenContent { element } = reason;
Warning {
message: format!("<{element}> must not occur inside <finalize>"),
reason,
location,
}
}
}
fn quote(value: Option<&String>) -> String {
value.map_or_else(|| "nil".to_owned(), |value| format!("{value:?}"))
}
fn owner_description(owner: &DefaultTransitionOwner) -> String {
match owner {
DefaultTransitionOwner::Initial(None) => "<initial>".to_owned(),
DefaultTransitionOwner::Initial(Some(id)) => format!("<initial> on {id:?}"),
DefaultTransitionOwner::History(None) => "<history>".to_owned(),
DefaultTransitionOwner::History(Some(id)) => format!("<history> {id:?}"),
}
}
fn message(reason: &Reason) -> String {
const FINAL_CONTENT: &str = "only onentry, onexit, and donedata are legal there";
match reason {
Reason::DuplicateId { id } => format!("duplicate state id {id:?}"),
Reason::EmptyId => {
"state id must not be empty - an absent id is legal, an empty one is not".to_owned()
}
Reason::UnresolvedTarget { id } => {
format!("transition target {id:?} does not resolve to a state in the document")
}
Reason::UnresolvedInitial { id } => {
format!("initial state {id:?} does not resolve to a state in the document")
}
Reason::InitialNotDescendant { id, parent_id } => {
let parent = parent_id.as_ref().map_or_else(
|| "its parent state, which has no id".to_owned(),
|parent| format!("{parent:?}"),
);
format!("initial state {id:?} is not a descendant of {parent}")
}
Reason::InitialOnAtomicState { id } => {
let state = id.as_ref().map_or_else(
|| "a state with no id".to_owned(),
|id| format!("state {id:?}"),
);
format!("{state} has no child states to default into")
}
Reason::InitialAttributeAndElement { id } => format!(
"state {} has both an initial attribute and an <initial> element - at most one is legal",
quote(id.as_ref())
),
Reason::TransitionCount { owner, count } => format!(
"{} must have exactly one transition, found {count}",
owner_description(owner)
),
Reason::TransitionMissingTarget { owner } => format!(
"{} transition must specify a target",
owner_description(owner)
),
Reason::TransitionForbiddenAttribute { owner, attribute } => format!(
"{} transition must not specify {attribute}",
owner_description(owner)
),
Reason::HistoryBadParent { id, parent_kind } => format!(
"history {} must be a child of a compound <state> or <parallel>, not {parent_kind}",
quote(id.as_ref())
),
Reason::HistoryBadType { raw } => {
format!("history type {raw:?} must be \"shallow\" or \"deep\"")
}
Reason::TransitionBadType { raw } => {
format!("transition type {raw:?} must be \"internal\" or \"external\"")
}
Reason::ScxmlBadBinding { raw } => format!("binding {raw:?} must be \"early\" or \"late\""),
Reason::FinalHasStates { id } => format!(
"state {} must not be a child of <final> - {FINAL_CONTENT}",
quote(id.as_ref())
),
Reason::FinalHasTransitions { id } => format!(
"state {} is a <final> and must not carry a <transition> - {FINAL_CONTENT}",
quote(id.as_ref())
),
Reason::FinalParentMissingId { .. } => "a state containing a <final> child must have an \
id - its completion event is named done.state.<id>"
.to_owned(),
Reason::DefaultEntryNotEnterable { id, child_kind } => format!(
"state {} has no initial attribute or <initial> element, and its first child is a \
{child_kind} pseudo-state, which cannot be entered by default",
quote(id.as_ref())
),
Reason::DonedataNotOnFinal { id } => format!(
"state {} carries <donedata>, which is only legal on <final>",
quote(id.as_ref())
),
Reason::DonedataContentAndParams { id } => format!(
"<donedata> on {} must not specify both a <content> child and one or more <param> \
children",
quote(id.as_ref())
),
Reason::ContentExprAndText { expr } => content_message(expr, "text"),
Reason::ParamExprAndLocation { name } => format!(
"<param> {name:?} must not specify both an expr attribute and a location attribute"
),
Reason::ParamNoValue { name } => {
format!(
"<param> {name:?} must specify either an expr attribute or a location attribute"
)
}
Reason::BadNamespace { uri: None } => format!(
"the root element declares no namespace - expected xmlns={:?}",
crate::lowering::SCXML_NAMESPACE
),
Reason::BadNamespace { uri: Some(uri) } => format!(
"the root element's namespace {uri:?} is not the SCXML namespace {:?}",
crate::lowering::SCXML_NAMESPACE
),
Reason::BadVersion { version: None } => {
"the root element has no version attribute, expected \"1.0\"".to_owned()
}
Reason::BadVersion {
version: Some(version),
} => format!("the root element's version {version:?} must be \"1.0\""),
Reason::ScxmlBadDatamodel { raw } => format!(
"datamodel {raw:?} must be one of \"predicator\", \"elixir\", \"null\", \
\"ecmascript\", or \"xpath\""
),
Reason::DataExprAndSrc { id } => {
format!("<data> {id:?} must not specify both expr and src")
}
Reason::DataValueAndChildren { id } => {
format!("<data> {id:?} must not specify expr or src together with child content")
}
Reason::DataReservedId { id } => {
format!("<data> id {id:?} must not begin with \"_\" - that prefix is reserved")
}
Reason::DatamodelBadParent { kind } => {
format!("<datamodel> must not be a child of a {kind} state")
}
Reason::AssignExprAndText { expr } => {
format!("<assign> must not specify both an expr attribute ({expr:?}) and child content")
}
Reason::IfElseifAfterElse => {
"<elseif> must not follow <else> - <else> must occur after all <elseif> tags".to_owned()
}
Reason::IfDuplicateElse => "<if> must not carry more than one <else> branch".to_owned(),
Reason::ScriptNoSrcOrText => {
"<script> must specify either the src attribute or child content".to_owned()
}
Reason::InvokeTypeAndTypeexpr => both("invoke", "type", "typeexpr"),
Reason::InvokeSrcAndSrcexpr => both("invoke", "src", "srcexpr"),
Reason::InvokeSrcAndContent => {
"<invoke> must not specify src or srcexpr together with a <content> child".to_owned()
}
Reason::InvokeIdAndIdlocation => both("invoke", "id", "idlocation"),
Reason::InvokeNamelistAndParam => {
"<invoke> must not specify namelist together with a <param> child".to_owned()
}
Reason::InvokeBadAutoforward { raw } => {
format!("invoke autoforward {raw:?} must be \"true\" or \"false\"")
}
Reason::SendEventAndEventexpr => both("send", "event", "eventexpr"),
Reason::SendTargetAndTargetexpr => both("send", "target", "targetexpr"),
Reason::SendTypeAndTypeexpr => both("send", "type", "typeexpr"),
Reason::SendIdAndIdlocation => both("send", "id", "idlocation"),
Reason::SendDelayAndDelayexpr => both("send", "delay", "delayexpr"),
Reason::SendDelayAndInternalTarget => {
"<send> must not specify delay or delayexpr when target is \"_internal\"".to_owned()
}
Reason::SendNamelistAndContent => {
"<send> must not specify namelist together with a <content> child".to_owned()
}
Reason::SendParamAndContent => {
"<send> must not specify a <param> child together with a <content> child".to_owned()
}
Reason::CancelSendidAndSendidexpr => both("cancel", "sendid", "sendidexpr"),
Reason::CancelNoSendid => {
"<cancel> must specify exactly one of sendid or sendidexpr".to_owned()
}
Reason::DuplicateAttribute { element, attribute } => {
format!("duplicate attribute {attribute:?} on <{element}>")
}
}
}
pub(super) fn content_message(expr: &str, form: &str) -> String {
format!("<content> must not specify both an expr attribute ({expr:?}) and inline {form}")
}
fn both(element: &str, first: &str, second: &str) -> String {
format!("<{element}> must not specify both {first} and {second}")
}