xsd_parser/pipeline/renderer/steps/
namespace_const.rs

1use proc_macro2::Literal;
2use quote::quote;
3
4use super::super::{Context, MetaData, Module, RenderStep, RenderStepType};
5
6/// Implements a [`RenderStep`] that renders constants for the different namespaces
7/// used in the schema.
8#[derive(Default, Debug)]
9pub struct NamespaceConstantsRenderStep {
10    using_added: bool,
11}
12
13impl RenderStep for NamespaceConstantsRenderStep {
14    fn render_step_type(&self) -> RenderStepType {
15        RenderStepType::ExtraImpls
16    }
17
18    fn initialize(&mut self, meta: &mut MetaData<'_>) {
19        self.using_added = meta.types.meta.types.modules.is_empty();
20    }
21
22    fn render_type(&mut self, ctx: &mut Context<'_, '_>) {
23        if !self.using_added {
24            self.using_added = true;
25            ctx.add_root_usings(["xsd_parser::models::schema::Namespace"]);
26        }
27    }
28
29    fn finish(&mut self, meta: &MetaData<'_>, module: &mut Module) {
30        let namespace_constants = meta.types.meta.types.modules.values().filter_map(|module| {
31            let ns = module.namespace.as_ref()?;
32            let const_name = module.make_ns_const();
33            let const_name = const_name.path.ident();
34            let namespace = Literal::byte_string(&ns.0);
35
36            Some(quote! {
37                pub const #const_name: Namespace = Namespace::new_const(#namespace);
38            })
39        });
40
41        module.prepend(quote! { #( #namespace_constants )* });
42    }
43}