Skip to main content

use_event_id/
lib.rs

1#![forbid(unsafe_code)]
2#![doc = include_str!("../README.md")]
3
4use core::fmt;
5
6#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
7pub struct EventId(String);
8
9impl EventId {
10    pub fn new(value: impl Into<String>) -> Self {
11        Self(value.into())
12    }
13
14    pub fn as_str(&self) -> &str {
15        &self.0
16    }
17}
18
19impl AsRef<str> for EventId {
20    fn as_ref(&self) -> &str {
21        self.as_str()
22    }
23}
24
25impl From<&str> for EventId {
26    fn from(value: &str) -> Self {
27        Self::new(value)
28    }
29}
30
31impl From<String> for EventId {
32    fn from(value: String) -> Self {
33        Self(value)
34    }
35}
36
37impl fmt::Display for EventId {
38    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
39        formatter.write_str(self.as_str())
40    }
41}
42
43#[cfg(test)]
44mod tests {
45    use super::EventId;
46
47    #[test]
48    fn stores_event_id_text() {
49        let id = EventId::new("evt-001");
50
51        assert_eq!(id.as_str(), "evt-001");
52        assert_eq!(id.to_string(), "evt-001");
53    }
54
55    #[test]
56    fn supports_string_conversions() {
57        let from_str = EventId::from("evt-001");
58        let from_string = EventId::from(String::from("evt-002"));
59
60        assert_eq!(from_str.as_ref(), "evt-001");
61        assert_eq!(from_string.as_str(), "evt-002");
62    }
63}