use crate::{constants, error::Error};
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Scheme {
#[cfg(feature = "aags")]
Aags,
#[cfg(feature = "apgs")]
Apgs,
#[cfg(feature = "aasv")]
Aasv,
#[cfg(feature = "apsv")]
Apsv,
#[cfg(feature = "upbc")]
Upbc,
#[cfg(feature = "zrbcx")]
Zrbcx,
#[cfg(feature = "mock")]
Mock1,
#[cfg(feature = "mock")]
Mock2,
#[cfg(feature = "zmock")]
Zmock1,
#[cfg(feature = "legacy")]
Legacy,
}
impl Scheme {
pub fn as_str(&self) -> &'static str {
match self {
#[cfg(feature = "aags")]
Scheme::Aags => "aags",
#[cfg(feature = "apgs")]
Scheme::Apgs => "apgs",
#[cfg(feature = "aasv")]
Scheme::Aasv => "aasv",
#[cfg(feature = "apsv")]
Scheme::Apsv => "apsv",
#[cfg(feature = "upbc")]
Scheme::Upbc => "upbc",
#[cfg(feature = "zrbcx")]
Scheme::Zrbcx => "zrbcx",
#[cfg(feature = "mock")]
Scheme::Mock1 => "mock1",
#[cfg(feature = "mock")]
Scheme::Mock2 => "mock2",
#[cfg(feature = "zmock")]
Scheme::Zmock1 => "zmock1",
#[cfg(feature = "legacy")]
Scheme::Legacy => "legacy",
}
}
pub fn from_str(s: &str) -> Result<Self, Error> {
s.parse()
}
pub fn is_deterministic(&self) -> bool {
match self {
#[cfg(feature = "aags")]
Scheme::Aags => true,
#[cfg(feature = "apgs")]
Scheme::Apgs => false,
#[cfg(feature = "aasv")]
Scheme::Aasv => true,
#[cfg(feature = "apsv")]
Scheme::Apsv => false,
#[cfg(feature = "upbc")]
Scheme::Upbc => false,
#[cfg(feature = "zrbcx")]
Scheme::Zrbcx => true,
#[cfg(feature = "mock")]
Scheme::Mock1 => true,
#[cfg(feature = "mock")]
Scheme::Mock2 => true,
#[cfg(feature = "zmock")]
Scheme::Zmock1 => true,
#[cfg(feature = "legacy")]
Scheme::Legacy => true,
}
}
pub fn is_probabilistic(&self) -> bool {
!self.is_deterministic()
}
pub fn marker(&self) -> [u8; 2] {
match self {
#[cfg(feature = "aags")]
Scheme::Aags => constants::AAGS_MARKER,
#[cfg(feature = "apgs")]
Scheme::Apgs => constants::APGS_MARKER,
#[cfg(feature = "aasv")]
Scheme::Aasv => constants::AASV_MARKER,
#[cfg(feature = "apsv")]
Scheme::Apsv => constants::APSV_MARKER,
#[cfg(feature = "upbc")]
Scheme::Upbc => constants::UPBC_MARKER,
#[cfg(feature = "zrbcx")]
Scheme::Zrbcx => constants::ZRBCX_MARKER,
#[cfg(feature = "mock")]
Scheme::Mock1 => constants::MOCK1_MARKER,
#[cfg(feature = "mock")]
Scheme::Mock2 => constants::MOCK2_MARKER,
#[cfg(feature = "zmock")]
Scheme::Zmock1 => constants::ZMOCK1_MARKER,
#[cfg(feature = "legacy")]
Scheme::Legacy => unreachable!("legacy does not use a scheme marker"),
}
}
#[deprecated(
since = "1.0.0",
note = "Use marker() instead for 2-byte scheme markers"
)]
pub fn byte(&self) -> u8 {
self.marker()[1]
}
}
impl std::str::FromStr for Scheme {
type Err = Error;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
#[cfg(feature = "aags")]
"aags" => Ok(Scheme::Aags),
#[cfg(feature = "apgs")]
"apgs" => Ok(Scheme::Apgs),
#[cfg(feature = "aasv")]
"aasv" => Ok(Scheme::Aasv),
#[cfg(feature = "apsv")]
"apsv" => Ok(Scheme::Apsv),
#[cfg(feature = "upbc")]
"upbc" => Ok(Scheme::Upbc),
#[cfg(feature = "zrbcx")]
"zrbcx" => Ok(Scheme::Zrbcx),
#[cfg(feature = "mock")]
"mock1" => Ok(Scheme::Mock1),
#[cfg(feature = "mock")]
"mock2" => Ok(Scheme::Mock2),
#[cfg(feature = "zmock")]
"zmock1" => Ok(Scheme::Zmock1),
#[cfg(feature = "legacy")]
"legacy" => Ok(Scheme::Legacy),
_ => Err(Error::UnknownScheme),
}
}
}
impl std::fmt::Display for Scheme {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.as_str())
}
}