1use 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 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 pub fn len(&self) -> usize {
61 self.program_input.len() + self.program_state.len()
62 }
63
64 pub fn set_main_input(&mut self, input: MainInput) {
66 self.program_input.main = input;
67 }
68
69 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 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 #[allow(clippy::ptr_arg)]
99 pub fn get(&self, name: &String) -> Option<Option<InputValue>> {
100 self.program_input.get(name)
101 }
102
103 pub fn get_registers(&self) -> &Registers {
105 self.program_input.get_registers()
106 }
107
108 pub fn get_record(&self) -> &Record {
110 self.program_state.get_record()
111 }
112
113 pub fn get_state(&self) -> &State {
115 self.program_state.get_state()
116 }
117
118 pub fn get_state_leaf(&self) -> &StateLeaf {
120 self.program_state.get_state_leaf()
121 }
122}