use-event-target 0.1.0

Lightweight event target primitive for RustUse.
Documentation
#![forbid(unsafe_code)]
#![doc = include_str!("../README.md")]

use core::fmt;

#[derive(Clone, Debug, Default, Eq, Hash, Ord, PartialEq, PartialOrd)]
pub struct EventTarget(String);

impl EventTarget {
    pub fn new(value: impl Into<String>) -> Self {
        Self(value.into())
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }
}

impl AsRef<str> for EventTarget {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl From<&str> for EventTarget {
    fn from(value: &str) -> Self {
        Self::new(value)
    }
}

impl From<String> for EventTarget {
    fn from(value: String) -> Self {
        Self(value)
    }
}

impl fmt::Display for EventTarget {
    fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
        formatter.write_str(self.as_str())
    }
}

#[cfg(test)]
mod tests {
    use super::EventTarget;

    #[test]
    fn stores_event_target_text() {
        let target = EventTarget::new("worker");

        assert_eq!(target.as_str(), "worker");
        assert_eq!(target.to_string(), "worker");
    }

    #[test]
    fn supports_string_conversions() {
        let from_str = EventTarget::from("worker");
        let from_string = EventTarget::from(String::from("audit-log"));

        assert_eq!(from_str.as_ref(), "worker");
        assert_eq!(from_string.as_str(), "audit-log");
    }
}