named_item/
named.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
crate::ix!();

/// Trait for getting the name of an item.
pub trait Named {
    /// Returns the name associated with `self`. 
    /// We use `Cow` to allow both owned and borrowed strings.
    fn name(&self) -> Cow<'_, str>;
}

/// Trait for setting the name of an item with error handling.
pub trait SetName {
    /// Sets the name of the item. Returns a Result to handle invalid inputs.
    fn set_name(&mut self, name: &str) -> Result<(), NameError>;
}

/// Trait for providing a default name.
pub trait DefaultName {
    /// Returns the default name for an item. `Cow<'static, str>` allows owned or static string.
    fn default_name() -> Cow<'static, str>;
}

/// Macro to create a name with an optional separator.
#[macro_export]
macro_rules! name {
    ($prefix:expr, $suffix:expr, $sep:expr) => {
        &format!("{}{}{}", $prefix, $sep, $suffix)
    };
    ($prefix:expr, $suffix:expr) => {
        &format!("{}.{}", $prefix, $suffix)
    };
}