intrepid-model 0.3.0

Manage complex async business logic with ease
Documentation
use std::str::FromStr;

use super::SubscriptionTopic;

/// Subscription names are used to identify streams that subscriptions read from. They
/// are URI shaped, and must be valid URI strings.
#[derive(Clone, Debug)]
pub struct SubscriptionName(String);

impl SubscriptionName {
    /// Create a new subscription name, bypassing the URI check.
    pub fn new(name: impl AsRef<str>) -> Self {
        Self(name.as_ref().to_owned())
    }

    /// Parse a string into a subscription name. This will return an error if the string
    /// is not a valid URI.
    pub fn parse_string(candiate: impl AsRef<str>) -> Result<Self, String> {
        Ok(Self::new(candiate))
    }
}

impl SubscriptionTopic for SubscriptionName {
    fn stream_name(&self) -> SubscriptionName {
        self.clone()
    }
}

impl std::fmt::Display for SubscriptionName {
    fn fmt(&self, formatter: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(formatter, "{}", self.0)
    }
}

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

impl FromStr for SubscriptionName {
    type Err = String;

    fn from_str(candidate: &str) -> Result<Self, Self::Err> {
        Self::parse_string(candidate)
    }
}