use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(String);
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct AgentId(String);
pub fn as_session_id(id: &str) -> SessionId {
SessionId(id.to_string())
}
pub fn as_agent_id(id: &str) -> AgentId {
AgentId(id.to_string())
}
const AGENT_ID_PATTERN: &str = r"^a(?:.+-)?[0-9a-f]{16}$";
pub fn to_agent_id(s: &str) -> Option<AgentId> {
let re = regex::Regex::new(AGENT_ID_PATTERN).ok()?;
if re.is_match(s) {
Some(AgentId(s.to_string()))
} else {
None
}
}
impl SessionId {
pub fn inner(&self) -> &str {
&self.0
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl AgentId {
pub fn inner(&self) -> &str {
&self.0
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl std::fmt::Display for SessionId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl std::fmt::Display for AgentId {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<&str> for SessionId {
fn from(s: &str) -> Self {
as_session_id(s)
}
}
impl From<&str> for AgentId {
fn from(s: &str) -> Self {
as_agent_id(s)
}
}