tnid 0.2.0

A UUID compatible ID with static type checking
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
//! TNID name encoding and validation.
//!
//! TNIDs include a name field (20 bits, 1-4 characters) that allows different kinds of IDs
//! to be differentiated at runtime and compile time. This module handles the encoding of
//! names into the TNID bit representation and validation of name strings.
//!
//! # Name Requirements
//!
//! Valid TNID names must:
//! - Be 1-4 characters long
//! - Contain only ASCII characters
//! - Use only characters from the allowed character set: digits (0-4) and lowercase letters (a-z)
//!
//! # Name Encoding
//!
//! Names are encoded using a 5-bit character encoding scheme (see [`CHAR_MAPPING`]). The encoding
//! is designed so that the bit representation ordering matches ASCII character ordering, making
//! TNIDs sortable in their string representation.
//!
//! If a name is shorter than 4 characters, it is null-padded to fill the 20-bit name field.
//!
//! # Examples
//!
//! ```rust
//! use tnid::NameStr;
//!
//! // Valid names
//! let name1 = NameStr::new("user").unwrap();
//! let name2 = NameStr::new("post").unwrap();
//! let name3 = NameStr::new("a").unwrap();     // Single character is ok
//! let name4 = NameStr::new("test").unwrap();  // 4 characters (max)
//!
//! // Invalid names
//! assert!(NameStr::new("").is_err());           // Too short
//! assert!(NameStr::new("toolong").is_err());    // Too long (>4 chars)
//! assert!(NameStr::new("User").is_err());       // Uppercase not allowed
//! assert!(NameStr::new("a-b").is_err());        // Dash not allowed
//! assert!(NameStr::new("test9").is_err());      // Digit 9 not in allowed set
//! ```

#[allow(clippy::indexing_slicing)] // panic is expected error path
pub const fn name_valid_check(name: &str) {
    if let NAME_MIN_CHARS..=NAME_MAX_CHARS = name.len() {
        if !name.is_ascii() {
            panic!("Id name must be ascii");
        }
    } else {
        panic!("Id name length must be within range")
    }

    let bytes = name.as_bytes();
    let mut i = 0;

    'check_loop: while i < bytes.len() {
        let mut j = 0;
        while j < CHAR_MAPPING.len() {
            if CHAR_MAPPING[j].1 == bytes[i] {
                i += 1;
                continue 'check_loop;
            }
            j += 1;
        }

        panic!("Invalid char in name");
    }
}

/// Minimum number of characters in a TNID name.
pub const NAME_MIN_CHARS: usize = 1;
/// Maximum number of characters in a TNID name.
pub const NAME_MAX_CHARS: usize = 4;

/// Result of validating the name bits in a TNID.
///
/// Used by [`validate_name_bits`] to indicate whether the name encoding
/// in a u128 ID is valid according to TNID rules.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum NameBitsValidation {
    /// The name encoding is valid (1-4 characters, properly null-padded).
    Valid,
    /// The name encoding is invalid (empty, improperly padded, or invalid encoding).
    Invalid,
}

/// Number of bits used for each character in a TNID name.
pub const CHAR_BIT_LENGTH: u8 = 5;
/// Mask for a single name character's bits.
pub const CHAR_MASK: u8 = (1 << CHAR_BIT_LENGTH) - 1;
/// Number of bits in a TNID that are not used for the name.
pub const NON_NAME_BITS: u8 = u128::BITS as u8 - (CHAR_BIT_LENGTH * NAME_MAX_CHARS as u8);

/// Mapping from numeric values to characters for name encoding.
pub const CHAR_MAPPING: [(u8, u8); 31] = [
    // zero is a null terminator

    // nums
    (1, b'0'),
    (2, b'1'),
    (3, b'2'),
    (4, b'3'),
    (5, b'4'),
    // alpha
    (6, b'a'),
    (7, b'b'),
    (8, b'c'),
    (9, b'd'),
    (10, b'e'),
    (11, b'f'),
    (12, b'g'),
    (13, b'h'),
    (14, b'i'),
    (15, b'j'),
    (16, b'k'),
    (17, b'l'),
    (18, b'm'),
    (19, b'n'),
    (20, b'o'),
    (21, b'p'),
    (22, b'q'),
    (23, b'r'),
    (24, b's'),
    (25, b't'),
    (26, b'u'),
    (27, b'v'),
    (28, b'w'),
    (29, b'x'),
    (30, b'y'),
    (31, b'z'),
];

/// Error when creating a [`NameStr`] from a string.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum NameError {
    /// The name string is empty (must be at least 1 character).
    Empty,
    /// The name string exceeds the maximum length of 4 characters.
    /// Contains the actual length provided.
    TooLong(usize),
    /// The name contains non-ASCII characters.
    NonAscii,
    /// The name contains a character not in the allowed set (0-4, a-z).
    /// Contains the invalid byte value.
    InvalidChar(u8),
}

