#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum Error {
Seed(SeedError),
Entropy(EntropyError),
Fork(ForkError),
}
impl core::fmt::Display for Error {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
Error::Seed(e) => write!(f, "Seed error: {}", e),
Error::Entropy(e) => write!(f, "Entropy error: {}", e),
Error::Fork(e) => write!(f, "Fork error: {}", e),
}
}
}
#[cfg(feature = "std")]
impl std::error::Error for Error {}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum SeedError {
InvalidSize {
expected: usize,
got: usize,
},
AllZeros,
ValidationFailed(&'static str),
}
impl core::fmt::Display for SeedError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
SeedError::InvalidSize { expected, got } => {
write!(f, "Invalid seed size: expected {}, got {}", expected, got)
}
SeedError::AllZeros => write!(f, "Seed contains all zeros (weak seed)"),
SeedError::ValidationFailed(msg) => write!(f, "Seed validation failed: {}", msg),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum EntropyError {
InsufficientEntropy,
ReadFailed(&'static str),
SourceUnavailable,
}
impl core::fmt::Display for EntropyError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
EntropyError::InsufficientEntropy => write!(f, "Insufficient entropy available"),
EntropyError::ReadFailed(msg) => write!(f, "Failed to read entropy: {}", msg),
EntropyError::SourceUnavailable => write!(f, "Entropy source unavailable"),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub enum ForkError {
ReseedFailed(&'static str),
InvalidBlockHash(&'static str),
StateCorrupted,
}
impl core::fmt::Display for ForkError {
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
match self {
ForkError::ReseedFailed(msg) => write!(f, "Reseed failed after fork: {}", msg),
ForkError::InvalidBlockHash(msg) => write!(f, "Invalid block hash: {}", msg),
ForkError::StateCorrupted => write!(f, "Fork detection state corrupted"),
}
}
}
pub type Result<T> = core::result::Result<T, Error>;
impl From<SeedError> for Error {
fn from(err: SeedError) -> Self {
Error::Seed(err)
}
}
impl From<EntropyError> for Error {
fn from(err: EntropyError) -> Self {
Error::Entropy(err)
}
}
impl From<ForkError> for Error {
fn from(err: ForkError) -> Self {
Error::Fork(err)
}
}