nectar_primitives/chunk/
type_id.rs1use core::fmt;
7
8#[derive(Clone, Copy, PartialEq, Eq, Hash)]
35pub struct ChunkTypeId(u8);
36
37impl ChunkTypeId {
38 pub const CONTENT: Self = Self(0);
42
43 pub const SINGLE_OWNER: Self = Self(1);
47
48 #[inline]
57 pub const fn new(id: u8) -> Self {
58 Self(id)
59 }
60
61 #[inline]
75 pub const fn custom(id: u8) -> Self {
76 Self(id)
77 }
78
79 #[inline]
81 pub const fn as_u8(self) -> u8 {
82 self.0
83 }
84
85 #[inline]
89 pub const fn is_standard(self) -> bool {
90 self.0 < 128
91 }
92
93 #[inline]
97 pub const fn is_custom(self) -> bool {
98 self.0 >= 128
99 }
100
101 pub const fn name(self) -> Option<&'static str> {
105 match self.0 {
106 0 => Some("content"),
107 1 => Some("single_owner"),
108 _ => None,
109 }
110 }
111
112 pub const fn abbreviation(self) -> Option<&'static str> {
127 match self.0 {
128 0 => Some("CAC"),
129 1 => Some("SOC"),
130 _ => None,
131 }
132 }
133}
134
135impl fmt::Debug for ChunkTypeId {
136 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
137 match self.name() {
138 Some(name) => write!(f, "ChunkTypeId::{}({})", name.to_uppercase(), self.0),
139 None => write!(f, "ChunkTypeId({})", self.0),
140 }
141 }
142}
143
144impl fmt::Display for ChunkTypeId {
145 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
146 match self.name() {
147 Some(name) => write!(f, "{}", name),
148 None => write!(f, "custom({})", self.0),
149 }
150 }
151}
152
153impl From<u8> for ChunkTypeId {
154 #[inline]
155 fn from(id: u8) -> Self {
156 Self(id)
157 }
158}
159
160impl From<ChunkTypeId> for u8 {
161 #[inline]
162 fn from(id: ChunkTypeId) -> Self {
163 id.0
164 }
165}
166
167#[cfg(test)]
168mod tests {
169 use super::*;
170
171 #[test]
172 fn test_constants() {
173 assert_eq!(ChunkTypeId::CONTENT.as_u8(), 0);
174 assert_eq!(ChunkTypeId::SINGLE_OWNER.as_u8(), 1);
175 }
176
177 #[test]
178 fn test_equality() {
179 assert_eq!(ChunkTypeId::CONTENT, ChunkTypeId::new(0));
180 assert_eq!(ChunkTypeId::SINGLE_OWNER, ChunkTypeId::new(1));
181 assert_ne!(ChunkTypeId::CONTENT, ChunkTypeId::SINGLE_OWNER);
182 }
183
184 #[test]
185 fn test_is_standard() {
186 assert!(ChunkTypeId::CONTENT.is_standard());
187 assert!(ChunkTypeId::SINGLE_OWNER.is_standard());
188 assert!(ChunkTypeId::new(127).is_standard());
189 assert!(!ChunkTypeId::new(128).is_standard());
190 assert!(!ChunkTypeId::custom(200).is_standard());
191 }
192
193 #[test]
194 fn test_is_custom() {
195 assert!(!ChunkTypeId::CONTENT.is_custom());
196 assert!(!ChunkTypeId::SINGLE_OWNER.is_custom());
197 assert!(!ChunkTypeId::new(127).is_custom());
198 assert!(ChunkTypeId::new(128).is_custom());
199 assert!(ChunkTypeId::custom(200).is_custom());
200 }
201
202 #[test]
203 fn test_name() {
204 assert_eq!(ChunkTypeId::CONTENT.name(), Some("content"));
205 assert_eq!(ChunkTypeId::SINGLE_OWNER.name(), Some("single_owner"));
206 assert_eq!(ChunkTypeId::new(50).name(), None);
207 assert_eq!(ChunkTypeId::custom(200).name(), None);
208 }
209
210 #[test]
211 fn test_abbreviation() {
212 assert_eq!(ChunkTypeId::CONTENT.abbreviation(), Some("CAC"));
213 assert_eq!(ChunkTypeId::SINGLE_OWNER.abbreviation(), Some("SOC"));
214 assert_eq!(ChunkTypeId::new(50).abbreviation(), None);
215 assert_eq!(ChunkTypeId::custom(200).abbreviation(), None);
216 }
217
218 #[test]
219 fn test_conversions() {
220 let id: ChunkTypeId = 5u8.into();
221 assert_eq!(id.as_u8(), 5);
222
223 let byte: u8 = ChunkTypeId::CONTENT.into();
224 assert_eq!(byte, 0);
225 }
226
227 #[test]
228 fn test_debug_display() {
229 assert_eq!(
230 format!("{:?}", ChunkTypeId::CONTENT),
231 "ChunkTypeId::CONTENT(0)"
232 );
233 assert_eq!(
234 format!("{:?}", ChunkTypeId::SINGLE_OWNER),
235 "ChunkTypeId::SINGLE_OWNER(1)"
236 );
237 assert_eq!(
238 format!("{:?}", ChunkTypeId::custom(200)),
239 "ChunkTypeId(200)"
240 );
241
242 assert_eq!(format!("{}", ChunkTypeId::CONTENT), "content");
243 assert_eq!(format!("{}", ChunkTypeId::SINGLE_OWNER), "single_owner");
244 assert_eq!(format!("{}", ChunkTypeId::custom(200)), "custom(200)");
245 }
246
247 #[test]
248 fn test_hash() {
249 use std::collections::HashSet;
250
251 let mut set = HashSet::new();
252 set.insert(ChunkTypeId::CONTENT);
253 set.insert(ChunkTypeId::SINGLE_OWNER);
254 set.insert(ChunkTypeId::custom(200));
255
256 assert!(set.contains(&ChunkTypeId::CONTENT));
257 assert!(set.contains(&ChunkTypeId::SINGLE_OWNER));
258 assert!(set.contains(&ChunkTypeId::custom(200)));
259 assert!(!set.contains(&ChunkTypeId::custom(201)));
260 }
261}