use-event-id 0.1.0

Lightweight event identifier 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 EventId(String);

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

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

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

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

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

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

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

    #[test]
    fn stores_event_id_text() {
        let id = EventId::new("evt-001");

        assert_eq!(id.as_str(), "evt-001");
        assert_eq!(id.to_string(), "evt-001");
    }

    #[test]
    fn supports_string_conversions() {
        let from_str = EventId::from("evt-001");
        let from_string = EventId::from(String::from("evt-002"));

        assert_eq!(from_str.as_ref(), "evt-001");
        assert_eq!(from_string.as_str(), "evt-002");
    }
}