Skip to main content

pq_oid/
error.rs

1//! Error types for pq-oid.
2
3use core::fmt;
4
5/// Error type for pq-oid operations.
6///
7/// All error variants use `&'static str` to avoid heap allocations on error paths,
8/// which is important for crypto code to prevent timing side channels.
9#[derive(Debug, Clone, Copy, PartialEq, Eq)]
10#[non_exhaustive]
11pub enum Error {
12    /// Unknown algorithm name.
13    UnknownAlgorithm,
14    /// Unknown OID string.
15    UnknownOid,
16    /// Invalid OID format.
17    InvalidOid(&'static str),
18    /// Invalid OID bytes.
19    InvalidOidBytes(&'static str),
20    /// Unknown JOSE algorithm identifier.
21    UnknownJoseAlgorithm,
22    /// Unknown COSE algorithm number.
23    UnknownCoseAlgorithm(i32),
24}
25
26impl fmt::Display for Error {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            Error::UnknownAlgorithm => write!(f, "unknown algorithm"),
30            Error::UnknownOid => write!(f, "unknown OID"),
31            Error::InvalidOid(msg) => write!(f, "invalid OID: {}", msg),
32            Error::InvalidOidBytes(msg) => write!(f, "invalid OID bytes: {}", msg),
33            Error::UnknownJoseAlgorithm => write!(f, "unknown JOSE algorithm"),
34            Error::UnknownCoseAlgorithm(n) => write!(f, "unknown COSE algorithm: {}", n),
35        }
36    }
37}
38
39#[cfg(feature = "std")]
40impl std::error::Error for Error {}
41
42/// Result type for pq-oid operations.
43pub type Result<T> = core::result::Result<T, Error>;