1use crate::{
2 AbiType,
3 input_parser::{InputTypecheckingError, InputValue},
4};
5use acvm::{AcirField, FieldElement, acir::native_types::Witness};
6use thiserror::Error;
7
8#[derive(Debug, Error)]
9pub enum InputParserError {
10 #[error("input file is badly formed, could not parse, {0}")]
11 ParseInputMap(String),
12 #[error(
13 "The value passed for parameter `{arg_name}` is invalid:\nExpected witness values to be integers, but `{value}` failed with `{error}`"
14 )]
15 ParseStr { arg_name: String, value: String, error: String },
16 #[error(
17 "The value passed for parameter `{arg_name}` is invalid:\nValue {value} is less than minimum allowed value of {min}"
18 )]
19 InputUnderflowsMinimum { arg_name: String, value: String, min: String },
20 #[error(
21 "The value passed for parameter `{arg_name}` is invalid:\nValue {value} exceeds maximum allowed value of {max}"
22 )]
23 InputOverflowsMaximum { arg_name: String, value: String, max: String },
24 #[error(
25 "The value passed for parameter `{arg_name}` is invalid:\nValue {value} exceeds field modulus. Values must fall within [0, {})",
26 FieldElement::modulus()
27 )]
28 InputExceedsFieldModulus { arg_name: String, value: String },
29 #[error("cannot parse value `{0}` into {1:?}")]
30 AbiTypeMismatch(String, AbiType),
31 #[error("Expected argument `{0}`, but none was found")]
32 MissingArgument(String),
33}
34
35impl From<toml::ser::Error> for InputParserError {
36 fn from(err: toml::ser::Error) -> Self {
37 Self::ParseInputMap(err.to_string())
38 }
39}
40
41impl From<toml::de::Error> for InputParserError {
42 fn from(err: toml::de::Error) -> Self {
43 Self::ParseInputMap(err.to_string())
44 }
45}
46
47impl From<serde_json::Error> for InputParserError {
48 fn from(err: serde_json::Error) -> Self {
49 Self::ParseInputMap(err.to_string())
50 }
51}
52
53#[derive(Debug, Error)]
54pub enum AbiError {
55 #[error("Received parameters not expected by ABI: {0:?}")]
56 UnexpectedParams(Vec<String>),
57 #[error("The value passed for parameter `{}` does not match the specified type:\n{0}", .0.path())]
58 TypeMismatch(#[from] InputTypecheckingError),
59 #[error("ABI expects the parameter `{0}`, but this was not found")]
60 MissingParam(String),
61 #[error(
62 "Could not read witness value at index {witness_index:?} (required for parameter \"{name}\")"
63 )]
64 MissingParamWitnessValue { name: String, witness_index: Witness },
65 #[error(
66 "Attempted to write to witness index {0:?} but it is already initialized to a different value"
67 )]
68 InconsistentWitnessAssignment(Witness),
69 #[error(
70 "The return value is expected to be a {return_type:?} but found incompatible value {value:?}"
71 )]
72 ReturnTypeMismatch { return_type: AbiType, value: InputValue },
73 #[error("No return value is expected but received {0:?}")]
74 UnexpectedReturnValue(InputValue),
75}