tablo_core/naming/
panel_id.rs1use std::{fmt, sync::Arc};
4
5#[derive(Clone, PartialEq, Eq, Hash)]
24pub struct PanelId(Arc<str>);
25
26impl PanelId {
27 pub fn new(prefix: &str) -> Self {
30 Self(super::normalize_prefix(prefix).into())
31 }
32}
33
34fn 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}