use crate::error::DatError;
use std::fmt::Display;
use std::str::FromStr;

#[repr(u8)]
#[derive(Clone, Copy)]
pub enum SignatureAlgorithm {
    P256,
    P384,
    P521,
}

impl SignatureAlgorithm {
    pub fn to_str(&self) -> &'static str {
        match self {
            SignatureAlgorithm::P256 => "P256",
            SignatureAlgorithm::P384 => "P384",
            SignatureAlgorithm::P521 => "P521",
        }
    }
}

impl FromStr for SignatureAlgorithm {
    type Err = DatError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "P256" => Ok(SignatureAlgorithm::P256),
            "P384" => Ok(SignatureAlgorithm::P384),
            "P521" => Ok(SignatureAlgorithm::P521),
            _ => Err(DatError::UnknownCryptoAlgorithm),
        }
    }
}

impl Display for SignatureAlgorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.to_str())
    }
}
