Skip to main content

kdk_mnemonic/
error.rs

1use core::fmt;
2
3use kdk_entropy::EntropyError;
4
5/// Errors returned by the conversion functions.
6#[derive(Debug)]
7pub enum MnemonicError {
8    /// `W` is not a BIP-0039 word count (12/15/18/21/24) or the
9    /// corresponding `words-<W>` Cargo feature is off.
10    InvalidWordCount(u8),
11
12    /// Entropy length is not enabled by the build's feature set.
13    /// Reachable via [`crate::entropy_to_mnemonic`] for raw
14    /// `SensitiveBytes` paths.
15    InvalidEntropyLength(usize),
16
17    /// Underlying `bip39` crate rejected the input. Stored for
18    /// pattern-matching but NEVER formatted via `Display`/`Debug` —
19    /// upstream impls may include user-supplied bytes.
20    Bip39(bip39::Error),
21
22    /// `kdk-entropy` error propagated from a `*_mnemonic` pipeline.
23    Entropy(EntropyError),
24}
25
26impl fmt::Display for MnemonicError {
27    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28        match self {
29            MnemonicError::InvalidWordCount(w) => {
30                write!(f, "invalid or feature-gated BIP39 word count: {w}")
31            }
32            MnemonicError::InvalidEntropyLength(n) => {
33                write!(
34                    f,
35                    "invalid or feature-gated BIP39 entropy length: {n} bytes"
36                )
37            }
38            MnemonicError::Bip39(_) => write!(f, "bip39 mnemonic conversion failed"),
39            MnemonicError::Entropy(e) => write!(f, "{e}"),
40        }
41    }
42}
43
44impl From<EntropyError> for MnemonicError {
45    fn from(e: EntropyError) -> Self {
46        MnemonicError::Entropy(e)
47    }
48}
49
50impl core::error::Error for MnemonicError {}