use std::fmt;
use std::str::FromStr;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ThreadId(pub String);
impl ThreadId {
#[must_use]
pub fn new() -> Self {
Self(format!("thread-{}", Uuid::new_v4()))
}
#[must_use]
pub fn from_string(s: impl Into<String>) -> Self {
Self(s.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl Default for ThreadId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for ThreadId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for ThreadId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<ThreadId> for String {
fn from(id: ThreadId) -> Self {
id.0
}
}
impl FromStr for ThreadId {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(transparent)]
pub struct SessionId(pub String);
impl SessionId {
#[must_use]
pub fn new() -> Self {
Self(format!("session-{}", Uuid::new_v4()))
}
#[must_use]
pub fn from_string(s: impl Into<String>) -> Self {
Self(s.into())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl Default for SessionId {
fn default() -> Self {
Self::new()
}
}
impl fmt::Display for SessionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<String> for SessionId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<SessionId> for String {
fn from(id: SessionId) -> Self {
id.0
}
}
impl FromStr for SessionId {
type Err = std::convert::Infallible;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(s.to_string()))
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn thread_id_roundtrip() {
let id = ThreadId::new();
let s = id.to_string();
assert!(s.starts_with("thread-"));
let parsed: ThreadId = s.parse().unwrap();
assert_eq!(parsed.as_str(), id.as_str());
}
#[test]
fn session_id_display() {
let id = SessionId::from_string("session-abc");
assert_eq!(format!("{id}"), "session-abc");
let json = serde_json::to_string(&id).unwrap();
assert_eq!(json, "\"session-abc\"");
let back: SessionId = serde_json::from_str(&json).unwrap();
assert_eq!(back, id);
}
}