1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
use crate::InputValueError;
use leo_ast::{InputValue, Parameter};
use indexmap::IndexMap;
pub fn find_input(
name: String,
parameters: &IndexMap<Parameter, Option<InputValue>>,
) -> Result<InputValue, InputValueError> {
let matched_parameter = parameters
.iter()
.find(|(parameter, _value)| parameter.variable.name.as_ref() == name);
match matched_parameter {
Some((_, Some(value))) => Ok(value.clone()),
_ => Err(InputValueError::MissingParameter(name)),
}
}
pub fn input_to_integer_string(input: InputValue) -> Result<String, InputValueError> {
match input {
InputValue::Integer(_type, string) => Ok(string),
value => Err(InputValueError::ExpectedInteger(value.to_string())),
}
}
pub fn input_to_bytes(input: InputValue) -> Result<Vec<u8>, InputValueError> {
let input_array = match input {
InputValue::Array(values) => values,
value => return Err(InputValueError::ExpectedBytes(value.to_string())),
};
let mut result_vec = Vec::with_capacity(input_array.len());
for input in input_array {
let integer_string = input_to_integer_string(input)?;
let byte = integer_string.parse::<u8>()?;
result_vec.push(byte);
}
Ok(result_vec)
}
pub fn input_to_nested_bytes(input: InputValue) -> Result<Vec<Vec<u8>>, InputValueError> {
let inner_arrays = match input {
InputValue::Array(arrays) => arrays,
value => return Err(InputValueError::ExpectedBytes(value.to_string())),
};
let mut result_vec = Vec::with_capacity(inner_arrays.len());
for input_array in inner_arrays {
let array = input_to_bytes(input_array)?;
result_vec.push(array);
}
Ok(result_vec)
}