xsd_parser/generator/renderer/
mod.rs

1//! The `renderer` module contains different types that implements the [`Renderer`] trait.
2
3use std::fmt::Debug;
4
5use inflector::Inflector;
6use quote::format_ident;
7
8use crate::{
9    code::{IdentPath, Module},
10    types::Module as TypesModule,
11};
12
13use super::{data::TypeData, Config, Context};
14
15mod defaults;
16mod namespace_const;
17mod quick_xml;
18mod types;
19mod with_namespace_trait;
20
21pub use self::defaults::DefaultsRenderer;
22pub use self::namespace_const::NamespaceConstantsRenderer;
23pub use self::quick_xml::{QuickXmlDeserializeRenderer, QuickXmlSerializeRenderer};
24pub use self::types::TypesRenderer;
25pub use self::with_namespace_trait::WithNamespaceTraitRenderer;
26
27/// Trait that is used to define a renderer.
28///
29/// A renderer is used to generate the actual code of a specific [`TypeData`].
30/// The idea is that different renderers generate different code. This can be
31/// used by the user to compose different renderers depending on his needs, or
32/// he could even implement customized renderers on his own.
33pub trait Renderer: Debug {
34    /// Initialized the renderer.
35    ///
36    /// This is called once for each renderer when the generator is initialized.
37    fn initialize(&mut self, config: &mut Config<'_>) {
38        let _config = config;
39    }
40
41    /// Renders the code for the given type.
42    ///
43    /// This is called once for each type that needs to be rendered.
44    fn render_type(&mut self, ctx: &mut Context<'_, '_>, ty: &TypeData<'_>) {
45        let _cxt = ctx;
46        let _ty = ty;
47    }
48
49    /// Finishes the rendering process.
50    ///
51    /// This is called once for each renderer after the actual rendering of the
52    /// types is finished and the code generation process is being finished.
53    fn finish(&mut self, config: &Config<'_>, module: &mut Module) {
54        let _config = config;
55        let _module = module;
56    }
57}
58
59impl TypesModule {
60    pub(super) fn make_ns_const(&self) -> IdentPath {
61        let ident = format_ident!(
62            "NS_{}",
63            self.name
64                .as_ref()
65                .map_or_else(|| String::from("DEFAULT"), ToString::to_string)
66                .to_screaming_snake_case()
67        );
68
69        IdentPath::from_parts([], ident)
70    }
71}