leo_typed/input/
input.rs

1// Copyright (C) 2019-2020 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::{InputValue, MainInput, ProgramInput, ProgramState, Record, Registers, State, StateLeaf};
18use leo_input::{
19    files::{File, TableOrSection},
20    InputParserError,
21};
22
23#[derive(Clone, PartialEq, Eq)]
24pub struct Input {
25    name: String,
26    program_input: ProgramInput,
27    program_state: ProgramState,
28}
29
30impl Default for Input {
31    fn default() -> Self {
32        Self {
33            name: "default".to_owned(),
34            program_input: ProgramInput::new(),
35            program_state: ProgramState::new(),
36        }
37    }
38}
39
40#[allow(clippy::len_without_is_empty)]
41impl Input {
42    pub fn new() -> Self {
43        Self::default()
44    }
45
46    /// Returns an empty version of this struct with `None` values.
47    /// Called during constraint synthesis to provide private input variables.
48    pub fn empty(&self) -> Self {
49        let input = self.program_input.empty();
50        let state = self.program_state.empty();
51
52        Self {
53            name: self.name.clone(),
54            program_input: input,
55            program_state: state,
56        }
57    }
58
59    /// Returns the number of input variables to pass into the `main` program function
60    pub fn len(&self) -> usize {
61        self.program_input.len() + self.program_state.len()
62    }
63
64    /// Manually set the input variables to the `main` program function
65    pub fn set_main_input(&mut self, input: MainInput) {
66        self.program_input.main = input;
67    }
68
69    /// Parse all input variables included in a file and store them in `self`.
70    pub fn parse_input(&mut self, file: File) -> Result<(), InputParserError> {
71        for entry in file.entries.into_iter() {
72            match entry {
73                TableOrSection::Section(section) => {
74                    self.program_input.parse(section)?;
75                }
76                TableOrSection::Table(table) => return Err(InputParserError::table(table)),
77            }
78        }
79
80        Ok(())
81    }
82
83    /// Parse all input variables included in a file and store them in `self`.
84    pub fn parse_state(&mut self, file: File) -> Result<(), InputParserError> {
85        for entry in file.entries.into_iter() {
86            match entry {
87                TableOrSection::Section(section) => return Err(InputParserError::section(section.header)),
88                TableOrSection::Table(table) => {
89                    self.program_state.parse(table)?;
90                }
91            }
92        }
93
94        Ok(())
95    }
96
97    /// Returns the main function input value with the given `name`
98    #[allow(clippy::ptr_arg)]
99    pub fn get(&self, name: &String) -> Option<Option<InputValue>> {
100        self.program_input.get(name)
101    }
102
103    /// Returns the runtime register input values
104    pub fn get_registers(&self) -> &Registers {
105        self.program_input.get_registers()
106    }
107
108    /// Returns the runtime record input values
109    pub fn get_record(&self) -> &Record {
110        self.program_state.get_record()
111    }
112
113    /// Returns the runtime state input values
114    pub fn get_state(&self) -> &State {
115        self.program_state.get_state()
116    }
117
118    /// Returns the runtime state leaf input values
119    pub fn get_state_leaf(&self) -> &StateLeaf {
120        self.program_state.get_state_leaf()
121    }
122}