leo_input/sections/
header.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    common::Identifier,
20    sections::{Constants, Main, Record, Registers, State, StateLeaf},
21};
22
23use pest::Span;
24use pest_ast::FromPest;
25use std::fmt;
26
27#[derive(Clone, Debug, FromPest, PartialEq)]
28#[pest_ast(rule(Rule::header))]
29pub enum Header<'ast> {
30    Constants(Constants<'ast>),
31    Main(Main<'ast>),
32    Record(Record<'ast>),
33    Registers(Registers<'ast>),
34    State(State<'ast>),
35    StateLeaf(StateLeaf<'ast>),
36    Identifier(Identifier<'ast>),
37}
38
39impl<'ast> Header<'ast> {
40    pub fn span(self) -> Span<'ast> {
41        match self {
42            Header::Constants(constants) => constants.span,
43            Header::Main(main) => main.span,
44            Header::Record(record) => record.span,
45            Header::Registers(registers) => registers.span,
46            Header::State(state) => state.span,
47            Header::StateLeaf(state_leaf) => state_leaf.span,
48            Header::Identifier(identifier) => identifier.span,
49        }
50    }
51}
52
53impl<'ast> fmt::Display for Header<'ast> {
54    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
55        match self {
56            Header::Constants(_constants) => write!(f, "constants"),
57            Header::Main(_main) => write!(f, "main"),
58            Header::Record(_record) => write!(f, "record"),
59            Header::Registers(_registers) => write!(f, "registers"),
60            Header::State(_state) => write!(f, "state"),
61            Header::StateLeaf(_state_leaf) => write!(f, "state_leaf"),
62            Header::Identifier(identifier) => write!(f, "{}", identifier.value),
63        }
64    }
65}