Skip to main content

icydb_model/node/
newtype.rs

1use crate::prelude::*;
2
3///
4/// Newtype
5///
6
7#[derive(Clone, Debug, Serialize)]
8pub struct Newtype {
9    def: Def,
10    name: &'static str,
11    item: Item,
12
13    #[serde(skip_serializing_if = "Option::is_none")]
14    default: Option<Arg>,
15
16    ty: Type,
17}
18
19impl Newtype {
20    /// Creates a newtype node from its canonical schema parts.
21    #[must_use]
22    pub const fn new(
23        def: Def,
24        name: &'static str,
25        item: Item,
26        default: Option<Arg>,
27        ty: Type,
28    ) -> Self {
29        Self {
30            def,
31            name,
32            item,
33            default,
34            ty,
35        }
36    }
37
38    /// Returns the definition metadata for this newtype node.
39    #[must_use]
40    pub const fn def(&self) -> &Def {
41        &self.def
42    }
43
44    /// Returns the current declared type name.
45    #[must_use]
46    pub const fn name(&self) -> &'static str {
47        self.name
48    }
49
50    /// Returns the wrapped item descriptor.
51    #[must_use]
52    pub const fn item(&self) -> &Item {
53        &self.item
54    }
55
56    /// Returns the optional default value descriptor.
57    #[must_use]
58    pub const fn default(&self) -> Option<&Arg> {
59        self.default.as_ref()
60    }
61
62    /// Returns the canonical runtime type descriptor.
63    #[must_use]
64    pub const fn ty(&self) -> &Type {
65        &self.ty
66    }
67}
68
69impl MacroNode for Newtype {
70    fn as_any(&self) -> &dyn std::any::Any {
71        self
72    }
73}
74
75impl ValidateNode for Newtype {
76    fn validate(&self) -> Result<(), ErrorTree> {
77        let mut errs = ErrorTree::new();
78        validate_source_name(
79            &mut errs,
80            "newtype",
81            self.name(),
82            icydb_schema::TypeSourceKey::try_new,
83        );
84        errs.result()
85    }
86}
87
88impl VisitableNode for Newtype {
89    fn route_key(&self) -> String {
90        self.def().path()
91    }
92
93    fn drive<V: Visitor>(&self, v: &mut V) {
94        self.def().accept(v);
95        self.item().accept(v);
96        if let Some(node) = self.default() {
97            node.accept(v);
98        }
99        self.ty().accept(v);
100    }
101}