use std::{fmt, sync::Arc}; /// The id of the theme that stands for each app's own palette. pub const APP_DEFAULT: &str = "craft.app-default"; /// The suite's default theme: the id apps normally put in their [`crate::AppDefaults`]. pub const SUITE_DEFAULT: &str = "craft.artcraft"; /// Ids with this prefix belong to the built-in themes and [`APP_DEFAULT`]. pub const RESERVED_PREFIX: &str = "craft."; const MAX_LEN: usize = 64; /// A theme's stable identity: `[a-z0-9][a-z0-9._-]{1,63}`. /// /// Renaming a theme keeps its id. Selections and per-app choices refer to themes only by id. #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct ThemeId(Arc); /// Why an id is not valid. #[derive(Clone, Copy, Debug, PartialEq, Eq)] pub enum IdError { /// Shorter than 2 or longer than 64 bytes. Length, /// The first character is not `a-z` or `0-9`. Start, /// A character outside `a-z 0-9 . _ -`. Char(char), /// `craft.` ids are the built-ins'; a user or imported theme can't take one. Reserved, /// The first dot-separated segment is a Windows device name (`con`, `nul`, `aux`, `prn`, /// `com1`-`com9`, `lpt1`-`lpt9`), which can't be a file name there. DeviceName, } /// Windows refuses these as file names, with any extension. const DEVICE_NAMES: [&str; 22] = [ "con", "nul", "aux", "prn", "com1", "com2", "com3", "com4", "com5", "com6", "com7", "com8", "com9", "lpt1", "lpt2", "lpt3", "lpt4", "lpt5", "lpt6", "lpt7", "lpt8", "lpt9", ]; /// The form shared by theme and app ids. fn check_form(s: &str) -> Result<(), IdError> { if !(2..=MAX_LEN).contains(&s.len()) { return Err(IdError::Length); } let mut chars = s.chars(); if !chars.next().is_some_and(|c| c.is_ascii_lowercase() || c.is_ascii_digit()) { return Err(IdError::Start); } if let Some(c) = chars.find(|&c| !(c.is_ascii_lowercase() || c.is_ascii_digit() || matches!(c, '.' | '_' | '-'))) { return Err(IdError::Char(c)); } Ok(()) } /// The form, then the rules for ids users create, which become file names: no reserved `craft.` prefix and no Windows /// device name as the first dot-separated part. fn check_user(s: &str) -> Result<(), IdError> { check_form(s)?; let first = s.split('.').next().unwrap_or_default(); if s.starts_with(RESERVED_PREFIX) { Err(IdError::Reserved) } else if DEVICE_NAMES.iter().any(|d| first.eq_ignore_ascii_case(d)) { Err(IdError::DeviceName) } else { Ok(()) } } /// The id of an icon pack a theme names: the same form as a [`ThemeId`]. `craft.` ids are the built-in sets /// (`craft.lucide`, `craft.phosphor`, `craft.carbon`, `craft.tabler`); other ids name packs in the suite library. #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct IconPackId(Arc); impl IconPackId { /// Checks the form only, as a theme file does: a well-formed id that names no installed pack draws the default /// icons. pub fn parse(s: &str) -> Result { check_form(s)?; Ok(IconPackId(Arc::from(s))) } /// [`IconPackId::parse`], refusing the reserved `craft.` prefix and Windows device names: for packs users create /// or import, whose ids become file names. pub fn parse_user(s: &str) -> Result { check_user(s)?; Ok(IconPackId(Arc::from(s))) } /// The id as text. pub fn as_str(&self) -> &str { &self.0 } /// True for the built-in sets' `craft.` ids. pub fn is_builtin(&self) -> bool { self.0.starts_with(RESERVED_PREFIX) } } impl fmt::Debug for IconPackId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&*self.0, f) } } impl fmt::Display for IconPackId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } /// An app's stable identity, the key of a theme's per-app overrides: the same form as a /// [`ThemeId`], `[a-z0-9][a-z0-9._-]{1,63}`, without the reserved prefix or device-name rules. #[derive(Clone, PartialEq, Eq, Hash, PartialOrd, Ord)] pub struct AppId(Arc); impl AppId { /// Checks the form. pub fn parse(s: &str) -> Result { check_form(s)?; Ok(AppId(Arc::from(s))) } /// The id as text. pub fn as_str(&self) -> &str { &self.0 } } impl fmt::Debug for AppId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&*self.0, f) } } impl fmt::Display for AppId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl ThemeId { /// Checks the form only; [`IdError::Reserved`] comes from [`ThemeId::parse_user`]. pub fn parse(s: &str) -> Result { check_form(s)?; Ok(ThemeId(Arc::from(s))) } /// [`ThemeId::parse`], refusing the reserved `craft.` prefix and Windows device names: for /// themes users create or import, whose ids become file names. pub fn parse_user(s: &str) -> Result { check_user(s)?; Ok(ThemeId(Arc::from(s))) } /// For the crate's own constants, which are valid by construction (a test checks each). pub(crate) fn from_static(s: &'static str) -> Self { ThemeId(Arc::from(s)) } /// The id of the suite's default theme ([`SUITE_DEFAULT`]). pub fn suite_default() -> Self { Self::from_static(SUITE_DEFAULT) } /// The id as text. pub fn as_str(&self) -> &str { &self.0 } /// True for every id in the reserved `craft.` namespace, including [`APP_DEFAULT`]. pub fn is_builtin(&self) -> bool { self.0.starts_with(RESERVED_PREFIX) } /// Whether this is [`APP_DEFAULT`]. pub fn is_app_default(&self) -> bool { &*self.0 == APP_DEFAULT } } impl fmt::Debug for ThemeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { fmt::Debug::fmt(&*self.0, f) } } impl fmt::Display for ThemeId { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { f.write_str(&self.0) } } impl fmt::Display for IdError { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match self { IdError::Length => write!(f, "a theme id has 2 to {MAX_LEN} characters"), IdError::Start => f.write_str("a theme id starts with a-z or 0-9"), IdError::Char(c) => write!(f, "a theme id can't contain {c:?}; use a-z, 0-9, '.', '_' or '-'"), IdError::Reserved => write!(f, "theme ids starting with {RESERVED_PREFIX:?} are reserved for built-in themes"), IdError::DeviceName => f.write_str("a theme id can't start with a Windows device name such as \"con\" or \"lpt1\""), } } } impl std::error::Error for IdError {} impl serde::Serialize for ThemeId { fn serialize(&self, s: S) -> Result { s.serialize_str(&self.0) } } impl<'de> serde::Deserialize<'de> for ThemeId { fn deserialize>(d: D) -> Result { let s = >::deserialize(d)?; ThemeId::parse(&s).map_err(serde::de::Error::custom) } } #[cfg(test)] mod tests { use super::*; #[test] fn windows_device_names_are_refused_for_user_ids() { for bad in ["con", "nul", "aux", "prn", "com1", "com9", "lpt1", "lpt9", "con.dark", "nul.x", "aux.my-theme"] { assert_eq!(ThemeId::parse_user(bad), Err(IdError::DeviceName), "{bad}"); assert!(ThemeId::parse(bad).is_ok(), "{bad}"); } for ok in ["console", "con-dark", "com0", "com10", "lpt", "my.con", "nullish", "auxiliary"] { assert!(ThemeId::parse_user(ok).is_ok(), "{ok}"); } } }