Skip to main content

leo_input/types/
array_dimensions.rs

1// Copyright (C) 2019-2021 Aleo Systems Inc.
2// This file is part of the Leo library.
3
4// The Leo library is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8
9// The Leo library is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13
14// You should have received a copy of the GNU General Public License
15// along with the Leo library. If not, see <https://www.gnu.org/licenses/>.
16
17use crate::{ast::Rule, values::PositiveNumber};
18
19use pest::Span;
20use pest_ast::FromPest;
21
22#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
23#[pest_ast(rule(Rule::array_dimensions))]
24pub enum ArrayDimensions<'ast> {
25    Single(Single<'ast>),
26    Multiple(Multiple<'ast>),
27}
28
29#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
30#[pest_ast(rule(Rule::dimension_single))]
31pub struct Single<'ast> {
32    pub number: PositiveNumber<'ast>,
33    #[pest_ast(outer())]
34    pub span: Span<'ast>,
35}
36
37#[derive(Clone, Debug, FromPest, PartialEq, Eq)]
38#[pest_ast(rule(Rule::dimension_multiple))]
39pub struct Multiple<'ast> {
40    pub numbers: Vec<PositiveNumber<'ast>>,
41    #[pest_ast(outer())]
42    pub span: Span<'ast>,
43}
44
45impl<'ast> ArrayDimensions<'ast> {
46    pub fn next_dimension(&self) -> Self {
47        match self {
48            ArrayDimensions::Single(single) => ArrayDimensions::Multiple(Multiple {
49                numbers: vec![],
50                span: single.span.clone(),
51            }),
52            ArrayDimensions::Multiple(multiple) => {
53                let old_dimension = &multiple.numbers;
54
55                ArrayDimensions::Multiple(Multiple {
56                    numbers: old_dimension[1..].to_vec(),
57                    span: multiple.span.clone(),
58                })
59            }
60        }
61    }
62
63    pub fn is_empty(&self) -> bool {
64        match self {
65            ArrayDimensions::Single(_) => false,
66            ArrayDimensions::Multiple(multiple) => multiple.numbers.is_empty(),
67        }
68    }
69}
70
71impl<'ast> std::fmt::Display for ArrayDimensions<'ast> {
72    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
73        match *self {
74            ArrayDimensions::Single(ref single) => write!(f, "{}", single.number),
75            ArrayDimensions::Multiple(ref multiple) => {
76                let string = multiple
77                    .numbers
78                    .iter()
79                    .map(|x| x.value.clone())
80                    .collect::<Vec<_>>()
81                    .join(", ");
82
83                write!(f, "{}", string)
84            }
85        }
86    }
87}