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
112bitflags! {
113 /// Flags to control the [`Parser`](crate::Parser).
114 #[derive(Debug, Clone)]
115 pub struct ParserFlags: u32 {
116 /// Whether the parser should resolve `xs:include` and `xs:import` elements
117 /// or not.
118 ///
119 /// See [`resolve_includes`](crate::Parser::resolve_includes) for details.
120 const RESOLVE_INCLUDES = 1 << 0;
121
122 /// Whether to add the default namespaces to the parser or not.
123 ///
124 /// See [`with_default_namespaces`](crate::Parser::with_default_namespaces) for details.
125 const DEFAULT_NAMESPACES = 1 << 1;
126
127 /// Allow the parser to assign prefixes known from other schemas to a certain
128 /// namespace if the actual prefix is unknown or already in use.
129 ///
130 /// See [`alternative_prefixes`](crate::Parser::alternative_prefixes) for details.
131 const ALTERNATIVE_PREFIXES = 1 << 2;
132
133 /// Allow the parser to generate suitable prefixes for a certain namespace,
134 /// if the actual prefix is already used.
135 ///
136 /// See [`generate_prefixes`](crate::Parser::generate_prefixes) for details.
137 const GENERATE_PREFIXES = 1 << 3;
138 }
139}