use serde::{Deserialize, Serialize};
use std::str::FromStr;
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, Serialize, Deserialize, Default)]
pub enum EncryptionPrefix {
#[default]
Enc,
}
impl EncryptionPrefix {
pub fn as_str(&self) -> &'static str {
match self {
EncryptionPrefix::Enc => "enc:",
}
}
pub fn is_prefixed(&self, value: &str) -> bool {
value.starts_with(self.as_str())
}
pub fn strip<'a>(&self, value: &'a str) -> Option<&'a str> {
value.strip_prefix(self.as_str())
}
}
impl std::fmt::Display for EncryptionPrefix {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for EncryptionPrefix {
type Err = ();
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"enc:" => Ok(Self::Enc),
_ => Err(()),
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_encryption_prefix_as_str() {
assert_eq!(EncryptionPrefix::Enc.as_str(), "enc:");
}
#[test]
fn test_encryption_prefix_from_str() {
assert_eq!(
EncryptionPrefix::from_str("enc:"),
Ok(EncryptionPrefix::Enc)
);
assert_eq!(EncryptionPrefix::from_str("invalid"), Err(()));
}
#[test]
fn test_encryption_prefix_is_prefixed() {
assert!(EncryptionPrefix::Enc.is_prefixed("enc:base64data"));
assert!(!EncryptionPrefix::Enc.is_prefixed("plaintext"));
}
#[test]
fn test_encryption_prefix_strip() {
assert_eq!(
EncryptionPrefix::Enc.strip("enc:base64data"),
Some("base64data")
);
assert_eq!(EncryptionPrefix::Enc.strip("plaintext"), None);
}
}