1use std::fmt;
2use std::str::FromStr;
3use std::sync::Arc;
4
5use crate::error::{Error, Result};
6use crate::limits::check_bytes;
7
8#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Epoch(Arc<str>);
11
12impl Epoch {
13 pub fn mint() -> Self {
15 Self(uuid::Uuid::now_v7().hyphenated().to_string().into())
16 }
17
18 pub fn as_str(&self) -> &str {
20 &self.0
21 }
22}
23
24impl FromStr for Epoch {
25 type Err = Error;
26
27 fn from_str(text: &str) -> Result<Self> {
29 if text.is_empty() || text.contains('/') {
30 return Err(Error::Identity);
31 }
32 check_bytes(text.len())?;
33 Ok(Self(text.into()))
34 }
35}
36
37impl fmt::Debug for Epoch {
38 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
39 f.debug_tuple("Epoch").field(&self.as_str()).finish()
40 }
41}
42
43impl fmt::Display for Epoch {
44 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
45 f.write_str(self.as_str())
46 }
47}
48
49impl AsRef<str> for Epoch {
50 fn as_ref(&self) -> &str {
51 self.as_str()
52 }
53}