mod friend;
mod group;
pub use friend::FriendContact;
pub use group::GroupContact;
use serde::{Deserialize, Serialize};
use std::fmt::Debug;
use strum::{Display, EnumString, IntoStaticStr};
#[derive(
Debug, Default, Clone, PartialEq, EnumString, Display, IntoStaticStr, Deserialize, Serialize,
)]
#[serde(rename_all = "lowercase", tag = "type", content = "field0")]
pub enum Scene {
#[strum(serialize = "group")]
#[default]
Group,
#[strum(serialize = "friend")]
Friend,
}
pub trait Contact: Send + Sync + Debug + Clone {
fn scene(&self) -> Scene;
fn peer(&self) -> &str;
fn name(&self) -> Option<&str>;
fn is_friend(&self) -> bool;
fn is_group(&self) -> bool;
}
#[derive(Debug, Clone, PartialEq, Display, IntoStaticStr, Deserialize, Serialize)]
#[serde(rename_all = "lowercase", tag = "type", content = "field0")]
pub enum ContactType {
#[strum(serialize = "friend")]
Friend(FriendContact),
#[strum(serialize = "group")]
Group(GroupContact),
}
impl Contact for ContactType {
fn scene(&self) -> Scene {
match self {
ContactType::Friend(f) => f.scene(),
ContactType::Group(g) => g.scene(),
}
}
fn peer(&self) -> &str {
match self {
ContactType::Friend(f) => f.peer(),
ContactType::Group(g) => g.peer(),
}
}
fn name(&self) -> Option<&str> {
match self {
ContactType::Friend(f) => f.name(),
ContactType::Group(g) => g.name(),
}
}
fn is_friend(&self) -> bool {
matches!(self, ContactType::Friend(_))
}
fn is_group(&self) -> bool {
matches!(self, ContactType::Group(_))
}
}
impl From<FriendContact> for ContactType {
fn from(contact: FriendContact) -> Self {
ContactType::Friend(contact)
}
}
impl From<GroupContact> for ContactType {
fn from(contact: GroupContact) -> Self {
ContactType::Group(contact)
}
}