maycoon_theme/
id.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
use std::fmt::{Debug, Display, Formatter};

/// An identifier for a widget. This is not for instantiated widgets, but for the widget types in general.
/// It contains a namespace, which should be the crate name and the id of the widget.
///
/// ```
/// use maycoon_theme::id::WidgetId;
///
/// WidgetId::new("fancy_text_widget", "FancyText");
/// ```
#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
pub struct WidgetId {
    namespace: String,
    id: String,
}

impl WidgetId {
    /// Create a new widget id by a namespace and custom id.
    /// The namespace should be the crate name and the id should be the widget type name.
    ///
    /// Example:
    /// ```
    /// let id = maycoon_theme::id::WidgetId::new("my_crate", "MyWidget");
    /// ```
    pub fn new(namespace: impl ToString, id: impl ToString) -> Self {
        Self {
            namespace: namespace.to_string(),
            id: id.to_string(),
        }
    }

    /// Returns the namespace of the widget id.
    pub fn namespace(&self) -> &str {
        &self.namespace
    }

    /// Returns the actual widget id.
    pub fn id(&self) -> &str {
        &self.id
    }
}

impl Display for WidgetId {
    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
        write!(f, "{}:{}", self.namespace, self.id)
    }
}