#[derive(Debug, Clone, Copy, Eq, PartialEq, Hash)]
#[non_exhaustive]
pub enum Algorithm {
HS256,
HS384,
HS512,
ES256,
ES384,
RS256,
RS384,
RS512,
PS256,
PS384,
PS512,
EdDSA,
}
impl Default for Algorithm {
#[inline]
fn default() -> Self {
Algorithm::HS256
}
}
impl From<Algorithm> for jsonwebtoken::Algorithm {
#[inline]
fn from(value: Algorithm) -> Self {
match value {
Algorithm::HS256 => jsonwebtoken::Algorithm::HS256,
Algorithm::HS384 => jsonwebtoken::Algorithm::HS384,
Algorithm::HS512 => jsonwebtoken::Algorithm::HS512,
Algorithm::ES256 => jsonwebtoken::Algorithm::ES256,
Algorithm::ES384 => jsonwebtoken::Algorithm::ES384,
Algorithm::RS256 => jsonwebtoken::Algorithm::RS256,
Algorithm::RS384 => jsonwebtoken::Algorithm::RS384,
Algorithm::RS512 => jsonwebtoken::Algorithm::RS512,
Algorithm::PS256 => jsonwebtoken::Algorithm::PS256,
Algorithm::PS384 => jsonwebtoken::Algorithm::PS384,
Algorithm::PS512 => jsonwebtoken::Algorithm::PS512,
Algorithm::EdDSA => jsonwebtoken::Algorithm::EdDSA,
}
}
}
impl From<jsonwebtoken::Algorithm> for Algorithm {
#[inline]
fn from(value: jsonwebtoken::Algorithm) -> Self {
match value {
jsonwebtoken::Algorithm::HS256 => Algorithm::HS256,
jsonwebtoken::Algorithm::HS384 => Algorithm::HS384,
jsonwebtoken::Algorithm::HS512 => Algorithm::HS512,
jsonwebtoken::Algorithm::ES256 => Algorithm::ES256,
jsonwebtoken::Algorithm::ES384 => Algorithm::ES384,
jsonwebtoken::Algorithm::RS256 => Algorithm::RS256,
jsonwebtoken::Algorithm::RS384 => Algorithm::RS384,
jsonwebtoken::Algorithm::RS512 => Algorithm::RS512,
jsonwebtoken::Algorithm::PS256 => Algorithm::PS256,
jsonwebtoken::Algorithm::PS384 => Algorithm::PS384,
jsonwebtoken::Algorithm::PS512 => Algorithm::PS512,
jsonwebtoken::Algorithm::EdDSA => Algorithm::EdDSA,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn it_defaults_to_hs256() {
assert_eq!(Algorithm::default(), Algorithm::HS256);
}
#[test]
fn it_converts_every_variant_to_jsonwebtoken() {
let pairs: [(Algorithm, jsonwebtoken::Algorithm); 12] = [
(Algorithm::HS256, jsonwebtoken::Algorithm::HS256),
(Algorithm::HS384, jsonwebtoken::Algorithm::HS384),
(Algorithm::HS512, jsonwebtoken::Algorithm::HS512),
(Algorithm::ES256, jsonwebtoken::Algorithm::ES256),
(Algorithm::ES384, jsonwebtoken::Algorithm::ES384),
(Algorithm::RS256, jsonwebtoken::Algorithm::RS256),
(Algorithm::RS384, jsonwebtoken::Algorithm::RS384),
(Algorithm::RS512, jsonwebtoken::Algorithm::RS512),
(Algorithm::PS256, jsonwebtoken::Algorithm::PS256),
(Algorithm::PS384, jsonwebtoken::Algorithm::PS384),
(Algorithm::PS512, jsonwebtoken::Algorithm::PS512),
(Algorithm::EdDSA, jsonwebtoken::Algorithm::EdDSA),
];
for (volga, jwt) in pairs {
assert_eq!(jsonwebtoken::Algorithm::from(volga), jwt);
assert_eq!(Algorithm::from(jwt), volga);
}
}
#[test]
fn it_debugs_hs256() {
assert_eq!(format!("{:?}", Algorithm::HS256), "HS256");
}
#[test]
fn it_compares_for_equality() {
assert_eq!(Algorithm::RS256, Algorithm::RS256);
assert_ne!(Algorithm::RS256, Algorithm::HS256);
}
}