xsd_parser/config.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326
//! Contains the [`Config`] structures for the [`generate`](super::generate) method.
use std::path::PathBuf;
use bitflags::bitflags;
use url::Url;
pub use crate::generator::{BoxFlags, ContentMode, GenerateFlags, SerdeSupport, TypedefMode};
pub use crate::schema::{Namespace, NamespacePrefix};
pub use crate::types::{IdentType, Type};
/// Configuration structure for the [`generate`](super::generate) method.
#[derive(Default, Debug, Clone)]
pub struct Config {
/// Configuration for the schema parser.
pub parser: ParserConfig,
/// Configuration for the schema interpreter.
pub interpreter: InterpreterConfig,
/// Configuration for the type information optimizer.
pub optimizer: OptimizerConfig,
/// Configuration for the code generator.
pub generator: GeneratorConfig,
}
/// Configuration for the schema parser.
#[derive(Debug, Clone)]
pub struct ParserConfig {
/// List of resolvers to use for resolving referenced schemas.
pub resolver: Vec<Resolver>,
/// List of namespaces to add to the parser before the schemas are loaded.
///
/// See [`with_namespace`](crate::Parser::with_namespace) for more details.
pub namespaces: Vec<(NamespacePrefix, Namespace)>,
/// List of schemas to load.
pub schemas: Vec<Schema>,
/// Additional flags to control the parser.
pub flags: ParserFlags,
/// Wether to enable the debug output and where to write it to.
pub debug_output: Option<PathBuf>,
}
/// Configuration for the schema interpreter.
#[derive(Debug, Clone)]
pub struct InterpreterConfig {
/// List of user defined types to add to the interpreter before the schemas
/// are actually interpreted.
///
/// See [`with_type`](crate::Interpreter::with_type) for more details.
pub types: Vec<(IdentType, String, Type)>,
/// Additional flags to control the interpreter.
pub flags: InterpreterFlags,
/// Wether to enable the debug output and where to write it to.
pub debug_output: Option<PathBuf>,
}
/// Configuration for the type information optimizer.
#[derive(Debug, Clone)]
pub struct OptimizerConfig {
/// Additional flags to control the optimizer.
pub flags: OptimizerFlags,
/// Wether to enable the debug output and where to write it to.
pub debug_output: Option<PathBuf>,
}
/// Configuration for the code generator.
#[derive(Debug, Clone)]
pub struct GeneratorConfig {
/// Types to add to the generator before the actual code is generated.
///
/// See [`with_type`](crate::Generator::with_type) for more details.
pub types: Vec<(IdentType, String)>,
/// Sets the traits the generated types should derive from.
///
/// See [`derive`](crate::Generator::derive) for more details.
pub derive: Option<Vec<String>>,
/// Postfixes that should be applied to the name of the different generated
/// types.
///
/// See [`with_type_postfix`](crate::Generator::with_type_postfix) for more details.
pub type_postfix: TypePostfix,
/// Tell the generator how to deal with boxing.
pub box_flags: BoxFlags,
/// Tell the generator what type should be generated for the content of an XML element.
pub content_mode: ContentMode,
/// Tells the generator how to deal with type definitions.
pub typedef_mode: TypedefMode,
/// Tells the generator how to generate code for the [`serde`] crate.
pub serde_support: SerdeSupport,
/// Specify which types the generator should generate code for.
pub generate: Generate,
/// Additional flags to control the generator.
pub flags: GenerateFlags,
/// Name of the `xsd-parser` crate that is used for the generated code.
pub xsd_parser: String,
}
/// Postfixed that will be added to the different types generated by the code generator.
#[derive(Debug, Clone)]
pub struct TypePostfix {
/// Postfix added to normal types (like `xs:simpleType` or `xs:complexType`).
pub type_: String,
/// Postfixes added to elements (like `xs:element`).
pub element: String,
}
/// Configuration for the resolver used in [`ParserConfig`].
#[derive(Debug, Clone)]
pub enum Resolver {
/// Resolver that is used to resolve ewb resources (like `http://...` or `https://...`).
#[cfg(feature = "web-resolver")]
Web,
/// Resolver that is used to resolve local resources from disk (like `./local-schema.xsd` or `file://...`).
File {
/// Use the directory the current schema is load from as search path for other schemas.
use_current_path: bool,
/// Add additional search paths that should be used to resolve other local stored schemas.
search_paths: Vec<PathBuf>,
},
}
/// Configuration for the schemas to load used in [`ParserConfig`].
#[derive(Debug, Clone)]
pub enum Schema {
/// Load a schema from the provided URL.
Url(Url),
/// Load a schema from the provided file path.
File(PathBuf),
/// Load the schema from the provided string.
Schema(String),
}
/// Configuration which types the [`Generator`](crate::Generator) should generate
/// code for used in [`GeneratorConfig`].
#[derive(Debug, Clone)]
pub enum Generate {
/// The generator will generate code for all types of the schemas.
All,
/// List of identifiers the generator will generate code for.
///
/// A type is a combination of a identifier type and a string like so:
/// - `(IdentType::Type, "xs:int")`
/// - `(IdentType::Element, "example:rootElement")`
Types(Vec<(IdentType, String)>),
}
bitflags! {
/// Flags to control the [`Parser`](crate::Parser).
#[derive(Debug, Clone)]
pub struct ParserFlags: u32 {
/// Whether the parser should resolve `xs:include` and `xs:import` elements
/// or not.
///
/// See [`resolve_includes`](crate::Parser::resolve_includes) for details.
const RESOLVE_INCLUDES = 1 << 0;
/// Whether to add the default namespaces to the parser or not.
///
/// See [`with_default_namespaces`](crate::Parser::with_default_namespaces) for details.
const DEFAULT_NAMESPACES = 1 << 1;
}
}
bitflags! {
/// Flags to control the [`Interpreter`](crate::Interpreter).
#[derive(Debug, Clone)]
pub struct InterpreterFlags: u32 {
/// Whether to add the build-in types to the interpreter or not.
///
/// See [`with_buildin_types`](crate::Interpreter::with_buildin_types) for details.
const BUILDIN_TYPES = 1 << 0;
/// Whether to add the default types definitions to the interpreter or not.
///
/// See [`with_default_typedefs`](crate::Interpreter::with_default_typedefs) for details.
const DEFAULT_TYPEDEFS = 1 << 1;
}
}
bitflags! {
/// Flags to control the [`Optimizer`](crate::Optimizer).
#[derive(Debug, Clone)]
pub struct OptimizerFlags: u32 {
/// Whether to remove empty enum variants or not.
///
/// See [`remove_empty_enum_variants`](crate::Optimizer::remove_empty_enum_variants) for details.
const REMOVE_EMPTY_ENUM_VARIANTS = 1 << 0;
/// Whether to remove empty enums or not.
///
/// See [`remove_empty_enums`](crate::Optimizer::remove_empty_enums) for details.
const REMOVE_EMPTY_ENUMS = 1 << 1;
/// Whether to remove duplicate union variants or not.
///
/// See [`remove_duplicate_union_variants`](crate::Optimizer::remove_duplicate_union_variants) for details.
const REMOVE_DUPLICATE_UNION_VARIANTS = 1 << 2;
/// Whether to remove empty unions or not.
///
/// See [`remove_empty_unions`](crate::Optimizer::remove_empty_unions) for details.
const REMOVE_EMPTY_UNIONS = 1 << 3;
/// Whether to use the unrestricted base type of a type or not.
///
/// See [`use_unrestricted_base_type`](crate::Optimizer::use_unrestricted_base_type) for details.
const USE_UNRESTRICTED_BASE_TYPE = 1 << 4;
/// Whether to convert abstract types to choices or not.
///
/// See [`convert_abstract_to_choice`](crate::Optimizer::convert_abstract_to_choice) for details.
const CONVERT_ABSTRACT_TO_CHOICE = 1 << 5;
/// Whether to flatten the content of an element or not.
///
/// See [`flatten_element_content`](crate::Optimizer::flatten_element_content) for details.
const FLATTEN_ELEMENT_CONTENT = 1 << 6;
/// Whether to flatten unions or not.
///
/// See [`flatten_unions`](crate::Optimizer::flatten_unions) for details.
const FLATTEN_UNIONS = 1 << 7;
/// Whether to merge enumerations and unions or not.
///
/// See [`merge_enum_unions`](crate::Optimizer::merge_enum_unions) for details.
const MERGE_ENUM_UNIONS = 1 << 8;
/// Whether to resolve type definitions or not.
///
/// See [`resolve_typedefs`](crate::Optimizer::resolve_typedefs) for details.
const RESOLVE_TYPEDEFS = 1 << 9;
/// Whether to remove duplicate types or not.
///
/// See [`remove_duplicates`](crate::Optimizer::remove_duplicates) for details.
const REMOVE_DUPLICATES = 1 << 10;
}
}
impl Default for ParserConfig {
fn default() -> Self {
Self {
resolver: vec![Resolver::File {
use_current_path: true,
search_paths: vec![],
}],
schemas: vec![],
namespaces: vec![],
flags: ParserFlags::RESOLVE_INCLUDES | ParserFlags::DEFAULT_NAMESPACES,
debug_output: None,
}
}
}
impl Default for InterpreterConfig {
fn default() -> Self {
Self {
types: vec![],
debug_output: None,
flags: InterpreterFlags::BUILDIN_TYPES | InterpreterFlags::DEFAULT_TYPEDEFS,
}
}
}
impl Default for OptimizerConfig {
fn default() -> Self {
Self {
debug_output: None,
flags: OptimizerFlags::REMOVE_EMPTY_ENUM_VARIANTS
| OptimizerFlags::REMOVE_EMPTY_ENUMS
| OptimizerFlags::REMOVE_DUPLICATE_UNION_VARIANTS
| OptimizerFlags::REMOVE_EMPTY_UNIONS,
}
}
}
impl Default for GeneratorConfig {
fn default() -> Self {
Self {
types: vec![],
derive: None,
type_postfix: TypePostfix::default(),
box_flags: BoxFlags::AUTO,
content_mode: ContentMode::Auto,
typedef_mode: TypedefMode::Auto,
serde_support: SerdeSupport::None,
generate: Generate::All,
flags: GenerateFlags::empty(),
xsd_parser: "xsd_parser".into(),
}
}
}
impl Default for TypePostfix {
fn default() -> Self {
Self {
type_: String::from("Type"),
element: String::new(),
}
}
}