1#[derive(Debug, Clone, PartialEq, Eq)]
5#[non_exhaustive]
6pub enum Error {
7 Seed(SeedError),
9 Entropy(EntropyError),
11 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#[derive(Debug, Clone, PartialEq, Eq)]
30#[non_exhaustive]
31pub enum SeedError {
32 InvalidSize {
34 expected: usize,
36 got: usize,
38 },
39 AllZeros,
41 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#[derive(Debug, Clone, PartialEq, Eq)]
59#[non_exhaustive]
60pub enum EntropyError {
61 InsufficientEntropy,
63 ReadFailed(&'static str),
65 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#[derive(Debug, Clone, PartialEq, Eq)]
81#[non_exhaustive]
82pub enum ForkError {
83 ReseedFailed(&'static str),
85 InvalidBlockHash(&'static str),
87 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
101pub 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}