aldrin_parser/ast/
newtype_def.rs

1use super::{Attribute, Comment, DocString, Ident, Prelude, TypeName};
2use crate::error::RecursiveNewtype;
3use crate::grammar::Rule;
4use crate::validate::Validate;
5use crate::warning::{BrokenDocLink, NonCamelCaseNewtype};
6use crate::Span;
7use pest::iterators::Pair;
8
9#[derive(Debug, Clone)]
10pub struct NewtypeDef {
11    span: Span,
12    comment: Vec<Comment>,
13    doc: Vec<DocString>,
14    attrs: Vec<Attribute>,
15    name: Ident,
16    target_type: TypeName,
17}
18
19impl NewtypeDef {
20    pub(crate) fn parse(pair: Pair<Rule>) -> Self {
21        assert_eq!(pair.as_rule(), Rule::newtype_def);
22
23        let span = Span::from_pair(&pair);
24        let mut pairs = pair.into_inner();
25        let mut prelude = Prelude::regular(&mut pairs);
26
27        pairs.next().unwrap(); // Skip keyword.
28
29        let pair = pairs.next().unwrap();
30        let name = Ident::parse(pair);
31
32        pairs.next().unwrap(); // Skip =.
33
34        let target_type_pair = pairs.next().unwrap();
35        let target_type = TypeName::parse(target_type_pair);
36
37        Self {
38            span,
39            comment: prelude.take_comment(),
40            doc: prelude.take_doc(),
41            attrs: prelude.take_attrs(),
42            name,
43            target_type,
44        }
45    }
46
47    pub(crate) fn validate(&self, validate: &mut Validate) {
48        BrokenDocLink::validate(&self.doc, validate);
49        RecursiveNewtype::validate(self, validate);
50        NonCamelCaseNewtype::validate(self, validate);
51
52        self.name.validate(true, validate);
53        self.target_type.validate(false, validate);
54    }
55
56    pub fn span(&self) -> Span {
57        self.span
58    }
59
60    pub fn comment(&self) -> &[Comment] {
61        &self.comment
62    }
63
64    pub fn doc(&self) -> &[DocString] {
65        &self.doc
66    }
67
68    pub fn attributes(&self) -> &[Attribute] {
69        &self.attrs
70    }
71
72    pub fn name(&self) -> &Ident {
73        &self.name
74    }
75
76    pub fn target_type(&self) -> &TypeName {
77        &self.target_type
78    }
79}