use std::fmt;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum DaoKeyPrefix {
Session,
Token,
Captcha,
Saml,
Cred,
Lockout,
BruteForce,
Tenant,
Role,
PermissionCache,
RoleCache,
UserCache,
OAuth2Client,
OAuth2AuthCode,
OAuth2AccessToken,
OAuth2CodeUsed,
Mfa,
PasswordReset,
}
impl DaoKeyPrefix {
pub const fn as_str(&self) -> &'static str {
match self {
Self::Session => "session:",
Self::Token => "token:",
Self::Captcha => "captcha:",
Self::Saml => "saml:",
Self::Cred => "cred:",
Self::Lockout => "lockout:",
Self::BruteForce => "bf:",
Self::Tenant => "tenant:",
Self::Role => "role:",
Self::PermissionCache => "perm:cache:",
Self::RoleCache => "role:cache:",
Self::UserCache => "user:cache:",
Self::OAuth2Client => "oauth2:client:",
Self::OAuth2AuthCode => "oauth2:authcode:",
Self::OAuth2AccessToken => "oauth2:atoken:",
Self::OAuth2CodeUsed => "oauth2:codeused:",
Self::Mfa => "mfa:",
Self::PasswordReset => "pwdreset:",
}
}
pub fn build_key(&self, id: &str) -> String {
debug_assert!(
!id.contains(':'),
"dao_keys::build_key: id must not contain ':' (ambiguous key) — got {:?}",
id
);
format!("{}{}", self.as_str(), id)
}
}
impl fmt::Display for DaoKeyPrefix {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn dao_key_prefix_as_str_returns_correct_string() {
assert_eq!(DaoKeyPrefix::Session.as_str(), "session:");
assert_eq!(DaoKeyPrefix::Token.as_str(), "token:");
assert_eq!(DaoKeyPrefix::Captcha.as_str(), "captcha:");
assert_eq!(DaoKeyPrefix::Saml.as_str(), "saml:");
assert_eq!(DaoKeyPrefix::Cred.as_str(), "cred:");
assert_eq!(DaoKeyPrefix::Lockout.as_str(), "lockout:");
assert_eq!(DaoKeyPrefix::BruteForce.as_str(), "bf:");
assert_eq!(DaoKeyPrefix::Tenant.as_str(), "tenant:");
assert_eq!(DaoKeyPrefix::Role.as_str(), "role:");
assert_eq!(DaoKeyPrefix::PermissionCache.as_str(), "perm:cache:");
assert_eq!(DaoKeyPrefix::RoleCache.as_str(), "role:cache:");
assert_eq!(DaoKeyPrefix::UserCache.as_str(), "user:cache:");
}
#[test]
fn dao_key_prefix_build_key_returns_correct_string() {
assert_eq!(DaoKeyPrefix::Session.build_key("abc"), "session:abc");
assert_eq!(DaoKeyPrefix::Token.build_key("xyz123"), "token:xyz123");
assert_eq!(
DaoKeyPrefix::Captcha.build_key("img_001"),
"captcha:img_001"
);
assert_eq!(DaoKeyPrefix::Cred.build_key("user_pass"), "cred:user_pass");
assert_eq!(
DaoKeyPrefix::BruteForce.build_key("192.168.1.1"),
"bf:192.168.1.1"
);
assert_eq!(
DaoKeyPrefix::PermissionCache.build_key("1001"),
"perm:cache:1001"
);
assert_eq!(DaoKeyPrefix::RoleCache.build_key("1001"), "role:cache:1001");
assert_eq!(DaoKeyPrefix::UserCache.build_key("1001"), "user:cache:1001");
}
#[test]
fn dao_key_prefix_display_matches_as_str() {
assert_eq!(format!("{}", DaoKeyPrefix::Session), "session:");
assert_eq!(format!("{}", DaoKeyPrefix::Token), "token:");
}
#[cfg(debug_assertions)]
#[test]
#[should_panic(expected = "id must not contain ':'")]
fn dao_key_prefix_build_key_rejects_colon_id_in_debug() {
let _ = DaoKeyPrefix::Role.build_key("cache:x");
}
}