Skip to main content

tablo_core/naming/
panel_id.rs

1//! [`PanelId`]: which mounted panel a check is about.
2
3use std::{fmt, sync::Arc};
4
5/// A mounted panel's identity: its prefix, normalized once when the panel is declared.
6///
7/// [`PanelUser::can_access_panel`](crate::PanelUser::can_access_panel) receives one, and a
8/// mounted panel's [`PanelHandle::id`](crate::PanelHandle::id) and [`Panel::id`](crate::Panel::id)
9/// give the same one. It compares equal to a prefix in any spelling
10/// [`Panel::new`](crate::Panel::new) accepts, so `"admin"`, `"/admin"` and `"/admin/"` all name
11/// `Panel::new("admin")`, and to nothing else: it exposes no string to match a substring or a
12/// leading segment against.
13///
14/// ```rust
15/// # use tablo_core::PanelId;
16/// let admin = PanelId::new("admin");
17/// assert!(admin == "/admin");
18/// assert!(admin == "admin/");
19/// assert!(admin != "/admin-portal");
20/// assert_eq!(admin, PanelId::new("/admin/"));
21/// assert_eq!(admin.to_string(), "/admin");
22/// ```
23#[derive(Clone, PartialEq, Eq, Hash)]
24pub struct PanelId(Arc<str>);
25
26impl PanelId {
27    /// The panel `Panel::new(prefix)` declares, its prefix spelled as for
28    /// [`Panel::new`](crate::Panel::new): an empty prefix names `/admin`.
29    pub fn new(prefix: &str) -> Self {
30        Self(super::normalize_prefix(prefix).into())
31    }
32}
33
34/// Whether `raw`, spelled as for `Panel::new`, normalizes to `normalized`, without allocating.
35fn names(normalized: &str, raw: &str) -> bool {
36    let trimmed = raw.trim().trim_matches('/');
37    let trimmed = if trimmed.is_empty() { "admin" } else { trimmed };
38    normalized.strip_prefix('/') == Some(trimmed)
39}
40
41impl PartialEq<str> for PanelId {
42    fn eq(&self, other: &str) -> bool {
43        names(&self.0, other)
44    }
45}
46
47impl PartialEq<&str> for PanelId {
48    fn eq(&self, other: &&str) -> bool {
49        names(&self.0, other)
50    }
51}
52
53impl PartialEq<String> for PanelId {
54    fn eq(&self, other: &String) -> bool {
55        names(&self.0, other)
56    }
57}
58
59impl PartialEq<PanelId> for str {
60    fn eq(&self, other: &PanelId) -> bool {
61        other == self
62    }
63}
64
65impl PartialEq<PanelId> for &str {
66    fn eq(&self, other: &PanelId) -> bool {
67        other == *self
68    }
69}
70
71impl PartialEq<PanelId> for String {
72    fn eq(&self, other: &PanelId) -> bool {
73        other == self
74    }
75}
76
77impl fmt::Display for PanelId {
78    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
79        f.write_str(&self.0)
80    }
81}
82
83impl fmt::Debug for PanelId {
84    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
85        f.debug_tuple("PanelId").field(&&*self.0).finish()
86    }
87}
88
89#[cfg(test)]
90mod tests {
91    use super::PanelId;
92
93    #[test]
94    fn a_prefix_in_any_spelling_names_the_panel_and_no_other() {
95        let admin = PanelId::new("admin");
96        for spelling in ["admin", "/admin", "/admin/", " admin "] {
97            assert!(admin == spelling, "{spelling:?} names /admin");
98        }
99        for other in ["/admin-portal", "/adm", "/admin/reports", "portal"] {
100            assert!(admin != other, "{other:?} names another panel");
101        }
102        assert!(PanelId::new("") == "admin", "an empty prefix is /admin");
103        assert!(PanelId::new("a/b") == "/a/b/");
104    }
105}