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

#[repr(u8)]
#[derive(Clone, Copy)]
pub enum CryptoAlgorithm {
    AES128GCMN,
    AES256GCMN,
}

impl CryptoAlgorithm {
    pub fn to_str(&self) -> &'static str {
        match self {
            CryptoAlgorithm::AES128GCMN => "AES128GCMN",
            CryptoAlgorithm::AES256GCMN => "AES256GCMN",
        }
    }
}

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

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