use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LoadError {
pub kind: LoadErrorKind,
pub file: String,
pub line: u32,
pub message: String,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum LoadErrorKind {
MissingFile,
Malformed,
Unsupported,
Invalid,
}
impl fmt::Display for LoadError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let kind = match self.kind {
LoadErrorKind::MissingFile => "missing schema document",
LoadErrorKind::Malformed => "malformed schema document",
LoadErrorKind::Unsupported => "unsupported schema construct",
LoadErrorKind::Invalid => "invalid schema",
};
write!(f, "{}:{}: {kind}: {}", self.file, self.line, self.message)
}
}
impl std::error::Error for LoadError {}
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum DocumentError {
Malformed {
line: u32,
column: u32,
message: String,
},
Unsupported {
line: u32,
column: u32,
message: String,
},
}
impl fmt::Display for DocumentError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self {
Self::Malformed {
line,
column,
message,
} => {
write!(f, "{line}:{column}: malformed document: {message}")
}
Self::Unsupported {
line,
column,
message,
} => {
write!(f, "{line}:{column}: unsupported: {message}")
}
}
}
}
impl std::error::Error for DocumentError {}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub enum ErrorKind {
UnknownRoot,
UnexpectedElement,
MissingElement,
TextNotAllowed,
ElementInSimpleContent,
ContentNotEmpty,
UndeclaredElement,
UndeclaredAttribute,
AttributeNotAllowed,
MissingAttribute,
InvalidValue,
InvalidAttributeValue,
DuplicateId,
UnresolvedIdref,
InvalidXsiType,
AbstractType,
NotNillable,
InvalidXsiNil,
}
impl ErrorKind {
#[must_use]
pub fn as_str(self) -> &'static str {
match self {
Self::UnknownRoot => "unknown-root",
Self::UnexpectedElement => "unexpected-element",
Self::MissingElement => "missing-element",
Self::TextNotAllowed => "text-not-allowed",
Self::ElementInSimpleContent => "element-in-simple-content",
Self::ContentNotEmpty => "content-not-empty",
Self::UndeclaredElement => "undeclared-element",
Self::UndeclaredAttribute => "undeclared-attribute",
Self::AttributeNotAllowed => "attribute-not-allowed",
Self::MissingAttribute => "missing-attribute",
Self::InvalidValue => "invalid-value",
Self::InvalidAttributeValue => "invalid-attribute-value",
Self::DuplicateId => "duplicate-id",
Self::UnresolvedIdref => "unresolved-idref",
Self::InvalidXsiType => "invalid-xsi-type",
Self::AbstractType => "abstract-type",
Self::NotNillable => "not-nillable",
Self::InvalidXsiNil => "invalid-xsi-nil",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ValidationError {
pub kind: ErrorKind,
pub line: u32,
pub column: u32,
pub element: String,
pub attribute: Option<String>,
pub message: String,
}
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}:{}: {}: element {}",
self.line, self.column, self.kind, self.element
)?;
if let Some(attribute) = &self.attribute {
write!(f, ", attribute {attribute}")?;
}
write!(f, ": {}", self.message)
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default)]
pub struct Report {
pub errors: Vec<ValidationError>,
}
impl Report {
#[must_use]
pub fn is_valid(&self) -> bool {
self.errors.is_empty()
}
}