use crate::core::cipher;
use crate::core::types::{MemberName, PublicKey};
use crate::error::Result;
#[derive(Debug, Clone)]
pub struct Recipient {
name: MemberName,
public_key: PublicKey,
}
impl Recipient {
pub fn new(name: MemberName, public_key: PublicKey) -> Result<Self> {
cipher::parse_recipient(&public_key)?;
Ok(Self { name, public_key })
}
pub fn name(&self) -> &str {
&self.name
}
pub fn public_key(&self) -> &str {
&self.public_key
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_recipient_new_valid() {
let identity = age::x25519::Identity::generate();
let pubkey = identity.to_public().to_string();
let recipient = Recipient::new("alice".to_string(), pubkey.clone());
assert!(recipient.is_ok());
let recipient = recipient.unwrap();
assert_eq!(recipient.name(), "alice");
assert_eq!(recipient.public_key(), &pubkey);
}
#[test]
fn test_recipient_new_invalid_key() {
let result = Recipient::new("bob".to_string(), "not-a-valid-key".to_string());
assert!(result.is_err());
}
#[test]
fn test_recipient_new_empty_name() {
let identity = age::x25519::Identity::generate();
let pubkey = identity.to_public().to_string();
let recipient = Recipient::new("".to_string(), pubkey);
assert!(recipient.is_ok());
}
}