xsd_parser/pipeline/parser/
error.rs1use std::fmt::{Display, Formatter, Result as FmtResult};
2use std::io::Error as IoError;
3use std::path::PathBuf;
4
5use thiserror::Error;
6use url::{ParseError as UrlParseError, Url};
7
8use xsd_parser_types::misc::Namespace;
9use xsd_parser_types::quick_xml::Error as XmlError;
10
11use super::resolver::ResolveRequest;
12
13#[derive(Debug, Error)]
15pub enum Error<E> {
16 #[error("IO Error: {0}")]
18 IoError(#[from] IoError),
19
20 #[error("{0}")]
22 XmlError(#[from] XmlErrorWithLocation),
23
24 #[error("URL Parse Error: {0}")]
26 UrlParseError(#[from] UrlParseError),
27
28 #[error("Unable to resolve requested resource: {0}")]
30 UnableToResolve(Box<ResolveRequest>),
31
32 #[error(
35 "Mismatching target namespace (location={location}, found={found}, expected={expected})"
36 )]
37 MismatchingTargetNamespace {
38 location: Url,
40
41 found: Namespace,
43
44 expected: Namespace,
46 },
47
48 #[error("Resolver Error: {0}")]
50 Resolver(E),
51
52 #[error("Invalid file path: {0}!")]
56 InvalidFilePath(PathBuf),
57}
58
59impl<E> Error<E> {
60 pub fn resolver<X: Into<E>>(error: X) -> Self {
62 Self::Resolver(error.into())
63 }
64}
65
66#[derive(Debug, Error)]
68pub struct XmlErrorWithLocation {
69 pub error: XmlError,
71
72 pub location: Option<Url>,
74}
75
76impl From<XmlError> for XmlErrorWithLocation {
77 fn from(error: XmlError) -> Self {
78 Self {
79 error,
80 location: None,
81 }
82 }
83}
84
85impl Display for XmlErrorWithLocation {
86 fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
87 let Self { error, location } = self;
88
89 write!(f, "XML Error: ")?;
90 error.fmt(f)?;
91
92 if let Some(location) = location {
93 write!(f, "\n in schema ")?;
94 location.fmt(f)?;
95 }
96
97 Ok(())
98 }
99}