xsd_parser/config/parser.rs
1use std::path::PathBuf;
2
3use bitflags::bitflags;
4use url::Url;
5
6use super::{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 | ParserFlags::DEFAULT_NAMESPACES,
36 debug_output: None,
37 }
38 }
39}
40
41/// Configuration for the [`Resolver`](crate::pipeline::parser::Resolver)s used in [`ParserConfig`].
42#[derive(Debug, Clone)]
43pub enum Resolver {
44 /// Resolver that is used to resolve ewb resources (like `http://...` or `https://...`).
45 #[cfg(feature = "web-resolver")]
46 Web,
47
48 /// Resolver that is used to resolve local resources from disk (like `./local-schema.xsd` or `file://...`).
49 File,
50}
51
52/// Configuration for the schemas to load used in [`ParserConfig`].
53#[derive(Debug, Clone)]
54pub enum Schema {
55 /// Load a schema from the provided URL.
56 Url(Url),
57
58 /// Load a schema from the provided file path.
59 File(PathBuf),
60
61 /// Load the schema from the provided string.
62 Schema(String),
63
64 /// Load the schema from the provided strings: `(name, schema)`.
65 NamedSchema(String, String),
66}
67
68impl Schema {
69 /// Create a [`Schema::Url`] from the passed `value`.
70 #[inline]
71 pub fn url<T>(value: T) -> Self
72 where
73 T: Into<Url>,
74 {
75 Self::Url(value.into())
76 }
77
78 /// Create a [`Schema::File`] from the passed `value`.
79 #[inline]
80 pub fn file<T>(value: T) -> Self
81 where
82 T: Into<PathBuf>,
83 {
84 Self::File(value.into())
85 }
86
87 /// Create a [`Schema::Schema`] from the passed `value`.
88 #[inline]
89 #[allow(clippy::self_named_constructors)]
90 pub fn schema<T>(value: T) -> Self
91 where
92 T: Into<String>,
93 {
94 Self::Schema(value.into())
95 }
96
97 /// Create a [`Schema::NamedSchema`] from the passed `name` and `value`.
98 #[inline]
99 #[allow(clippy::self_named_constructors)]
100 pub fn named_schema<S, T>(name: S, value: T) -> Self
101 where
102 S: Into<String>,
103 T: Into<String>,
104 {
105 Self::NamedSchema(name.into(), value.into())
106 }
107}
108
109bitflags! {
110 /// Flags to control the [`Parser`](crate::Parser).
111 #[derive(Debug, Clone)]
112 pub struct ParserFlags: u32 {
113 /// Whether the parser should resolve `xs:include` and `xs:import` elements
114 /// or not.
115 ///
116 /// See [`resolve_includes`](crate::Parser::resolve_includes) for details.
117 const RESOLVE_INCLUDES = 1 << 0;
118
119 /// Whether to add the default namespaces to the parser or not.
120 ///
121 /// See [`with_default_namespaces`](crate::Parser::with_default_namespaces) for details.
122 const DEFAULT_NAMESPACES = 1 << 1;
123 }
124}