use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(from = "String", into = "String")]
#[non_exhaustive]
pub enum ReverificationLevel {
FirstFactor,
SecondFactor,
MultiFactor,
Other(OtherReverificationLevel),
}
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct OtherReverificationLevel(String);
impl OtherReverificationLevel {
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for OtherReverificationLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(&self.0)
}
}
impl ReverificationLevel {
pub fn as_str(&self) -> &str {
match self {
Self::FirstFactor => "first_factor",
Self::SecondFactor => "second_factor",
Self::MultiFactor => "multi_factor",
Self::Other(level) => level.as_str(),
}
}
fn from_known(level: &str) -> Option<Self> {
Some(match level {
"first_factor" => Self::FirstFactor,
"second_factor" => Self::SecondFactor,
"multi_factor" => Self::MultiFactor,
_ => return None,
})
}
}
impl From<&str> for ReverificationLevel {
fn from(level: &str) -> Self {
Self::from_known(level)
.unwrap_or_else(|| Self::Other(OtherReverificationLevel(level.to_owned())))
}
}
impl From<String> for ReverificationLevel {
fn from(level: String) -> Self {
Self::from_known(&level).unwrap_or(Self::Other(OtherReverificationLevel(level)))
}
}
impl From<ReverificationLevel> for String {
fn from(level: ReverificationLevel) -> Self {
level.as_str().to_owned()
}
}
impl std::str::FromStr for ReverificationLevel {
type Err = std::convert::Infallible;
fn from_str(level: &str) -> Result<Self, Self::Err> {
Ok(Self::from(level))
}
}
impl std::fmt::Display for ReverificationLevel {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}