fuel_streams_core/nats/nats_namespace.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75
use std::fmt;
static DEFAULT_NAMESPACE: &str = "fuel";
/// Represents a namespace for NATS subjects and streams.
///
/// # Examples
///
/// ```
/// use fuel_streams_core::nats::NatsNamespace;
///
/// let default_namespace = NatsNamespace::default();
/// assert_eq!(default_namespace.to_string(), "fuel");
///
/// let custom_namespace = NatsNamespace::Custom("my_custom_namespace".to_string());
/// assert_eq!(custom_namespace.to_string(), "my_custom_namespace");
/// ```
#[derive(Debug, Clone, Default)]
pub enum NatsNamespace {
#[default]
Fuel,
Custom(String),
}
impl fmt::Display for NatsNamespace {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
let value = match self {
NatsNamespace::Fuel => DEFAULT_NAMESPACE,
NatsNamespace::Custom(s) => s,
};
write!(f, "{value}")
}
}
impl From<NatsNamespace> for String {
fn from(val: NatsNamespace) -> Self {
val.to_string()
}
}
impl NatsNamespace {
/// Creates a subject name by combining the namespace with the given value.
///
/// # Examples
///
/// ```
/// use fuel_streams_core::nats::NatsNamespace;
///
/// let namespace = NatsNamespace::default();
/// assert_eq!(namespace.subject_name("test"), "fuel.test");
///
/// let custom_namespace = NatsNamespace::Custom("custom".to_string());
/// assert_eq!(custom_namespace.subject_name("test"), "custom.test");
/// ```
pub fn subject_name(&self, val: &str) -> String {
format!("{self}.{}", val)
}
/// Creates a stream name by combining the namespace with the given value.
///
/// # Examples
///
/// ```
/// use fuel_streams_core::nats::NatsNamespace;
///
/// let namespace = NatsNamespace::default();
/// assert_eq!(namespace.stream_name("test"), "fuel_test");
///
/// let custom_namespace = NatsNamespace::Custom("custom".to_string());
/// assert_eq!(custom_namespace.stream_name("test"), "custom_test");
/// ```
pub fn stream_name(&self, val: &str) -> String {
format!("{self}_{val}")
}
}