#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum Algorithm {
EdDSA,
}
impl Algorithm {
pub const ALL: &'static [Algorithm] = &[Algorithm::EdDSA];
pub const fn as_jose_str(self) -> &'static str {
match self {
Algorithm::EdDSA => "EdDSA",
}
}
}
impl std::str::FromStr for Algorithm {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
Algorithm::ALL
.iter()
.copied()
.find(|a| a.as_jose_str() == s)
.ok_or(())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn all_lists_every_algorithm_with_jose_wire_strings() {
assert_eq!(
Algorithm::ALL
.iter()
.map(|a| a.as_jose_str())
.collect::<Vec<_>>(),
["EdDSA"],
);
}
#[test]
fn from_str_round_trips_all_and_rejects_unknown() {
for a in Algorithm::ALL {
assert_eq!(a.as_jose_str().parse::<Algorithm>(), Ok(*a));
}
assert_eq!("HS256".parse::<Algorithm>(), Err(()));
assert_eq!("RS256".parse::<Algorithm>(), Err(()));
assert_eq!("".parse::<Algorithm>(), Err(()));
}
}