Skip to main content

appletheia_application/messaging/
topic_id.rs

1use std::fmt::{self, Display};
2
3use super::TopicIdError;
4
5#[derive(Clone, Debug, Eq, PartialEq, Hash)]
6pub struct TopicId(String);
7
8impl TopicId {
9    pub fn new(value: String) -> Result<Self, TopicIdError> {
10        if value.is_empty() {
11            return Err(TopicIdError::Empty);
12        }
13        Ok(Self(value))
14    }
15
16    pub fn value(&self) -> &str {
17        &self.0
18    }
19}
20
21impl AsRef<str> for TopicId {
22    fn as_ref(&self) -> &str {
23        self.value()
24    }
25}
26
27impl From<TopicId> for String {
28    fn from(value: TopicId) -> Self {
29        value.0
30    }
31}
32
33impl Display for TopicId {
34    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
35        write!(f, "{}", self.value())
36    }
37}
38
39#[cfg(test)]
40mod tests {
41    use super::*;
42
43    #[test]
44    fn new_rejects_empty() {
45        let err = TopicId::new(String::new()).expect_err("empty topic id should be rejected");
46        assert!(matches!(err, TopicIdError::Empty));
47    }
48}