xsd-parser 1.5.2

Rust code generator for XML schema files
Documentation
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
use std::any::{Any, TypeId};
use std::borrow::Cow;
use std::collections::hash_map::{Entry, HashMap};
use std::ops::Deref;
use std::str::FromStr;

use parking_lot::Mutex;
use proc_macro2::{Ident as Ident2, TokenStream};
use quote::format_ident;

use crate::config::GeneratorFlags;
use crate::models::code::{IdentPath, ModuleIdent};
use crate::models::{
    code::{Module, ModulePath},
    data::{DataType, PathData},
    TypeIdent,
};

use super::MetaData;

/// Context for the rendering process.
///
/// This contains different additional information and configuration that may be
/// needed by a [`Renderer`](super::Renderer) to render the actual code. It is
/// also used to collect the rendered code and add it to the corresponding module.
#[derive(Debug)]
pub struct Context<'a, 'types> {
    /// Meta information for the rendering process.
    pub meta: &'a MetaData<'types>,

    /// Data type that needs to be rendered.
    pub data: &'a DataType<'types>,

    /// Identifier of the data type that needs to be rendered.
    pub ident: &'a TypeIdent,

    /// Generic data storage for any type that implements [`ValueKey`].
    pub values: Values,

    module: Mutex<&'a mut Module>,

    module_path: ModulePath,
    serialize_module_path: ModulePath,
    deserialize_module_path: ModulePath,
}

/// Cache that can store any value.
///
/// It uses a key type to identify the value inserted to the cache. The key type
/// needs to implement [`ValueKey`].
#[derive(Default, Debug)]
pub struct Values(HashMap<TypeId, Box<dyn Any>>);

/// Trait that represents a key of a certain type that can be stored in the
/// [`Values`] cache.
pub trait ValueKey: Any {
    /// Actual value type that is stored in the [`Values`] cache.
    ///
    /// You can also use `Self` as type if you don't want to make use of a
    /// separate key type.
    type Type: Any;
}

impl<'a, 'types> Context<'a, 'types> {
    /// Resolves the passed `ident` relative to the module of the current rendered type.
    pub fn resolve_type_for_module(&self, target_type: &PathData) -> TokenStream {
        self.add_usings(&target_type.usings);

        target_type.resolve_relative_to(&self.module_path)
    }

    /// Resolves the passed identifier `path` for build-in types before it can
    /// be rendered.
    ///
    /// If [`GeneratorFlags::BUILD_IN_ABSOLUTE_PATHS`] is set, the path is returned
    /// as it, otherwise it will return the identifier only (without the path).
    ///
    /// # Panics
    ///
    /// Panics if the passed `path` is not a valid [`IdentPath`].
    pub fn resolve_build_in(&self, path: &str) -> IdentPath {
        let path = self.patch_using(Cow::Borrowed(path));
        let path = IdentPath::from_str(&path).unwrap();

        if self.check_generator_flags(GeneratorFlags::BUILD_IN_ABSOLUTE_PATHS) {
            path
        } else {
            let (ident, _path, _absolute) = path.into_parts();

            IdentPath::from_ident(ident)
        }
    }

    /// Resolve the passed identifier `path` before it can be rendered.
    ///
    /// If [`GeneratorFlags::ABSOLUTE_PATHS_INSTEAD_USINGS`] is set this will
    /// returns the `path` as is, otherwise it will add a suitable using
    /// (see [`add_usings`](Context::add_usings)) and returns the identifier
    /// only (without the path).
    pub fn resolve_ident_path(&self, path: &str) -> IdentPath {
        self.resolve_ident_path_impl(path, Self::add_usings)
    }

