Skip to main content

coil_cms/model/
ids.rs

1use super::*;
2
3macro_rules! token_type {
4    ($name:ident, $field:literal) => {
5        #[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
6        pub struct $name(String);
7
8        impl $name {
9            pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
10                Ok(Self(validate_token($field, value.into())?))
11            }
12        }
13    };
14}
15
16#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
17pub struct PageId(String);
18
19impl PageId {
20    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
21        Ok(Self(validate_token("page_id", value.into())?))
22    }
23
24    pub fn as_str(&self) -> &str {
25        &self.0
26    }
27}
28
29impl fmt::Display for PageId {
30    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
31        f.write_str(&self.0)
32    }
33}
34
35#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36pub struct RevisionId(String);
37
38impl RevisionId {
39    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
40        Ok(Self(validate_token("revision_id", value.into())?))
41    }
42
43    pub fn as_str(&self) -> &str {
44        &self.0
45    }
46}
47
48impl fmt::Display for RevisionId {
49    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
50        f.write_str(&self.0)
51    }
52}
53
54token_type!(NavigationId, "navigation_id");
55
56#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
57pub struct NavigationItemId(String);
58
59impl NavigationItemId {
60    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
61        Ok(Self(validate_token("navigation_item_id", value.into())?))
62    }
63
64    pub fn as_str(&self) -> &str {
65        &self.0
66    }
67}
68
69impl fmt::Display for NavigationItemId {
70    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
71        f.write_str(&self.0)
72    }
73}
74
75#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
76pub struct LocaleCode(String);
77
78impl LocaleCode {
79    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
80        Ok(Self(validate_token("locale", value.into())?))
81    }
82
83    pub fn as_str(&self) -> &str {
84        &self.0
85    }
86}
87
88impl fmt::Display for LocaleCode {
89    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
90        f.write_str(&self.0)
91    }
92}
93
94#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
95pub struct Slug(String);
96
97impl Slug {
98    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
99        Ok(Self(validate_token("slug", value.into())?))
100    }
101
102    pub fn as_str(&self) -> &str {
103        &self.0
104    }
105}
106
107#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
108pub struct TemplateHandle(String);
109
110impl TemplateHandle {
111    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
112        Ok(Self(validate_token("template_handle", value.into())?))
113    }
114}
115
116#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
117pub struct AssetReference(String);
118
119impl AssetReference {
120    pub fn new(value: impl Into<String>) -> Result<Self, CmsModelError> {
121        Ok(Self(validate_token("asset_reference", value.into())?))
122    }
123
124    pub fn as_str(&self) -> &str {
125        &self.0
126    }
127}