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
//! Collection of custom types.

use crate::Identifier;
#[cfg(feature = "no_std")]
use std::prelude::v1::*;
use std::{any::type_name, collections::BTreeMap};

/// _(internals)_ Information for a registered custom type.
/// Exported under the `internals` feature only.
#[derive(Debug, Eq, PartialEq, Clone, Hash)]
#[non_exhaustive]
pub struct CustomTypeInfo {
    /// Rust name of the custom type.
    pub type_name: Identifier,
    /// Friendly display name of the custom type.
    pub display_name: Identifier,
    /// Comments.
    ///
    /// Block doc-comments are kept in separate strings.
    ///
    /// Line doc-comments are merged, with line-breaks, into a single string without a final
    /// termination line-break.
    ///
    /// Leading white-spaces are stripped, each string always starting with the corresponding
    /// doc-comment leader: `///` or `/**`.
    ///
    /// Each line in non-block doc-comments starts with `///`.
    #[cfg(feature = "metadata")]
    pub comments: crate::StaticVec<crate::SmartString>,
}

/// _(internals)_ A collection of custom types.
/// Exported under the `internals` feature only.
#[derive(Debug, Clone, Hash)]
pub struct CustomTypesCollection(BTreeMap<Identifier, Box<CustomTypeInfo>>);

impl Default for CustomTypesCollection {
    #[inline(always)]
    fn default() -> Self {
        Self::new()
    }
}

impl CustomTypesCollection {
    /// Create a new [`CustomTypesCollection`].
    #[inline(always)]
    pub const fn new() -> Self {
        Self(BTreeMap::new())
    }
    /// Clear the [`CustomTypesCollection`].
    #[inline(always)]
    pub fn clear(&mut self) {
        self.0.clear();
    }
    /// Register a custom type.
    #[inline(always)]
    pub fn add(&mut self, type_name: impl Into<Identifier>, name: impl Into<Identifier>) {
        let type_name = type_name.into();
        let custom_type = CustomTypeInfo {
            type_name: type_name.clone(),
            display_name: name.into(),
            #[cfg(feature = "metadata")]
            comments: <_>::default(),
        };
        self.add_raw(type_name, custom_type);
    }
    /// Register a custom type with doc-comments.
    /// Exported under the `metadata` feature only.
    #[cfg(feature = "metadata")]
    #[inline(always)]
    pub fn add_with_comments<C: Into<crate::SmartString>>(
        &mut self,
        type_name: impl Into<Identifier>,
        name: impl Into<Identifier>,
        comments: impl IntoIterator<Item = C>,
    ) {
        let type_name = type_name.into();
        let custom_type = CustomTypeInfo {
            type_name: type_name.clone(),
            display_name: name.into(),
            comments: comments.into_iter().map(Into::into).collect(),
        };
        self.add_raw(type_name, custom_type);
    }
    /// Register a custom type.
    #[inline(always)]
    pub fn add_type<T>(&mut self, name: &str) {
        self.add_raw(
            type_name::<T>(),
            CustomTypeInfo {
                type_name: type_name::<T>().into(),
                display_name: name.into(),
                #[cfg(feature = "metadata")]
                comments: <_>::default(),
            },
        );
    }
    /// Register a custom type with doc-comments.
    /// Exported under the `metadata` feature only.
    #[cfg(feature = "metadata")]
    #[inline(always)]
    pub fn add_type_with_comments<T>(&mut self, name: &str, comments: &[&str]) {
        self.add_raw(
            type_name::<T>(),
            CustomTypeInfo {
                type_name: type_name::<T>().into(),
                display_name: name.into(),
                #[cfg(feature = "metadata")]
                comments: comments.iter().map(|&s| s.into()).collect(),
            },
        );
    }
    /// Register a custom type.
    #[inline(always)]
    pub fn add_raw(&mut self, type_name: impl Into<Identifier>, custom_type: CustomTypeInfo) {
        self.0.insert(type_name.into(), custom_type.into());
    }
    /// Find a custom type.
    #[inline(always)]
    #[must_use]
    pub fn get(&self, key: &str) -> Option<&CustomTypeInfo> {
        self.0.get(key).map(<_>::as_ref)
    }
    /// Iterate all the custom types.
    #[inline(always)]
    pub fn iter(&self) -> impl Iterator<Item = (&str, &CustomTypeInfo)> {
        self.0.iter().map(|(k, v)| (k.as_str(), v.as_ref()))
    }
}