pakery_spake2plus/
error.rs1use core::fmt;
4
5#[derive(Debug)]
7pub enum Spake2PlusError {
8 InvalidPoint,
10 IdentityPoint,
12 ConfirmationFailed,
14 InternalError(&'static str),
16}
17
18impl fmt::Display for Spake2PlusError {
19 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
20 match self {
21 Spake2PlusError::InvalidPoint => write!(f, "invalid point encoding"),
22 Spake2PlusError::IdentityPoint => write!(f, "identity point encountered"),
23 Spake2PlusError::ConfirmationFailed => write!(f, "key confirmation failed"),
24 Spake2PlusError::InternalError(msg) => write!(f, "internal error: {msg}"),
25 }
26 }
27}
28
29#[cfg(feature = "std")]
30impl std::error::Error for Spake2PlusError {}
31
32impl From<pakery_core::PakeError> for Spake2PlusError {
33 fn from(e: pakery_core::PakeError) -> Self {
34 match e {
35 pakery_core::PakeError::InvalidPoint => Spake2PlusError::InvalidPoint,
36 pakery_core::PakeError::IdentityPoint => Spake2PlusError::IdentityPoint,
37 pakery_core::PakeError::ProtocolError(msg) => Spake2PlusError::InternalError(msg),
38 pakery_core::PakeError::InvalidInput(msg) => Spake2PlusError::InternalError(msg),
39 }
40 }
41}
42
43impl From<Spake2PlusError> for pakery_core::PakeError {
44 fn from(e: Spake2PlusError) -> Self {
45 match e {
46 Spake2PlusError::InvalidPoint => pakery_core::PakeError::InvalidPoint,
47 Spake2PlusError::IdentityPoint => pakery_core::PakeError::IdentityPoint,
48 Spake2PlusError::ConfirmationFailed => {
49 pakery_core::PakeError::ProtocolError("key confirmation failed")
50 }
51 Spake2PlusError::InternalError(msg) => pakery_core::PakeError::ProtocolError(msg),
52 }
53 }
54}
55
56#[cfg(test)]
57mod tests {
58 use super::*;
59 use alloc::string::ToString;
60 use pakery_core::PakeError;
61
62 #[test]
63 fn display_output_per_variant() {
64 assert_eq!(
65 Spake2PlusError::InvalidPoint.to_string(),
66 "invalid point encoding"
67 );
68 assert_eq!(
69 Spake2PlusError::IdentityPoint.to_string(),
70 "identity point encountered"
71 );
72 assert_eq!(
73 Spake2PlusError::ConfirmationFailed.to_string(),
74 "key confirmation failed"
75 );
76 assert_eq!(
77 Spake2PlusError::InternalError("kdf").to_string(),
78 "internal error: kdf"
79 );
80 }
81
82 #[test]
83 fn from_pake_error_maps_every_variant() {
84 assert!(matches!(
85 Spake2PlusError::from(PakeError::InvalidPoint),
86 Spake2PlusError::InvalidPoint
87 ));
88 assert!(matches!(
89 Spake2PlusError::from(PakeError::IdentityPoint),
90 Spake2PlusError::IdentityPoint
91 ));
92 assert!(matches!(
93 Spake2PlusError::from(PakeError::ProtocolError("x")),
94 Spake2PlusError::InternalError("x")
95 ));
96 assert!(matches!(
97 Spake2PlusError::from(PakeError::InvalidInput("x")),
98 Spake2PlusError::InternalError("x")
99 ));
100 }
101
102 #[test]
103 fn into_pake_error_maps_every_variant() {
104 assert!(matches!(
105 PakeError::from(Spake2PlusError::InvalidPoint),
106 PakeError::InvalidPoint
107 ));
108 assert!(matches!(
109 PakeError::from(Spake2PlusError::IdentityPoint),
110 PakeError::IdentityPoint
111 ));
112 assert!(matches!(
113 PakeError::from(Spake2PlusError::ConfirmationFailed),
114 PakeError::ProtocolError("key confirmation failed")
115 ));
116 assert!(matches!(
117 PakeError::from(Spake2PlusError::InternalError("x")),
118 PakeError::ProtocolError("x")
119 ));
120 }
121}