use thiserror::Error;

#[derive(Debug, Error)]
pub enum Error {
    #[error("hdpki: master seed must be at least 128 bits")]
    MasterSeedTooShort,
    #[error("hdpki: node missing path metadata")]
    MissingPathMetadata,
    #[error("hdpki: path {target:?} not derived from {prefix:?}")]
    InvalidDerivation { target: String, prefix: String },
    #[error(transparent)]
    Path(#[from] PathError),
    #[error("hdpki: ed25519 error: {0}")]
    Ed25519(#[from] ed25519_dalek::SignatureError),
    #[error("hdpki: x509 error: {0}")]
    X509(String),
}

#[derive(Debug, Error, Clone)]
#[error("{message}")]
pub struct PathError {
    message: String,
}

impl PathError {
    pub(crate) fn new(message: impl Into<String>) -> Self {
        Self {
            message: message.into(),
        }
    }

    pub fn message(&self) -> &str {
        &self.message
    }
}

#[derive(Debug, Error)]
pub enum SeedError {
    #[error("hdpki: entropy source failure: {0}")]
    Entropy(#[from] getrandom::Error),
    #[error("bip39: {0}")]
    Bip39(#[from] bip39::Error),
    #[error("bip39: empty seed generated")]
    EmptySeed,
}
