use crate::{InputValue, MainInput, ProgramInput, ProgramState, Record, Registers, State, StateLeaf};
use leo_input::{
files::{File, TableOrSection},
InputParserError,
};
#[derive(Clone, PartialEq, Eq)]
pub struct Input {
name: String,
program_input: ProgramInput,
program_state: ProgramState,
}
impl Default for Input {
fn default() -> Self {
Self {
name: "default".to_owned(),
program_input: ProgramInput::new(),
program_state: ProgramState::new(),
}
}
}
#[allow(clippy::len_without_is_empty)]
impl Input {
pub fn new() -> Self {
Self::default()
}
pub fn empty(&self) -> Self {
let input = self.program_input.empty();
let state = self.program_state.empty();
Self {
name: self.name.clone(),
program_input: input,
program_state: state,
}
}
pub fn len(&self) -> usize {
self.program_input.len() + self.program_state.len()
}
pub fn set_main_input(&mut self, input: MainInput) {
self.program_input.main = input;
}
pub fn parse_input(&mut self, file: File) -> Result<(), InputParserError> {
for entry in file.entries.into_iter() {
match entry {
TableOrSection::Section(section) => {
self.program_input.parse(section)?;
}
TableOrSection::Table(table) => return Err(InputParserError::table(table)),
}
}
Ok(())
}
pub fn parse_state(&mut self, file: File) -> Result<(), InputParserError> {
for entry in file.entries.into_iter() {
match entry {
TableOrSection::Section(section) => return Err(InputParserError::section(section.header)),
TableOrSection::Table(table) => {
self.program_state.parse(table)?;
}
}
}
Ok(())
}
#[allow(clippy::ptr_arg)]
pub fn get(&self, name: &String) -> Option<Option<InputValue>> {
self.program_input.get(name)
}
pub fn get_registers(&self) -> &Registers {
self.program_input.get_registers()
}
pub fn get_record(&self) -> &Record {
self.program_state.get_record()
}
pub fn get_state(&self) -> &State {
self.program_state.get_state()
}
pub fn get_state_leaf(&self) -> &StateLeaf {
self.program_state.get_state_leaf()
}
}