#[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 Algorithm {
#[inline]
pub(crate) fn to_jwt(self) -> jsonwebtoken::Algorithm {
match self {
Self::HS256 => jsonwebtoken::Algorithm::HS256,
Self::HS384 => jsonwebtoken::Algorithm::HS384,
Self::HS512 => jsonwebtoken::Algorithm::HS512,
Self::ES256 => jsonwebtoken::Algorithm::ES256,
Self::ES384 => jsonwebtoken::Algorithm::ES384,
Self::RS256 => jsonwebtoken::Algorithm::RS256,
Self::RS384 => jsonwebtoken::Algorithm::RS384,
Self::RS512 => jsonwebtoken::Algorithm::RS512,
Self::PS256 => jsonwebtoken::Algorithm::PS256,
Self::PS384 => jsonwebtoken::Algorithm::PS384,
Self::PS512 => jsonwebtoken::Algorithm::PS512,
Self::EdDSA => jsonwebtoken::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!(volga.to_jwt(), jwt);
}
}
#[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);
}
}