use crate::qname::QualifiedName;
use core::str;
use std::fmt;
use std::fmt::Formatter;
#[derive(Copy, Clone, Debug, PartialEq)]
pub enum ErrorKind {
StaticAbsent,
DynamicAbsent,
StaticSyntax,
TypeError,
StaticData,
StaticUndefined,
StaticNamespace,
StaticBadFunction,
MixedTypes,
NotNodes,
ContextNotNode,
Terminated,
NotImplemented,
ParseError,
Unknown,
}
impl ErrorKind {
pub fn to_string(&self) -> &'static str {
match *self {
ErrorKind::StaticAbsent => "a component of the static context is absent",
ErrorKind::DynamicAbsent => "a component of the dynamic context is absent",
ErrorKind::StaticSyntax => "syntax error",
ErrorKind::TypeError => "type error",
ErrorKind::StaticData => "wrong static type",
ErrorKind::StaticUndefined => "undefined name",
ErrorKind::StaticNamespace => "namespace axis not supported",
ErrorKind::StaticBadFunction => "function call name and arity do not match",
ErrorKind::MixedTypes => "result of path operator contains both nodes and non-nodes",
ErrorKind::NotNodes => "path expression is not a sequence of nodes",
ErrorKind::ContextNotNode => "context item is not a node for an axis step",
ErrorKind::Terminated => "application has voluntarily terminated processing",
ErrorKind::NotImplemented => "not implemented",
ErrorKind::Unknown => "unknown",
ErrorKind::ParseError => "XML Parse error",
}
}
}
impl fmt::Display for ErrorKind {
fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.to_string())
}
}
#[derive(Clone)]
pub struct Error {
pub kind: ErrorKind,
pub message: String,
pub code: Option<QualifiedName>,
}
impl std::error::Error for Error {}
impl Error {
pub fn new(kind: ErrorKind, message: impl Into<String>) -> Self {
Error {
kind,
message: message.into(),
code: None,
}
}
pub fn new_with_code(
kind: ErrorKind,
message: impl Into<String>,
code: Option<QualifiedName>,
) -> Self {
Error {
kind,
message: message.into(),
code,
}
}
}
impl fmt::Debug for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.message)
}
}
impl fmt::Display for Error {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.write_str(&self.message)
}
}