aldrin_parser/ast/
array_len.rs1use super::{LitInt, 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;
10use std::fmt;
11
12#[derive(Debug, Clone)]
13pub struct ArrayLen {
14 span: Span,
15 value: ArrayLenValue,
16}
17
18impl ArrayLen {
19 pub(crate) fn parse(pair: Pair<Rule>) -> Self {
20 assert_eq!(pair.as_rule(), Rule::array_len);
21
22 let span = Span::from_pair(&pair);
23
24 let mut pairs = pair.into_inner();
25 let pair = pairs.next().unwrap();
26 let value = ArrayLenValue::parse(pair);
27
28 Self { span, value }
29 }
30
31 pub(crate) fn validate(&self, validate: &mut Validate) {
32 InvalidArrayLen::validate(self, validate);
33
34 self.value.validate(validate);
35 }
36
37 pub fn span(&self) -> Span {
38 self.span
39 }
40
41 pub fn value(&self) -> &ArrayLenValue {
42 &self.value
43 }
44}
45
46#[derive(Debug, Clone)]
47pub enum ArrayLenValue {
48 Literal(LitInt),
49 Ref(NamedRef),
50}
51
52impl ArrayLenValue {
53 fn parse(pair: Pair<Rule>) -> Self {
54 match pair.as_rule() {
55 Rule::lit_int => Self::Literal(LitInt::parse(pair)),
56 Rule::named_ref => Self::Ref(NamedRef::parse(pair)),
57 _ => unreachable!(),
58 }
59 }
60
61 fn validate(&self, validate: &mut Validate) {
62 match self {
63 Self::Literal(_) => {}
64
65 Self::Ref(ty) => {
66 ConstIntNotFound::validate(ty, validate);
67 ExpectedConstIntFoundService::validate(ty, validate);
68 ExpectedConstIntFoundString::validate(ty, validate);
69 ExpectedConstIntFoundType::validate(ty, validate);
70 ExpectedConstIntFoundUuid::validate(ty, validate);
71
72 ty.validate(validate);
73 }
74 }
75 }
76}
77
78impl fmt::Display for ArrayLenValue {
79 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
80 match self {
81 Self::Literal(lit) => lit.value().fmt(f),
82 Self::Ref(named_ref) => named_ref.kind().fmt(f),
83 }
84 }
85}