From 2372495e1a0acb9ffead7651d8ed36a3bb98a15b Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Wed, 18 Mar 2026 11:19:51 +0800 Subject: Add new protocol crate with basic types and operations --- protocol/src/member/email.rs | 74 ++++++++++++++++++++++++++++++++++++++++++++ protocol/src/member/error.rs | 8 +++++ 2 files changed, 82 insertions(+) create mode 100644 protocol/src/member/email.rs create mode 100644 protocol/src/member/error.rs (limited to 'protocol/src/member') diff --git a/protocol/src/member/email.rs b/protocol/src/member/email.rs new file mode 100644 index 0000000..a3b829f --- /dev/null +++ b/protocol/src/member/email.rs @@ -0,0 +1,74 @@ +use serde::{Deserialize, Deserializer, Serialize, Serializer}; + +use crate::member::error::EMailAddressParseError; +use std::fmt::{Display, Formatter}; + +#[derive(Clone, Debug, Eq)] +pub struct EMailAddress { + account: String, + domain: String, +} + +impl std::hash::Hash for EMailAddress { + fn hash(&self, state: &mut H) { + self.account.hash(state); + self.domain.hash(state); + } +} + +impl PartialEq for EMailAddress { + fn eq(&self, other: &Self) -> bool { + self.account == other.account && self.domain == other.domain + } +} + +impl Display for EMailAddress { + fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { + write!(f, "{}@{}", self.account, self.domain) + } +} + +impl TryFrom<&str> for EMailAddress { + type Error = EMailAddressParseError; + + fn try_from(value: &str) -> Result { + let trimmed_value = value.trim(); + let parts: Vec<&str> = trimmed_value.split('@').collect(); + if parts.len() != 2 { + return Err(EMailAddressParseError::InvalidFormat); + } + let account = parts[0].trim().to_string(); + let domain = parts[1].trim().to_string(); + if account.is_empty() || domain.is_empty() { + return Err(EMailAddressParseError::EmptyPart); + } + Ok(Self { account, domain }) + } +} + +impl TryFrom for EMailAddress { + type Error = EMailAddressParseError; + + fn try_from(value: String) -> Result { + Self::try_from(value.as_str()) + } +} + +impl Serialize for EMailAddress { + fn serialize(&self, serializer: S) -> Result + where + S: Serializer, + { + serializer.serialize_str(&self.to_string()) + } +} + +impl<'de> Deserialize<'de> for EMailAddress { + fn deserialize(deserializer: D) -> Result + where + D: Deserializer<'de>, + { + let s = String::deserialize(deserializer)?; + EMailAddress::try_from(s).map_err(serde::de::Error::custom) + } +} diff --git a/protocol/src/member/error.rs b/protocol/src/member/error.rs new file mode 100644 index 0000000..143329c --- /dev/null +++ b/protocol/src/member/error.rs @@ -0,0 +1,8 @@ +#[derive(Debug, thiserror::Error)] +pub enum EMailAddressParseError { + #[error("Invalid email format")] + InvalidFormat, + + #[error("Account or domain cannot be empty")] + EmptyPart, +} -- cgit