maycoon_theme/
id.rs

1use std::fmt::{Debug, Display, Formatter};
2
3/// An identifier for a widget. This is not for instantiated widgets, but for the widget types in general.
4/// It contains a namespace, which should be the crate name and the id of the widget.
5///
6/// ```
7/// use maycoon_theme::id::WidgetId;
8///
9/// WidgetId::new("fancy_text_widget", "FancyText");
10/// ```
11#[derive(Debug, Clone, PartialEq, PartialOrd, Eq, Ord)]
12pub struct WidgetId {
13    namespace: String,
14    id: String,
15}
16
17impl WidgetId {
18    /// Create a new widget id by a namespace and custom id.
19    /// The namespace should be the crate name and the id should be the widget type name.
20    ///
21    /// Example:
22    /// ```
23    /// let id = maycoon_theme::id::WidgetId::new("my_crate", "MyWidget");
24    /// ```
25    pub fn new(namespace: impl ToString, id: impl ToString) -> Self {
26        Self {
27            namespace: namespace.to_string(),
28            id: id.to_string(),
29        }
30    }
31
32    /// Returns the namespace of the widget id.
33    pub fn namespace(&self) -> &str {
34        &self.namespace
35    }
36
37    /// Returns the actual widget id.
38    pub fn id(&self) -> &str {
39        &self.id
40    }
41}
42
43impl Display for WidgetId {
44    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
45        write!(f, "{}:{}", self.namespace, self.id)
46    }
47}