use serde::{Deserialize, Serialize};
use std::fmt;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub struct SessionId(String);
impl SessionId {
#[must_use]
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
#[must_use]
pub fn generate() -> Self {
use crate::utils::id_generator::IdGenerator;
Self(IdGenerator::new().generate_session_id())
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
#[must_use]
pub fn into_string(self) -> String {
self.0
}
}
impl fmt::Display for SessionId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<String> for SessionId {
fn from(s: String) -> Self {
Self(s)
}
}
impl From<&str> for SessionId {
fn from(s: &str) -> Self {
Self(s.to_string())
}
}
impl AsRef<str> for SessionId {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub struct StepNumber(u64);
impl StepNumber {
#[must_use]
pub fn new(step: u64) -> Self {
Self(step)
}
#[must_use]
pub fn zero() -> Self {
Self(0)
}
#[must_use]
pub fn value(self) -> u64 {
self.0
}
#[must_use]
pub fn next(self) -> Self {
Self(self.0.saturating_add(1))
}
#[must_use]
pub fn is_initial(self) -> bool {
self.0 == 0
}
}
impl fmt::Display for StepNumber {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}", self.0)
}
}
impl From<u64> for StepNumber {
fn from(step: u64) -> Self {
Self(step)
}
}
impl From<StepNumber> for u64 {
fn from(step: StepNumber) -> u64 {
step.0
}
}