impl std::fmt::Display for NameError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Empty => write!(f, "name cannot be empty"),
            Self::TooLong(len) => {
                write!(f, "name length {len} exceeds maximum of 4 characters")
            }
            Self::NonAscii => write!(f, "name must contain only ASCII characters"),
            Self::InvalidChar(byte) => {
                write!(
                    f,
                    "invalid character '{}' (0x{byte:02x}) in name; only 0-4 and a-z are allowed",
                    char::from(*byte)
                )
            }
        }
    }
}

impl std::error::Error for NameError {}

/// A validated TNID name string.
///
/// This type wraps a string slice and ensures it meets all TNID name requirements:
/// - Length between 1-4 characters (inclusive)
/// - ASCII only
/// - Only characters from the allowed set: digits 0-4 and lowercase letters a-z
///
/// # Examples
///
/// ```rust
/// use tnid::NameStr;
///
/// // Runtime validation with new()
/// let name = NameStr::new("user").unwrap();
/// assert_eq!(name.as_str(), "user");
///
/// // Invalid names return Err
/// assert!(NameStr::new("").is_err());
/// assert!(NameStr::new("CAPS").is_err());
/// ```
#[derive(Clone, Copy)]
pub struct NameStr<'a>(&'a str);
impl<'a> NameStr<'a> {
    /// Creates a new `NameStr` with compile-time validation when used in a const context.
    ///
    /// This method performs validation and will panic if the name is invalid. When used
    /// in a const context (like defining a [`TnidName`](crate::TnidName) implementation),
    /// the panic will occur at compile time. If used at runtime, it will panic the program.
    ///
    /// **Prefer using [`new()`](Self::new) for runtime validation** which returns a
    /// `Result<NameStr, NameError>` instead of panicking.
    ///
    /// # Panics
    ///
    /// Panics if the name:
    /// - Is not 1-4 characters long
    /// - Contains non-ASCII characters
    /// - Contains characters outside the allowed set (0-4, a-z)
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnid::{NameStr, TnidName};
    ///
    /// // Used in a const context for TNIDName (validated at compile time)
    /// struct User;
    /// impl TnidName for User {
    ///     const ID_NAME: NameStr<'static> = NameStr::new_const("user");
    /// }
    /// ```
    ///
    /// This will fail to compile because "INVALID" contains uppercase letters:
    /// ```compile_fail
    /// use tnid::{NameStr, TnidName};
    ///
    /// struct Invalid;
    /// impl TnidName for Invalid {
    ///     const ID_NAME: NameStr<'static> = NameStr::new_const("INVALID");
    /// }
    ///
    /// // This actually uses the const and triggers the compile-time check
    /// let _ = Invalid::ID_NAME;
    /// ```
    pub const fn new_const(s: &'static str) -> Self {
        name_valid_check(s);
        Self(s)
    }

    /// Creates a new `NameStr` with runtime validation.
    ///
    /// Returns `Ok(NameStr)` if the string is a valid TNID name, or `Err(NameError)` if it's invalid.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnid::NameStr;
    ///
    /// // Valid names (1-4 chars, digits 0-4 and lowercase a-z only)
    /// assert!(NameStr::new("user").is_ok());
    /// assert!(NameStr::new("post").is_ok());
    /// assert!(NameStr::new("a").is_ok());
    /// assert!(NameStr::new("test").is_ok());
    /// assert!(NameStr::new("id0").is_ok());
    ///
    /// // Too short or too long
    /// assert!(NameStr::new("").is_err());
    /// assert!(NameStr::new("toolong").is_err());
    ///
    /// // Invalid characters
    /// assert!(NameStr::new("User").is_err());    // uppercase not allowed
    /// assert!(NameStr::new("id5").is_err());     // digits 5-9 not allowed
    /// assert!(NameStr::new("a-b").is_err());     // special chars not allowed
    /// assert!(NameStr::new("café").is_err());    // non-ASCII not allowed
    /// ```
    pub fn new(s: &'a str) -> Result<Self, NameError> {
        if s.is_empty() {
            return Err(NameError::Empty);
        }

        if s.len() > NAME_MAX_CHARS {
            return Err(NameError::TooLong(s.len()));
        }

        if !s.is_ascii() {
            return Err(NameError::NonAscii);
        }

        // Check all characters are in CHAR_MAPPING
        let bytes = s.as_bytes();
        for &byte in bytes {
            let mut found = false;
            for &(_, valid_char) in &CHAR_MAPPING {
                if valid_char == byte {
                    found = true;
                    break;
                }
            }
            if !found {
                return Err(NameError::InvalidChar(byte));
            }
        }

        Ok(Self(s))
    }

    /// Returns the validated name as a string slice.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use tnid::NameStr;
    ///
    /// let name = NameStr::new("user").unwrap();
    /// assert_eq!(name.as_str(), "user");
    /// ```
    pub fn as_str(&self) -> &str {
        self.0
    }
}

