clock_rand/
error.rs

1//! Error types for clock-rand
2
3/// Error type for all clock-rand operations
4#[derive(Debug, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7    /// Seed-related error
8    Seed(SeedError),
9    /// Entropy-related error
10    Entropy(EntropyError),
11    /// Fork detection error
12    Fork(ForkError),
13}
14
15impl core::fmt::Display for Error {
16    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
17        match self {
18            Error::Seed(e) => write!(f, "Seed error: {}", e),
19            Error::Entropy(e) => write!(f, "Entropy error: {}", e),
20            Error::Fork(e) => write!(f, "Fork error: {}", e),
21        }
22    }
23}
24
25#[cfg(feature = "std")]
26impl std::error::Error for Error {}
27
28/// Seed-related errors
29#[derive(Debug, Clone, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum SeedError {
32    /// Invalid seed size
33    InvalidSize {
34        /// Expected size in bytes
35        expected: usize,
36        /// Actual size received
37        got: usize,
38    },
39    /// Seed contains all zeros (weak seed)
40    AllZeros,
41    /// Seed validation failed
42    ValidationFailed(&'static str),
43}
44
45impl core::fmt::Display for SeedError {
46    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
47        match self {
48            SeedError::InvalidSize { expected, got } => {
49                write!(f, "Invalid seed size: expected {}, got {}", expected, got)
50            }
51            SeedError::AllZeros => write!(f, "Seed contains all zeros (weak seed)"),
52            SeedError::ValidationFailed(msg) => write!(f, "Seed validation failed: {}", msg),
53        }
54    }
55}
56
57/// Entropy-related errors
58#[derive(Debug, Clone, PartialEq, Eq)]
59#[non_exhaustive]
60pub enum EntropyError {
61    /// Insufficient entropy available
62    InsufficientEntropy,
63    /// Failed to read from entropy source
64    ReadFailed(&'static str),
65    /// Entropy source unavailable
66    SourceUnavailable,
67}
68
69impl core::fmt::Display for EntropyError {
70    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
71        match self {
72            EntropyError::InsufficientEntropy => write!(f, "Insufficient entropy available"),
73            EntropyError::ReadFailed(msg) => write!(f, "Failed to read entropy: {}", msg),
74            EntropyError::SourceUnavailable => write!(f, "Entropy source unavailable"),
75        }
76    }
77}
78
79/// Fork detection errors
80#[derive(Debug, Clone, PartialEq, Eq)]
81#[non_exhaustive]
82pub enum ForkError {
83    /// Fork detected but reseeding failed
84    ReseedFailed(&'static str),
85    /// Invalid block hash format
86    InvalidBlockHash(&'static str),
87    /// Fork detection state corrupted
88    StateCorrupted,
89}
90
91impl core::fmt::Display for ForkError {
92    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
93        match self {
94            ForkError::ReseedFailed(msg) => write!(f, "Reseed failed after fork: {}", msg),
95            ForkError::InvalidBlockHash(msg) => write!(f, "Invalid block hash: {}", msg),
96            ForkError::StateCorrupted => write!(f, "Fork detection state corrupted"),
97        }
98    }
99}
100
101/// Result type alias for clock-rand operations
102pub type Result<T> = core::result::Result<T, Error>;
103
104impl From<SeedError> for Error {
105    fn from(err: SeedError) -> Self {
106        Error::Seed(err)
107    }
108}
109
110impl From<EntropyError> for Error {
111    fn from(err: EntropyError) -> Self {
112        Error::Entropy(err)
113    }
114}
115
116impl From<ForkError> for Error {
117    fn from(err: ForkError) -> Self {
118        Error::Fork(err)
119    }
120}