1#![expect(clippy::cast_possible_truncation)]
14
15#[cfg(test)]
16mod tests;
17
18use crate::MAX_INDEX_FIELDS;
19use icydb_utils::to_snake_case;
20use std::{
21 cmp::Ordering,
22 fmt::{self, Display},
23};
24
25pub(super) const MAX_ENTITY_NAME_LEN: usize = 64;
30pub(super) const MAX_INDEX_FIELD_NAME_LEN: usize = 64;
31const MAX_INDEX_NAME_PREFIX_LEN: usize = 5;
32const MAX_ENTITY_NAME_SLUG_LEN: usize = (MAX_ENTITY_NAME_LEN * 3) / 2;
33const MAX_INDEX_FIELD_NAME_SLUG_LEN: usize = (MAX_INDEX_FIELD_NAME_LEN * 3) / 2;
34pub(super) const MAX_INDEX_NAME_LEN: usize = MAX_INDEX_NAME_PREFIX_LEN
35 + MAX_ENTITY_NAME_SLUG_LEN
36 + 2
37 + (MAX_INDEX_FIELDS * MAX_INDEX_FIELD_NAME_SLUG_LEN)
38 + (MAX_INDEX_FIELDS - 1);
39const INDEX_NAME_SEGMENT_DELIMITER: u8 = b'|';
40#[derive(Debug)]
46pub enum IdentityDecodeError {
47 InvalidSize,
48
49 InvalidLength,
50
51 NonAscii,
52
53 NonZeroPadding,
54
55 Delimiter,
56}
57
58#[derive(Debug)]
63pub enum EntityNameError {
64 Empty,
65
66 TooLong { len: usize, max: usize },
67
68 NonAscii,
69
70 Delimiter,
71}
72
73#[derive(Debug)]
78pub enum IndexNameError {
79 TooManyFields { len: usize, max: usize },
80
81 NoFields,
82
83 FieldEmpty,
84
85 FieldTooLong { field: String, max: usize },
86
87 FieldNonAscii { field: String },
88
89 FieldDelimiter { field: String },
90
91 TooLong { len: usize, max: usize },
92}
93
94#[derive(Clone, Copy, Eq, Hash, PartialEq)]
99pub struct EntityName {
100 len: u8,
101 bytes: [u8; MAX_ENTITY_NAME_LEN],
102}
103
104impl EntityName {
105 pub const STORED_SIZE_BYTES: u64 = 1 + (MAX_ENTITY_NAME_LEN as u64);
107
108 pub const STORED_SIZE_USIZE: usize = Self::STORED_SIZE_BYTES as usize;
110
111 pub fn try_from_str(name: &str) -> Result<Self, EntityNameError> {
113 let bytes = name.as_bytes();
115 let len = bytes.len();
116
117 if len == 0 {
118 return Err(EntityNameError::Empty);
119 }
120 if len > MAX_ENTITY_NAME_LEN {
121 return Err(EntityNameError::TooLong {
122 len,
123 max: MAX_ENTITY_NAME_LEN,
124 });
125 }
126 if !bytes.is_ascii() {
127 return Err(EntityNameError::NonAscii);
128 }
129 if bytes.contains(&INDEX_NAME_SEGMENT_DELIMITER) {
130 return Err(EntityNameError::Delimiter);
131 }
132
133 let mut out = [0u8; MAX_ENTITY_NAME_LEN];
135 out[..len].copy_from_slice(bytes);
136
137 Ok(Self {
138 len: len as u8,
139 bytes: out,
140 })
141 }
142
143 #[must_use]
145 pub const fn len(&self) -> usize {
146 self.len as usize
147 }
148
149 #[must_use]
151 pub const fn is_empty(&self) -> bool {
152 self.len() == 0
153 }
154
155 #[must_use]
157 pub fn as_bytes(&self) -> &[u8] {
158 &self.bytes[..self.len()]
159 }
160
161 #[must_use]
163 pub fn as_str(&self) -> &str {
164 std::str::from_utf8(self.as_bytes()).expect("EntityName invariant: ASCII-only storage")
167 }
168
169 #[must_use]
171 pub fn to_bytes(self) -> [u8; Self::STORED_SIZE_USIZE] {
172 let mut out = [0u8; Self::STORED_SIZE_USIZE];
173 out[0] = self.len;
174 out[1..].copy_from_slice(&self.bytes);
175 out
176 }
177
178 pub fn from_bytes(bytes: &[u8]) -> Result<Self, IdentityDecodeError> {
180 if bytes.len() != Self::STORED_SIZE_USIZE {
182 return Err(IdentityDecodeError::InvalidSize);
183 }
184
185 let len = bytes[0] as usize;
186 if len == 0 || len > MAX_ENTITY_NAME_LEN {
187 return Err(IdentityDecodeError::InvalidLength);
188 }
189 if !bytes[1..=len].is_ascii() {
190 return Err(IdentityDecodeError::NonAscii);
191 }
192 if bytes[1..=len].contains(&INDEX_NAME_SEGMENT_DELIMITER) {
193 return Err(IdentityDecodeError::Delimiter);
194 }
195 if bytes[1 + len..].iter().any(|&b| b != 0) {
196 return Err(IdentityDecodeError::NonZeroPadding);
197 }
198
199 let mut name = [0u8; MAX_ENTITY_NAME_LEN];
201 name.copy_from_slice(&bytes[1..]);
202
203 Ok(Self {
204 len: len as u8,
205 bytes: name,
206 })
207 }
208}
209
210impl Ord for EntityName {
211 fn cmp(&self, other: &Self) -> Ordering {
212 self.len.cmp(&other.len).then(self.bytes.cmp(&other.bytes))
215 }
216}
217
218impl PartialOrd for EntityName {
219 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
220 Some(self.cmp(other))
221 }
222}
223
224impl Display for EntityName {
225 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
226 f.write_str(self.as_str())
227 }
228}
229
230impl fmt::Debug for EntityName {
231 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
232 write!(f, "EntityName({})", self.as_str())
233 }
234}
235
236#[derive(Clone, Copy, Eq, Hash, PartialEq)]
241pub struct IndexName {
242 len: u16,
243 bytes: [u8; MAX_INDEX_NAME_LEN],
244}
245
246impl IndexName {
247 pub const STORED_SIZE_BYTES: u64 = 2 + (MAX_INDEX_NAME_LEN as u64);
249 pub const STORED_SIZE_USIZE: usize = Self::STORED_SIZE_BYTES as usize;
251
252 pub fn try_from_entity_fields(
255 entity: &EntityName,
256 fields: &[&str],
257 ) -> Result<Self, IndexNameError> {
258 Self::try_from_entity_fields_with_prefix("idx", entity, fields)
259 }
260
261 pub fn try_unique_from_entity_fields(
264 entity: &EntityName,
265 fields: &[&str],
266 ) -> Result<Self, IndexNameError> {
267 Self::try_from_entity_fields_with_prefix("uniq", entity, fields)
268 }
269
270 fn try_from_entity_fields_with_prefix(
271 prefix: &str,
272 entity: &EntityName,
273 fields: &[&str],
274 ) -> Result<Self, IndexNameError> {
275 if fields.is_empty() {
277 return Err(IndexNameError::NoFields);
278 }
279 if fields.len() > MAX_INDEX_FIELDS {
280 return Err(IndexNameError::TooManyFields {
281 len: fields.len(),
282 max: MAX_INDEX_FIELDS,
283 });
284 }
285
286 let mut field_slugs = Vec::with_capacity(fields.len());
287 for field in fields {
288 let field_len = field.len();
289 if field_len == 0 {
290 return Err(IndexNameError::FieldEmpty);
291 }
292 if field_len > MAX_INDEX_FIELD_NAME_LEN {
293 return Err(IndexNameError::FieldTooLong {
294 field: (*field).to_string(),
295 max: MAX_INDEX_FIELD_NAME_LEN,
296 });
297 }
298 if !field.is_ascii() {
299 return Err(IndexNameError::FieldNonAscii {
300 field: (*field).to_string(),
301 });
302 }
303 if field.as_bytes().contains(&INDEX_NAME_SEGMENT_DELIMITER) {
304 return Err(IndexNameError::FieldDelimiter {
305 field: (*field).to_string(),
306 });
307 }
308 let slug = index_name_slug(field);
309 if slug.is_empty() {
310 return Err(IndexNameError::FieldEmpty);
311 }
312 field_slugs.push(slug);
313 }
314
315 let entity_slug = index_name_slug(entity.as_str());
316 let total_len = prefix
317 .len()
318 .saturating_add(1)
319 .saturating_add(entity_slug.len())
320 .saturating_add(2)
321 .saturating_add(field_slugs.iter().map(String::len).sum::<usize>())
322 .saturating_add(field_slugs.len().saturating_sub(1));
323 if total_len > MAX_INDEX_NAME_LEN {
324 return Err(IndexNameError::TooLong {
325 len: total_len,
326 max: MAX_INDEX_NAME_LEN,
327 });
328 }
329
330 let mut out = [0u8; MAX_INDEX_NAME_LEN];
332 let mut len = 0usize;
333
334 Self::push_bytes(&mut out, &mut len, prefix.as_bytes());
335 Self::push_bytes(&mut out, &mut len, b"_");
336 Self::push_bytes(&mut out, &mut len, entity_slug.as_bytes());
337 Self::push_bytes(&mut out, &mut len, b"__");
338 for (index, field_slug) in field_slugs.iter().enumerate() {
339 if index > 0 {
340 Self::push_bytes(&mut out, &mut len, b"_");
341 }
342 Self::push_bytes(&mut out, &mut len, field_slug.as_bytes());
343 }
344
345 Ok(Self {
346 len: len as u16,
347 bytes: out,
348 })
349 }
350
351 #[must_use]
353 pub fn as_bytes(&self) -> &[u8] {
354 &self.bytes[..self.len as usize]
355 }
356
357 #[must_use]
359 pub fn as_str(&self) -> &str {
360 std::str::from_utf8(self.as_bytes()).expect("IndexName invariant: ASCII-only storage")
363 }
364
365 #[must_use]
367 pub fn to_bytes(self) -> [u8; Self::STORED_SIZE_USIZE] {
368 let mut out = [0u8; Self::STORED_SIZE_USIZE];
369 out[..2].copy_from_slice(&self.len.to_be_bytes());
370 out[2..].copy_from_slice(&self.bytes);
371 out
372 }
373
374 pub fn from_bytes(bytes: &[u8]) -> Result<Self, IdentityDecodeError> {
381 if bytes.len() != Self::STORED_SIZE_USIZE {
383 return Err(IdentityDecodeError::InvalidSize);
384 }
385
386 let len = u16::from_be_bytes([bytes[0], bytes[1]]) as usize;
387 if len == 0 || len > MAX_INDEX_NAME_LEN {
388 return Err(IdentityDecodeError::InvalidLength);
389 }
390 if !bytes[2..2 + len].is_ascii() {
391 return Err(IdentityDecodeError::NonAscii);
392 }
393 if bytes[2 + len..].iter().any(|&b| b != 0) {
394 return Err(IdentityDecodeError::NonZeroPadding);
395 }
396
397 let mut name = [0u8; MAX_INDEX_NAME_LEN];
399 name.copy_from_slice(&bytes[2..]);
400
401 Ok(Self {
402 len: len as u16,
403 bytes: name,
404 })
405 }
406
407 fn push_bytes(out: &mut [u8; MAX_INDEX_NAME_LEN], len: &mut usize, bytes: &[u8]) {
409 let end = *len + bytes.len();
410 out[*len..end].copy_from_slice(bytes);
411 *len = end;
412 }
413}
414
415fn index_name_slug(value: &str) -> String {
416 let separated = value
417 .chars()
418 .map(|ch| if ch.is_ascii_alphanumeric() { ch } else { '_' })
419 .collect::<String>();
420
421 to_snake_case(separated.as_str())
422}
423
424impl Ord for IndexName {
425 fn cmp(&self, other: &Self) -> Ordering {
426 self.to_bytes().cmp(&other.to_bytes())
427 }
428}
429
430impl PartialOrd for IndexName {
431 fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
432 Some(self.cmp(other))
433 }
434}
435
436impl fmt::Debug for IndexName {
437 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
438 write!(f, "IndexName({})", self.as_str())
439 }
440}
441
442impl Display for IndexName {
443 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
444 f.write_str(self.as_str())
445 }
446}