xsd_parser/models/code/
format.rs

1use proc_macro2::Ident as Ident2;
2use quote::format_ident;
3
4use crate::models::Name;
5
6/// Formats the passed `name` as module name.
7#[must_use]
8pub fn format_module_ident(name: &Name) -> Ident2 {
9    format_field_ident(name, None)
10}
11
12/// Formats the passed `name` as field name.
13///
14/// Will format the passed `name` as field name if no `display_name` was passed as well.
15/// If a `display_name` was passed, this one will be formatted as field name instead.
16#[must_use]
17pub fn format_field_ident(name: &Name, display_name: Option<&str>) -> Ident2 {
18    let ident = Name::format_field_name(display_name.unwrap_or(name.as_str()));
19
20    format_ident!("{ident}")
21}
22
23/// Formats the passed `name` as type name.
24///
25/// Will format the passed `name` as type name if no `display_name` was passed as well.
26/// If a `display_name` was passed, this one will be formatted as type name instead.
27#[must_use]
28pub fn format_type_ident(name: &Name, display_name: Option<&str>) -> Ident2 {
29    let ident = Name::format_type_name(display_name.unwrap_or(name.as_str()));
30
31    format_ident!("{ident}")
32}
33
34/// Formats the passed `name` as variant name.
35///
36/// Will format the passed `name` as variant name if no `display_name` was passed as well.
37/// If a `display_name` was passed, this one will be formatted as variant name instead.
38#[must_use]
39pub fn format_variant_ident(name: &Name, display_name: Option<&str>) -> Ident2 {
40    format_type_ident(name, display_name)
41}