Skip to main content

coqui_peer/
error.rs

1use core::fmt;
2use std::io;
3
4/// Failures of ids, key material and the trust store. No message holds key material or a
5/// path.
6#[derive(Debug)]
7#[non_exhaustive]
8pub enum Error {
9    /// A [`PeerId`](crate::PeerId) is malformed.
10    InvalidId,
11    /// An id is an MD5 digest, which is not supported.
12    WeakDigest,
13    /// A digest name is unknown. The variant carries the name; `Display` does not quote it.
14    UnknownDigest(String),
15    /// Stored key material is malformed, too large, or does not hash to its id.
16    Material,
17    /// The peer is not waiting for acceptance.
18    NotWaiting,
19    /// The peer was already accepted.
20    AlreadyAccepted,
21    /// The peer is not accepted, or its key is retiring.
22    NotAccepted,
23    /// The key is not the pending successor of an accepted key.
24    NoSuccessor,
25    /// The key is the pending successor another accepted key announced.
26    SuccessorTaken,
27    /// Nobody proved the successor's key, and the rotation options require it.
28    NotProven,
29    /// No open invitation has this secret: it never existed, was used up or was revoked.
30    NotInvited,
31    /// The invitation or code expired.
32    Expired,
33    /// An option has no meaning: an invitation label that is not one line, zero uses or
34    /// validity, an empty code alphabet, a repeated character, length 0.
35    InvalidOptions,
36    /// A code holds a character outside its alphabet, or has the wrong length.
37    InvalidCode,
38    /// The random source failed.
39    Random,
40    /// A filesystem or stream operation failed.
41    Io(io::Error),
42}
43
44impl fmt::Display for Error {
45    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
46        match self {
47            Self::InvalidId => formatter.write_str("peer id is invalid"),
48            Self::WeakDigest => {
49                formatter.write_str("peer id is an MD5 digest, which is not supported")
50            }
51            Self::UnknownDigest(_) => {
52                formatter.write_str("unknown digest name; supported: sha256, sha384, sha512, sha1")
53            }
54            Self::Material => formatter.write_str("key material is invalid"),
55            Self::NotWaiting => formatter.write_str("peer is not waiting for acceptance"),
56            Self::AlreadyAccepted => formatter.write_str("peer is already accepted"),
57            Self::NotAccepted => formatter.write_str("peer is not accepted or is retiring"),
58            Self::NoSuccessor => formatter.write_str("key is not a pending successor"),
59            Self::NotProven => formatter.write_str("nobody proved the successor's key"),
60            Self::SuccessorTaken => {
61                formatter.write_str("key is the pending successor of another accepted key")
62            }
63            Self::NotInvited => formatter.write_str("no open invitation has this secret"),
64            Self::Expired => formatter.write_str("invitation or code expired"),
65            Self::InvalidOptions => formatter.write_str("an option has no meaning"),
66            Self::InvalidCode => formatter.write_str("code does not fit its alphabet"),
67            Self::Random => formatter.write_str("random source failed"),
68            Self::Io(_) => formatter.write_str("storage or stream operation failed"),
69        }
70    }
71}
72
73impl core::error::Error for Error {
74    fn source(&self) -> Option<&(dyn core::error::Error + 'static)> {
75        match self {
76            Self::Io(error) => Some(error),
77            _ => None,
78        }
79    }
80}