    /// Add using directives to the module the of the current rendered type.
    pub fn add_usings<I>(&self, usings: I)
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let usings = self.patch_usings(usings);
        let mut root = self.module.lock();
        Self::get_current_module(&self.module_path.0, &mut root).usings(false, usings);
    }

    /// Add using directives to the root module.
    pub fn add_root_usings<I>(&self, usings: I)
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let usings = self.patch_usings(usings);
        self.module.lock().usings(false, usings);
    }

    /// Returns a mutable reference to the main module.
    ///
    /// This might be useful if you need to add code anywhere to the generated
    /// result.
    pub fn main_module(&mut self) -> &mut Module {
        self.module.get_mut()
    }

    /// Returns a mutable reference to the module of the current rendered type.
    pub fn current_module(&mut self) -> &mut Module {
        let root = self.module.get_mut();

        Self::get_current_module(&self.module_path.0, root)
    }

    /// Set a `value` for the specified key `K`.
    pub fn set<K>(&mut self, value: K::Type)
    where
        K: ValueKey,
    {
        self.values.set::<K>(value);
    }

    /// Get the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn get<K>(&self) -> K::Type
    where
        K: ValueKey,
        K::Type: Clone,
    {
        self.values.get::<K>()
    }

    /// Get a reference to the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn get_ref<K>(&self) -> &K::Type
    where
        K: ValueKey,
    {
        self.values.get_ref::<K>()
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn get_mut<K>(&mut self) -> &mut K::Type
    where
        K: ValueKey,
    {
        self.values.get_mut::<K>()
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    /// If no value is available a new one is created.
    pub fn get_or_create<K>(&mut self) -> &mut K::Type
    where
        K: ValueKey,
        K::Type: Default,
    {
        self.values.get_or_create::<K>()
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    /// If no value is available a new one is created with the provided function `f`.
    pub fn get_or_create_with<K, F>(&mut self, f: F) -> &mut K::Type
    where
        K: ValueKey,
        F: FnOnce() -> K::Type,
    {
        self.values.get_or_create_with::<K, _>(f)
    }

    /// Extracts the value stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn extract<K>(&mut self) -> K::Type
    where
        K: ValueKey,
    {
        self.values.extract::<K>()
    }

    /// Removes any values for the specified key `K`.
    pub fn unset<K>(&mut self)
    where
        K: ValueKey,
    {
        self.values.unset::<K>();
    }

    /// Takes an iterator of usings (anything that implements `ToString`) and
    /// executes [`patch_using`](Self::patch_using) for each element.
    pub fn patch_usings<I>(&self, usings: I) -> impl Iterator<Item = String> + use<'_, I>
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let alloc = &self.alloc_crate;
        let xsd_parser_types = &self.xsd_parser_types;

        usings.into_iter().map(move |s| {
            Self::patch_using_impl(alloc, xsd_parser_types, Cow::Owned(s.to_string())).into_owned()
        })
    }

    /// Replaces the `alloc::` and `xsd_parser::` in the path with the configured
    /// name for the `alloc` and `xsd-parser` crate.
    ///
    /// See [`Renderer::alloc_crate`](crate::pipeline::Renderer::alloc_crate) and
    /// [`Renderer::xsd_parser_types`](crate::pipeline::Renderer::xsd_parser_types)
    /// for details.
    ///
    /// This should be used before you add using directives to a module manually.
    /// Normally you should use [`add_usings`](Self::add_usings) which does the
    /// patching automatically, but if you want to add code somewhere in the module
    /// tree, this might be useful.
    pub fn patch_using<'x>(&self, using: Cow<'x, str>) -> Cow<'x, str> {
        Self::patch_using_impl(&self.alloc_crate, &self.xsd_parser_types, using)
    }

    pub(crate) fn resolve_root_ident_path(&self, path: &str) -> IdentPath {
        self.resolve_ident_path_impl(path, Self::add_root_usings)
    }

    pub(crate) fn resolve_type_for_serialize_module(&self, target_type: &PathData) -> TokenStream {
        self.add_quick_xml_serialize_usings(false, &target_type.usings);

        target_type.resolve_relative_to(&self.serialize_module_path)
    }

    pub(crate) fn resolve_type_for_deserialize_module(
        &self,
        target_type: &PathData,
    ) -> TokenStream {
        self.add_quick_xml_deserialize_usings(false, &target_type.usings);

        target_type.resolve_relative_to(&self.deserialize_module_path)
    }

    pub(crate) fn quick_xml_serialize(&mut self) -> &mut Module {
        self.current_module().module_mut("quick_xml_serialize")
    }

    pub(crate) fn quick_xml_deserialize(&mut self) -> &mut Module {
        self.current_module().module_mut("quick_xml_deserialize")
    }

    pub(crate) fn resolve_quick_xml_serialize_ident_path(&self, path: &str) -> IdentPath {
        self.resolve_ident_path_impl(path, |x, path| {
            x.add_quick_xml_serialize_usings(false, path);
        })
    }

    pub(crate) fn resolve_quick_xml_deserialize_ident_path(&self, path: &str) -> IdentPath {
        self.resolve_ident_path_impl(path, |x, path| {
            x.add_quick_xml_deserialize_usings(false, path);
        })
    }

    pub(crate) fn add_quick_xml_serialize_usings<I>(&self, anonymous: bool, usings: I)
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let usings = self.patch_usings(usings);

        let mut root = self.module.lock();
        Self::get_current_module(&self.module_path.0, &mut root)
            .module_mut("quick_xml_serialize")
            .usings(anonymous, usings);
    }

    pub(crate) fn add_quick_xml_deserialize_usings<I>(&self, anonymous: bool, usings: I)
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let usings = self.patch_usings(usings);

        let mut root = self.module.lock();
        Self::get_current_module(&self.module_path.0, &mut root)
            .module_mut("quick_xml_deserialize")
            .usings(anonymous, usings);
    }

    pub(super) fn new(
        meta: &'a MetaData<'types>,
        data: &'a DataType<'types>,
        ident: &'a TypeIdent,
        module: &'a mut Module,
        values: Values,
    ) -> Self {
        let module_ident = ModuleIdent::new(
            meta.types,
            ident,
            meta.check_generator_flags(GeneratorFlags::USE_NAMESPACE_MODULES),
            meta.check_generator_flags(GeneratorFlags::USE_SCHEMA_MODULES),
        );
        let module_path = ModulePath::from_ident(meta.types.meta.types, module_ident);
        let serialize_module_path = module_path
            .clone()
            .join(format_ident!("quick_xml_serialize"));
        let deserialize_module_path = module_path
            .clone()
            .join(format_ident!("quick_xml_deserialize"));

        Self {
            meta,
            data,
            ident,
            module: Mutex::new(module),

            module_path,
            serialize_module_path,
            deserialize_module_path,

            values,
        }
    }

    fn get_current_module<I>(idents: I, root: &mut Module) -> &mut Module
    where
        I: IntoIterator,
        I::Item: ToString,
    {
        let mut module = root;

        for ident in idents {
            module = module.module_mut(ident.to_string());
        }

        module
    }

    fn resolve_ident_path_impl<'x, F>(&self, path: &'x str, add_usings: F) -> IdentPath
    where
        F: FnOnce(&Self, [Cow<'x, str>; 1]),
    {
        let path = self.patch_using(Cow::Borrowed(path));
        let ret = IdentPath::from_str(&path).unwrap();

        if self.check_generator_flags(GeneratorFlags::ABSOLUTE_PATHS_INSTEAD_USINGS) {
            ret
        } else {
            add_usings(self, [path]);
            let (ident, _path, _absolute) = ret.into_parts();

            IdentPath::from_ident(ident)
        }
    }

    fn patch_using_impl<'x>(
        alloc: &Ident2,
        xsd_parser_types: &Ident2,
        using: Cow<'x, str>,
    ) -> Cow<'x, str> {
        if let Some(s) = using.strip_prefix("xsd_parser_types::") {
            Cow::Owned(format!("{xsd_parser_types}::{s}"))
        } else if let Some(s) = using.strip_prefix("::xsd_parser_types::") {
            Cow::Owned(format!("::{xsd_parser_types}::{s}"))
        } else if let Some(s) = using.strip_prefix("alloc::") {
            Cow::Owned(format!("{alloc}::{s}"))
        } else if let Some(s) = using.strip_prefix("::alloc::") {
            Cow::Owned(format!("::{alloc}::{s}"))
        } else {
            using
        }
    }
}

