cortexfs_channels/
address.rs1use std::fmt;
2
3use serde::{Deserialize, Serialize};
4
5use crate::ChannelError;
6
7#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
9#[serde(transparent)]
10pub struct ChannelId(String);
11
12impl ChannelId {
13 #[must_use]
17 pub fn from_static(value: &'static str) -> Self {
18 Self(value.to_owned())
19 }
20
21 pub fn new(value: impl Into<String>) -> Result<Self, ChannelError> {
22 let value = value.into();
23 if value.is_empty()
24 || value.len() > 64
25 || !value.bytes().all(|byte| {
26 byte.is_ascii_lowercase()
27 || byte.is_ascii_digit()
28 || matches!(byte, b'.' | b'-' | b'_')
29 })
30 {
31 return Err(ChannelError::InvalidValue(value));
32 }
33 Ok(Self(value))
34 }
35
36 #[must_use]
37 pub fn as_str(&self) -> &str {
38 &self.0
39 }
40}
41
42impl fmt::Display for ChannelId {
43 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
44 f.write_str(&self.0)
45 }
46}
47
48#[derive(Clone, Debug, Eq, Ord, PartialEq, PartialOrd, Serialize, Deserialize)]
50#[serde(transparent)]
51pub struct ConversationId(String);
52
53impl ConversationId {
54 pub fn new(value: impl Into<String>) -> Result<Self, ChannelError> {
55 let value = value.into();
56 if value.is_empty() || value.len() > 256 || value.contains('\0') {
57 return Err(ChannelError::InvalidValue(value));
58 }
59 Ok(Self(value))
60 }
61
62 #[must_use]
63 pub fn as_str(&self) -> &str {
64 &self.0
65 }
66}
67
68impl fmt::Display for ConversationId {
69 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
70 f.write_str(&self.0)
71 }
72}