use anyhow::Result;
use saorsa_webrtc_core::identity::PeerIdentity;
use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct CommunitasIdentity {
pub four_words: String,
}
impl CommunitasIdentity {
pub fn new(four_words: String) -> Result<Self> {
if !crate::identity::validate_id_words(&four_words) {
return Err(anyhow::anyhow!("Invalid four-word address: {}", four_words));
}
Ok(Self { four_words })
}
pub fn four_words(&self) -> &str {
&self.four_words
}
}
impl PeerIdentity for CommunitasIdentity {
fn to_string_repr(&self) -> String {
self.four_words.clone()
}
fn from_string_repr(s: &str) -> Result<Self> {
Self::new(s.to_string())
}
fn unique_id(&self) -> String {
self.four_words.clone()
}
}
impl fmt::Display for CommunitasIdentity {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.four_words)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_identity_creation() {
let identity = CommunitasIdentity::new("ocean-forest-moon-star".to_string());
assert!(identity.is_ok());
let identity = identity.expect("valid identity");
assert_eq!(identity.four_words(), "ocean-forest-moon-star");
assert_eq!(identity.unique_id(), "ocean-forest-moon-star");
assert_eq!(identity.to_string(), "ocean-forest-moon-star");
}
#[test]
fn test_peer_identity_trait() {
let identity =
CommunitasIdentity::new("ocean-forest-moon-star".to_string()).expect("valid identity");
assert_eq!(identity.to_string_repr(), "ocean-forest-moon-star");
let identity2 = CommunitasIdentity::from_string_repr("ocean-forest-moon-star")
.expect("valid from_string_repr");
assert_eq!(identity, identity2);
assert_eq!(identity.unique_id(), "ocean-forest-moon-star");
}
#[test]
fn test_serialization() {
let identity =
CommunitasIdentity::new("ocean-forest-moon-star".to_string()).expect("valid identity");
let json = serde_json::to_string(&identity).expect("serialize");
let deserialized: CommunitasIdentity = serde_json::from_str(&json).expect("deserialize");
assert_eq!(identity, deserialized);
}
}