1use thiserror::Error;
2
3use crate::rq::Representation;
4
5pub type Result<T> = std::result::Result<T, Error>;
7
8#[derive(Debug, Error, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12 #[error("Invalid modulus: modulus {0} should be between 2 and (1 << 62) - 1.")]
14 InvalidModulus(u64),
15
16 #[error("{0}")]
18 Serialization(String),
19
20 #[error("This is the last context.")]
22 NoMoreContext,
23
24 #[error("Invalid context provided.")]
26 InvalidContext,
27
28 #[error("Incorrect representation: got {0:?}, expected {1:?}.")]
30 IncorrectRepresentation(Representation, Representation),
31
32 #[error("Invalid seed: got {0} bytes, expected {1} bytes.")]
34 InvalidSeedSize(usize, usize),
35
36 #[error("{0}")]
39 Default(String),
40}
41
42#[cfg(test)]
43mod tests {
44 use crate::{rq::Representation, Error};
45
46 #[test]
47 fn error_strings() {
48 assert_eq!(
49 Error::InvalidModulus(0).to_string(),
50 "Invalid modulus: modulus 0 should be between 2 and (1 << 62) - 1."
51 );
52 assert_eq!(Error::Serialization("test".to_string()).to_string(), "test");
53 assert_eq!(
54 Error::NoMoreContext.to_string(),
55 "This is the last context."
56 );
57 assert_eq!(
58 Error::InvalidContext.to_string(),
59 "Invalid context provided."
60 );
61 assert_eq!(
62 Error::IncorrectRepresentation(Representation::Ntt, Representation::NttShoup)
63 .to_string(),
64 "Incorrect representation: got Ntt, expected NttShoup."
65 );
66 assert_eq!(
67 Error::InvalidSeedSize(0, 1).to_string(),
68 "Invalid seed: got 0 bytes, expected 1 bytes."
69 );
70 }
71}