use crate::diagnostics::codes::ValidationCode;
use crate::diagnostics::{Category, Severity};
#[derive(Debug, Clone, Copy, PartialEq, Eq, strum::EnumIter)]
pub enum XsdConstraintCode {
ElementMissing,
UnexpectedElement,
PatternInvalid,
TypeInvalid,
SchemaConstraintFailed,
}
impl ValidationCode for XsdConstraintCode {
fn code(&self) -> &'static str {
match self {
Self::ElementMissing => "XSD/ElementMissing",
Self::UnexpectedElement => "XSD/UnexpectedElement",
Self::PatternInvalid => "XSD/PatternInvalid",
Self::TypeInvalid => "XSD/TypeInvalid",
Self::SchemaConstraintFailed => "XSD/SchemaConstraintFailed",
}
}
fn description(&self) -> &'static str {
match self {
Self::ElementMissing => "A required element is absent (XSD minOccurs violated).",
Self::UnexpectedElement => "An element appears outside its declared content model.",
Self::PatternInvalid => "A value violates an xs:pattern facet on its type.",
Self::TypeInvalid => {
"A value violates its declared XSD type (built-in or restriction)."
}
Self::SchemaConstraintFailed => "An XSD constraint failed; see message for details.",
}
}
fn default_severity(&self) -> Severity {
Severity::Error
}
fn category(&self) -> Category {
Category::Schema
}
fn example(&self) -> Option<&'static str> {
Some(match self {
Self::ElementMissing =>
"<CompositionPlaylist>…</CompositionPlaylist> <!-- missing required <EditRate> -->",
Self::UnexpectedElement =>
"<CompositionPlaylist><BogusTag/>…</CompositionPlaylist> <!-- not in the schema sequence -->",
Self::PatternInvalid =>
"<Id>not-a-uuid</Id> <!-- dcml:UUIDType pattern expects 'urn:uuid:...' -->",
Self::TypeInvalid =>
"<Size>not-a-number</Size> <!-- declared xs:positiveInteger -->",
Self::SchemaConstraintFailed =>
"<EditRate>24 0</EditRate> <!-- denominator violates a derived restriction -->",
})
}
}
impl XsdConstraintCode {
pub const ALL: &'static [Self] = &[
Self::ElementMissing,
Self::UnexpectedElement,
Self::PatternInvalid,
Self::TypeInvalid,
Self::SchemaConstraintFailed,
];
}
impl From<XsdConstraintCode> for String {
fn from(c: XsdConstraintCode) -> String {
c.code().to_string()
}
}