impl<'types> Deref for Context<'_, 'types> {
    type Target = MetaData<'types>;

    fn deref(&self) -> &Self::Target {
        self.meta
    }
}

/* Values */

impl Values {
    /// Create a new empty [`Values`] instance.
    #[must_use]
    pub fn new() -> Self {
        Self::default()
    }

    /// Set a `value` for the specified key `K`.
    pub fn set<K>(&mut self, value: K::Type)
    where
        K: ValueKey,
    {
        self.0.insert(TypeId::of::<K>(), Box::new(value));
    }

    /// Get the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    #[must_use]
    pub fn get<K>(&self) -> K::Type
    where
        K: ValueKey,
        K::Type: Clone,
    {
        self.get_ref::<K>().clone()
    }

    /// Get a reference to the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    #[must_use]
    pub fn get_ref<K>(&self) -> &K::Type
    where
        K: ValueKey,
    {
        self.0
            .get(&TypeId::of::<K>())
            .unwrap()
            .downcast_ref::<K::Type>()
            .unwrap()
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn get_mut<K>(&mut self) -> &mut K::Type
    where
        K: ValueKey,
    {
        self.0
            .get_mut(&TypeId::of::<K>())
            .unwrap()
            .downcast_mut::<K::Type>()
            .unwrap()
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    /// If no value is available a new one is created.
    pub fn get_or_create<K>(&mut self) -> &mut K::Type
    where
        K: ValueKey,
        K::Type: Default,
    {
        self.get_or_create_with::<K, _>(Default::default)
    }

    /// Get a mutable reference to the value that was stored for the specified key `K`.
    /// If no value is available a new one is created with the provided function `f`.
    pub fn get_or_create_with<K, F>(&mut self, f: F) -> &mut K::Type
    where
        K: ValueKey,
        F: FnOnce() -> K::Type,
    {
        match self.0.entry(TypeId::of::<K>()) {
            Entry::Vacant(e) => e.insert(Box::new(f())),
            Entry::Occupied(e) => e.into_mut(),
        }
        .downcast_mut::<K::Type>()
        .unwrap()
    }

    /// Extracts the value stored for the specified key `K`.
    ///
    /// Panics if the key was not set before.
    pub fn extract<K>(&mut self) -> K::Type
    where
        K: ValueKey,
    {
        *self
            .0
            .remove(&TypeId::of::<K>())
            .unwrap()
            .downcast::<K::Type>()
            .unwrap()
    }

    /// Removes any values for the specified key `K`.
    pub fn unset<K>(&mut self)
    where
        K: ValueKey,
    {
        self.0.remove(&TypeId::of::<K>());
    }
}