/// Generates a bitmask for the given name in its proper position.
pub fn name_mask(name: NameStr) -> u128 {
    let name = name.as_str();
    let name_bytes = name.as_bytes();

    let mut mask = 0u128;

    for &name_char in name_bytes {
        let encoding_mapping = CHAR_MAPPING
            .iter()
            .find(|(_encoded, from_char)| *from_char == name_char);

        let (encoded_byte, _) = encoding_mapping.expect("mapping must exist");

        debug_assert!(*encoded_byte < 32);

        mask <<= CHAR_BIT_LENGTH;
        mask |= *encoded_byte as u128;
    }

    let needed_padding_chars = NAME_MAX_CHARS - name.len();
    mask <<= CHAR_BIT_LENGTH * needed_padding_chars as u8;

    mask <<= NON_NAME_BITS;

    mask
}

/// Validates that the name bits in a TNID are well-formed.
pub fn validate_name_bits(id: u128) -> NameBitsValidation {
    // Extract the top 20 bits (bits 127-108)
    let name_bits = (id >> NON_NAME_BITS) as u32;

    let mut found_char = false;
    let mut found_null = false;

    // Extract 4 characters of 5 bits each
    for i in (0..NAME_MAX_CHARS).rev() {
        let shift = i * CHAR_BIT_LENGTH as usize;
        let encoded_byte = (name_bits >> shift) as u8 & CHAR_MASK;

        // 0 is null terminator
        if encoded_byte == 0 {
            found_null = true;
            continue;
        }

        // If we found a non-null after a null, that's invalid (no padding in middle)
        if found_null {
            return NameBitsValidation::Invalid;
        }

        found_char = true;
    }

    // Must have at least 1 character
    if found_char {
        NameBitsValidation::Valid
    } else {
        NameBitsValidation::Invalid
    }
}

/// Returns the hex representation of the name portion of a TNID.
pub fn name_bits_to_hex(id: u128) -> String {
    let name_bits = (id >> NON_NAME_BITS) as u32;
    let hex = format!("{:05x}", name_bits);

    debug_assert_eq!(hex.len(), 5);

    hex
}

/// Extracts the name string from a TNID.
pub fn extract_name_string(id: u128) -> Option<String> {
    let name_bits = (id >> NON_NAME_BITS) as u32;

    let expected_string_len = name_bits.trailing_zeros() / 5;

    let mut name_bytes = Vec::with_capacity(expected_string_len as usize);

    // Extract 4 characters of 5 bits each
    for i in (0..NAME_MAX_CHARS).rev() {
        let shift = i * CHAR_BIT_LENGTH as usize;
        let encoded_byte = (name_bits >> shift) as u8 & CHAR_MASK;

        // 0 is null terminator - stop decoding
        if encoded_byte == 0 {
            break;
        }

        // Find the corresponding character in CHAR_MAPPING
        let decoded_char = CHAR_MAPPING
            .iter()
            .find(|(encoded, _)| *encoded == encoded_byte)
            .map(|(_, ascii_char)| *ascii_char)
            .expect("all non-zero 5-bit values (1-31) have mappings");

        name_bytes.push(decoded_char);
    }

    if name_bytes.is_empty() {
        return None;
    }

    Some(String::from_utf8(name_bytes).expect("name bytes must be valid ASCII"))
}

#[cfg(all(test, not(debug_assertions)))]
mod tests_release {
    use super::*;
    use proptest::prelude::*;

    use proptest::test_runner::TestRunner;

    #[test]
    fn name_mask_no_panic() {
        let mut runner = TestRunner::new(ProptestConfig {
            cases: 100_000,
            ..ProptestConfig::default()
        });
        runner
            .run(&any::<String>(), |name| {
                let Ok(name) = NameStr::new(name.as_str()) else {
                    return Ok(());
                };
                name_mask(name);
                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn extract_name_string_no_panic() {
        let mut runner = TestRunner::new(ProptestConfig {
            cases: 100_000,
            ..ProptestConfig::default()
        });
        runner
            .run(&any::<u128>(), |id| {
                // Should never panic, even for arbitrary bit patterns.
                let _ = extract_name_string(id);
                Ok(())
            })
            .unwrap();
    }

    #[test]
    fn extract_name_string_no_panic_valid_chars() {
        let mut runner = TestRunner::new(ProptestConfig {
            cases: 100_000,
            ..ProptestConfig::default()
        });
        let strategy = (1u128..=31, 0u128..=31, 0u128..=31, 0u128..=31);
        runner
            .run(&strategy, |(c0, c1, c2, c3)| {
                // Build a u128 with specific non-zero chars in the name positions
                // to ensure the CHAR_MAPPING lookup is always hit.
                let id = (c0 << 123) | (c1 << 118) | (c2 << 113) | (c3 << 108);
                let result = extract_name_string(id);
                // c0 is always non-zero, so at least one char is decoded before
                // any null terminator is hit.
                assert!(result.is_some());
                Ok(())
            })
            .unwrap();
    }
}