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/// WidgetId::new("fancy_text_widget", "FancyText");
9/// ```
10#[derive(Debug, Copy, Clone, PartialEq, PartialOrd, Eq, Ord, Hash)]
11pub struct WidgetId {
12    namespace: &'static str,
13    id: &'static str,
14}
15
16impl WidgetId {
17    /// Create a new widget id by a namespace and custom id.
18    /// The namespace should be the crate name and the id should be the widget type name.
19    ///
20    /// Example:
21    /// ```
22    /// # use maycoon_theme::id::WidgetId;
23    /// let id = WidgetId::new("my_crate", "MyWidget");
24    /// ```
25    #[inline(always)]
26    pub const fn new(namespace: &'static str, id: &'static str) -> Self {
27        Self { namespace, id }
28    }
29
30    /// Returns the namespace of the widget id.
31    #[inline(always)]
32    pub const fn namespace(&self) -> &'static str {
33        self.namespace
34    }
35
36    /// Returns the actual widget id.
37    #[inline(always)]
38    pub const fn id(&self) -> &'static str {
39        self.id
40    }
41}
42
43impl Display for WidgetId {
44    #[inline(always)]
45    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
46        // TODO: use const formatting/writing when const format is stable
47        write!(f, "{}:{}", self.namespace, self.id)
48    }
49}