Skip to main content

use_event_name/
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 EventName(String);
8
9impl EventName {
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 EventName {
20    fn as_ref(&self) -> &str {
21        self.as_str()
22    }
23}
24
25impl From<&str> for EventName {
26    fn from(value: &str) -> Self {
27        Self::new(value)
28    }
29}
30
31impl From<String> for EventName {
32    fn from(value: String) -> Self {
33        Self(value)
34    }
35}
36
37impl fmt::Display for EventName {
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::EventName;
46
47    #[test]
48    fn stores_event_name_text() {
49        let name = EventName::new("command.started");
50
51        assert_eq!(name.as_str(), "command.started");
52        assert_eq!(name.to_string(), "command.started");
53    }
54
55    #[test]
56    fn supports_string_conversions() {
57        let from_str = EventName::from("command.started");
58        let from_string = EventName::from(String::from("command.finished"));
59
60        assert_eq!(from_str.as_ref(), "command.started");
61        assert_eq!(from_string.as_str(), "command.finished");
62    }
63}