Skip to main content

xsd_parser/config/
parser.rs

1use std::path::PathBuf;
2
3use bitflags::bitflags;
4use url::Url;
5
6use xsd_parser_types::misc::{Namespace, NamespacePrefix};
7
8/// Configuration for the schema parser.
9#[derive(Debug, Clone)]
10pub struct ParserConfig {
11    /// List of resolvers to use for resolving referenced schemas.
12    pub resolver: Vec<Resolver>,
13
14    /// List of namespaces to add to the parser before the schemas are loaded.
15    ///
16    /// See [`with_namespace`](crate::Parser::with_namespace) for more details.
17    pub namespaces: Vec<(NamespacePrefix, Namespace)>,
18
19    /// List of schemas to load.
20    pub schemas: Vec<Schema>,
21
22    /// Additional flags to control the parser.
23    pub flags: ParserFlags,
24
25    /// Wether to enable the debug output and where to write it to.
26    pub debug_output: Option<PathBuf>,
27}
28
29impl Default for ParserConfig {
30    fn default() -> Self {
31        Self {
32            resolver: vec![Resolver::File],
33            schemas: vec![],
34            namespaces: vec![],
35            flags: ParserFlags::RESOLVE_INCLUDES
36                | ParserFlags::GENERATE_PREFIXES
37                | ParserFlags::DEFAULT_NAMESPACES
38                | ParserFlags::ALTERNATIVE_PREFIXES,
39            debug_output: None,
40        }
41    }
42}
43
44/// Configuration for the [`Resolver`](crate::pipeline::parser::Resolver)s used in [`ParserConfig`].
45#[derive(Debug, Clone)]
46pub enum Resolver {
47    /// Resolver that is used to resolve ewb resources (like `http://...` or `https://...`).
48    #[cfg(feature = "web-resolver")]
49    Web,
50
51    /// Resolver that is used to resolve local resources from disk (like `./local-schema.xsd` or `file://...`).
52    File,
53}
54
55/// Configuration for the schemas to load used in [`ParserConfig`].
56#[derive(Debug, Clone)]
57pub enum Schema {
58    /// Load a schema from the provided URL.
59    Url(Url),
60
61    /// Load a schema from the provided file path.
62    File(PathBuf),
63
64    /// Load the schema from the provided string.
65    Schema(String),
66
67    /// Load the schema from the provided strings: `(name, schema)`.
68    NamedSchema(String, String),
69}
70
71impl Schema {
72    /// Create a [`Schema::Url`] from the passed `value`.
73    #[inline]
74    pub fn url<T>(value: T) -> Self
75    where
76        T: Into<Url>,
77    {
78        Self::Url(value.into())
79    }
80
81    /// Create a [`Schema::File`] from the passed `value`.
82    #[inline]
83    pub fn file<T>(value: T) -> Self
84    where
85        T: Into<PathBuf>,
86    {
87        Self::File(value.into())
88    }
89
90    /// Create a [`Schema::Schema`] from the passed `value`.
91    #[inline]
92    #[allow(clippy::self_named_constructors)]
93    pub fn schema<T>(value: T) -> Self
94    where
95        T: Into<String>,
96    {
97        Self::Schema(value.into())
98    }
99
100    /// Create a [`Schema::NamedSchema`] from the passed `name` and `value`.
101    #[inline]
102    #[allow(clippy::self_named_constructors)]
103    pub fn named_schema<S, T>(name: S, value: T) -> Self
104    where
105        S: Into<String>,
106        T: Into<String>,
107    {
108        Self::NamedSchema(name.into(), value.into())
109    }
110}
111
112impl From<Url> for Schema {
113    fn from(value: Url) -> Self {
114        Self::Url(value)
115    }
116}
117
118impl From<PathBuf> for Schema {
119    fn from(value: PathBuf) -> Self {
120        Self::File(value)
121    }
122}
123
124impl From<String> for Schema {
125    fn from(value: String) -> Self {
126        Self::Schema(value)
127    }
128}
129
130impl From<(String, String)> for Schema {
131    fn from((name, schema): (String, String)) -> Self {
132        Self::NamedSchema(name, schema)
133    }
134}
135
136bitflags! {
137    /// Flags to control the [`Parser`](crate::Parser).
138    #[derive(Debug, Clone)]
139    pub struct ParserFlags: u32 {
140        /// Whether the parser should resolve `xs:include` and `xs:import` elements
141        /// or not.
142        ///
143        /// See [`resolve_includes`](crate::Parser::resolve_includes) for details.
144        const RESOLVE_INCLUDES = 1 << 0;
145
146        /// Whether to add the default namespaces to the parser or not.
147        ///
148        /// See [`with_default_namespaces`](crate::Parser::with_default_namespaces) for details.
149        const DEFAULT_NAMESPACES = 1 << 1;
150
151        /// Allow the parser to assign prefixes known from other schemas to a certain
152        /// namespace if the actual prefix is unknown or already in use.
153        ///
154        /// See [`alternative_prefixes`](crate::Parser::alternative_prefixes) for details.
155        const ALTERNATIVE_PREFIXES = 1 << 2;
156
157        /// Allow the parser to generate suitable prefixes for a certain namespace,
158        /// if the actual prefix is already used.
159        ///
160        /// See [`generate_prefixes`](crate::Parser::generate_prefixes) for details.
161        const GENERATE_PREFIXES = 1 << 3;
162    }
163}