1use std::fmt::{self, Display, Formatter};
4
5#[cfg(feature = "serde")]
7#[derive(Debug)]
8pub enum WireDeserializationError {
9 InvalidWireMod3,
11 InvalidWireModQ(ModQDeserializationError),
13}
14
15#[cfg(feature = "serde")]
17#[derive(Debug)]
18pub enum ModQDeserializationError {
19 BadModulus(u16),
21
22 DigitTooLarge {
24 digit: u16,
26 modulus: u16,
28 },
29
30 InvalidDigitsLength {
32 got: usize,
34 needed: usize,
36 },
37}
38
39#[cfg(feature = "serde")]
44impl Display for WireDeserializationError {
45 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
46 match self {
47 WireDeserializationError::InvalidWireMod3 => {
48 "deserialization of WireMod3 failed: both lsb and msb cannot be set".fmt(f)
49 }
50 WireDeserializationError::InvalidWireModQ(e) => {
51 write!(f, "deserialization of WireModQ failed: {}", e)
52 }
53 }
54 }
55}
56
57#[cfg(feature = "serde")]
58impl Display for ModQDeserializationError {
59 fn fmt(&self, f: &mut Formatter<'_>) -> fmt::Result {
60 match self {
61 ModQDeserializationError::BadModulus(modulus) => {
62 write!(f, "modulus must be at least 2. Got {}", modulus)
63 }
64 ModQDeserializationError::DigitTooLarge { digit, modulus } => {
65 write!(
66 f,
67 "a digit {} is greater than the modulus ({}) ",
68 digit, modulus
69 )
70 }
71 ModQDeserializationError::InvalidDigitsLength { got, needed } => {
72 write!(
73 f,
74 "invalid number of digits. Expected {}, got {}",
75 needed, got
76 )
77 }
78 }
79 }
80}
81
82#[derive(Debug)]
84pub enum CircuitParserError {
85 IoError(std::io::Error),
87 RegexError(regex::Error),
89 ParseIntError,
91 ParseLineError(String),
93 ParseGateError(String),
95}
96
97impl Display for CircuitParserError {
98 fn fmt(&self, f: &mut Formatter) -> fmt::Result {
99 match self {
100 CircuitParserError::IoError(e) => write!(f, "io error: {}", e),
101 CircuitParserError::RegexError(e) => write!(f, "regex error: {}", e),
102 CircuitParserError::ParseIntError => write!(f, "unable to parse integer"),
103 CircuitParserError::ParseLineError(s) => write!(f, "unable to parse line '{}'", s),
104 CircuitParserError::ParseGateError(s) => write!(f, "unable to parse gate '{}'", s),
105 }
106 }
107}
108
109impl From<std::io::Error> for CircuitParserError {
110 fn from(e: std::io::Error) -> CircuitParserError {
111 CircuitParserError::IoError(e)
112 }
113}
114
115impl From<regex::Error> for CircuitParserError {
116 fn from(e: regex::Error) -> CircuitParserError {
117 CircuitParserError::RegexError(e)
118 }
119}
120
121impl From<std::num::ParseIntError> for CircuitParserError {
122 fn from(_: std::num::ParseIntError) -> CircuitParserError {
123 CircuitParserError::ParseIntError
124 }
125}