Skip to main content

moq_e2ee/
epoch.rs

1use std::fmt;
2use std::str::FromStr;
3use std::sync::Arc;
4
5use crate::error::{Error, Result};
6use crate::limits::check_bytes;
7
8/// One publisher instance's scope: a nonempty path segment that no other instance under the credential reuses.
9#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
10pub struct Epoch(Arc<str>);
11
12impl Epoch {
13	/// Mint a fresh epoch: the lowercase text of a UUID version 7, which sorts newest last.
14	pub fn mint() -> Self {
15		Self(uuid::Uuid::now_v7().hyphenated().to_string().into())
16	}
17
18	/// The epoch text, the last segment of the protected broadcast path.
19	pub fn as_str(&self) -> &str {
20		&self.0
21	}
22}
23
24impl FromStr for Epoch {
25	type Err = Error;
26
27	/// Accept an epoch discovered from a path: nonempty, no `/`, at most 65535 bytes.
28	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}