curv/arithmetic/
errors.rs

1use std::{error, fmt};
2
3/// Error type returned when conversion from hex to BigInt fails.
4#[derive(Debug)]
5pub struct ParseBigIntError {
6    pub(super) reason: ParseErrorReason,
7    #[allow(dead_code)]
8    pub(super) radix: u32,
9}
10
11#[derive(Debug)]
12pub enum ParseErrorReason {
13    #[cfg(feature = "rust-gmp-kzen")]
14    Gmp(gmp::mpz::ParseMpzError),
15    #[cfg(feature = "num-bigint")]
16    NumBigint,
17}
18
19impl fmt::Display for ParseBigIntError {
20    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
21        match &self.reason {
22            #[cfg(feature = "rust-gmp-kzen")]
23            ParseErrorReason::Gmp(reason) => write!(f, "{}", reason),
24            #[cfg(feature = "num-bigint")]
25            ParseErrorReason::NumBigint => {
26                write!(f, "invalid {}-based number representation", self.radix)
27            }
28        }
29    }
30}
31
32impl error::Error for ParseBigIntError {
33    fn source(&self) -> Option<&(dyn error::Error + 'static)> {
34        match &self.reason {
35            #[cfg(feature = "rust-gmp-kzen")]
36            ParseErrorReason::Gmp(reason) => Some(reason),
37            #[cfg(feature = "num-bigint")]
38            ParseErrorReason::NumBigint => None,
39        }
40    }
41}
42
43/// Error type returned when conversion from BigInt to primitive integer type (u64, i64, etc) fails
44#[derive(Debug)]
45pub struct TryFromBigIntError {
46    pub(super) type_name: &'static str,
47}
48
49impl fmt::Display for TryFromBigIntError {
50    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
51        write!(f, "conversion from BigInt to {} overflowed", self.type_name)
52    }
53}
54
55impl error::Error for TryFromBigIntError {}