Skip to main content

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