use std::fmt;
#[derive(Debug, Clone)]
pub struct SchemaCompileError {
pub message: String,
pub line: Option<u32>,
pub column: Option<u32>,
}
impl SchemaCompileError {
pub fn msg(s: impl Into<String>) -> Self {
Self { message: s.into(), line: None, column: None }
}
pub fn at(mut self, line: u32, column: u32) -> Self {
self.line = Some(line);
self.column = Some(column);
self
}
}
impl fmt::Display for SchemaCompileError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let (Some(l), Some(c)) = (self.line, self.column) {
write!(f, "{l}:{c}: ")?;
}
f.write_str(&self.message)
}
}
impl std::error::Error for SchemaCompileError {}
impl From<crate::error::XmlError> for SchemaCompileError {
fn from(e: crate::error::XmlError) -> Self {
let mut out = Self::msg(e.message);
out.line = e.line;
out.column = e.column;
out
}
}
#[derive(Debug, Clone)]
pub struct ValidationError {
pub issues: Vec<ValidationIssue>,
}
impl ValidationError {
pub fn single(issue: ValidationIssue) -> Self {
Self { issues: vec![issue] }
}
}
impl fmt::Display for ValidationError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self.issues.len() {
0 => f.write_str("(no issues)"),
1 => self.issues[0].fmt(f),
n => write!(f, "{n} validation issues; first: {}", self.issues[0]),
}
}
}
impl std::error::Error for ValidationError {}
#[derive(Debug, Clone)]
pub struct ValidationIssue {
pub message: String,
pub line: Option<u32>,
pub column: Option<u32>,
pub path: String,
pub kind: ValidationKind,
pub expected: Vec<String>,
pub value: Option<String>,
pub type_name: Option<String>,
}
impl fmt::Display for ValidationIssue {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if let (Some(l), Some(c)) = (self.line, self.column) {
write!(f, "{l}:{c}: ")?;
}
if !self.path.is_empty() {
write!(f, "at {}: ", self.path)?;
}
f.write_str(&self.message)
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum ValidationKind {
UnexpectedElement,
UnexpectedAttribute,
MissingRequiredElement,
MissingRequiredAttribute,
TypeMismatch,
FacetViolation,
KeyNotUnique,
KeyRefDangling,
SubstitutionMismatch,
NillableViolation,
AssertionViolation,
Other,
}
#[derive(Debug, Clone)]
pub struct ValidationOptions {
pub fail_fast: bool,
pub max_issues: usize,
pub apply_attribute_defaults: bool,
}
impl Default for ValidationOptions {
fn default() -> Self {
Self { fail_fast: true, max_issues: 1000, apply_attribute_defaults: false }
}
}