aldrin_parser/ast/
array_len.rs1use super::{LitPosInt, NamedRef};
2use crate::error::{
3 ConstIntNotFound, ExpectedConstIntFoundService, ExpectedConstIntFoundString,
4 ExpectedConstIntFoundType, ExpectedConstIntFoundUuid, InvalidArrayLen,
5};
6use crate::grammar::Rule;
7use crate::validate::Validate;
8use crate::Span;
9use pest::iterators::Pair;
10
11#[derive(Debug, Clone)]
12pub struct ArrayLen {
13 span: Span,
14 value: ArrayLenValue,
15}
16
17impl ArrayLen {
18 pub(crate) fn parse(pair: Pair<Rule>) -> Self {
19 assert_eq!(pair.as_rule(), Rule::array_len);
20
21 let span = Span::from_pair(&pair);
22
23 let mut pairs = pair.into_inner();
24 let pair = pairs.next().unwrap();
25 let value = ArrayLenValue::parse(pair);
26
27 Self { span, value }
28 }
29
30 pub(crate) fn validate(&self, validate: &mut Validate) {
31 InvalidArrayLen::validate(self, validate);
32
33 self.value.validate(validate);
34 }
35
36 pub fn span(&self) -> Span {
37 self.span
38 }
39
40 pub fn value(&self) -> &ArrayLenValue {
41 &self.value
42 }
43}
44
45#[derive(Debug, Clone)]
46pub enum ArrayLenValue {
47 Literal(LitPosInt),
48 Ref(NamedRef),
49}
50
51impl ArrayLenValue {
52 fn parse(pair: Pair<Rule>) -> Self {
53 match pair.as_rule() {
54 Rule::lit_pos_int => Self::Literal(LitPosInt::parse(pair)),
55 Rule::named_ref => Self::Ref(NamedRef::parse(pair)),
56 _ => unreachable!(),
57 }
58 }
59
60 fn validate(&self, validate: &mut Validate) {
61 match self {
62 Self::Literal(_) => {}
63
64 Self::Ref(ty) => {
65 ConstIntNotFound::validate(ty, validate);
66 ExpectedConstIntFoundService::validate(ty, validate);
67 ExpectedConstIntFoundString::validate(ty, validate);
68 ExpectedConstIntFoundType::validate(ty, validate);
69 ExpectedConstIntFoundUuid::validate(ty, validate);
70
71 ty.validate(validate);
72 }
73 }
74 }
75}