leo_input/values/
value.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::{
18    ast::Rule,
19    values::{BooleanValue, CharValue, FieldValue, GroupValue, IntegerValue, NumberValue},
20};
21
22use crate::values::AddressValue;
23use pest::Span;
24use pest_ast::FromPest;
25use std::fmt;
26
27#[derive(Clone, Debug, FromPest, PartialEq)]
28#[pest_ast(rule(Rule::value))]
29pub enum Value<'ast> {
30    Address(AddressValue<'ast>),
31    Boolean(BooleanValue<'ast>),
32    Char(CharValue<'ast>),
33    Field(FieldValue<'ast>),
34    Group(GroupValue<'ast>),
35    Implicit(NumberValue<'ast>),
36    Integer(IntegerValue<'ast>),
37}
38
39impl<'ast> Value<'ast> {
40    pub fn span(&self) -> &Span<'ast> {
41        match self {
42            Value::Address(value) => &value.span(),
43            Value::Boolean(value) => &value.span,
44            Value::Char(value) => &value.span,
45            Value::Field(value) => &value.span,
46            Value::Group(value) => &value.span,
47            Value::Implicit(value) => &value.span(),
48            Value::Integer(value) => &value.span(),
49        }
50    }
51}
52
53impl<'ast> fmt::Display for Value<'ast> {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        match *self {
56            Value::Address(ref value) => write!(f, "{}", value),
57            Value::Boolean(ref value) => write!(f, "{}", value),
58            Value::Char(ref value) => write!(f, "{}", value),
59            Value::Field(ref value) => write!(f, "{}", value),
60            Value::Group(ref value) => write!(f, "{}", value),
61            Value::Implicit(ref value) => write!(f, "{}", value),
62            Value::Integer(ref value) => write!(f, "{}", value),
63        }
64    }
65}