grain-id 0.16.0

Compact, human-readable unique IDs — grain-sized, yet enough for a lifetime
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
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
use core::{fmt::Display, str::FromStr};

use crate::{
    Error,
    alphabet::{BASE, u5_to_char_lossy},
};

/// Grain id struct
///
/// # Examples
#[cfg_attr(feature = "default", doc = "```rust")]
#[cfg_attr(not(feature = "default"), doc = "```ignore")]
/// # use grain_id::*;
/// # fn main() -> Result<(), Error> {
/// // Generate random value.
/// let random = GrainId::random();
///
/// assert_ne!(random, GrainId::NIL);
///
/// // Parse from string.
/// let from_str: GrainId = "012abcd".parse()?;
///
/// // Parse from integer.
/// let from_int: GrainId = 35990925u64.try_into()?;
///
/// assert_eq!(from_str, from_int);
/// # Ok(())
/// # }
/// ```
#[derive(Copy, Clone, Debug, Hash, PartialEq, Eq, PartialOrd, Ord)]
pub struct GrainId(u64);

impl GrainId {
    /// The size of the integer type in bits.
    ///
    /// This is not equal actually stored size.
    pub const BITS: u32 = 35;

    /// The capacity value of the grain id
    #[deprecated(since = "0.8.1")]
    pub const CAPACITY: u64 = (BASE as u64).pow(7);

    const MAX_VALUE_PLUS_ONE: u64 = (BASE as u64).pow(7);
    const MAX_VALUE: u64 = Self::MAX_VALUE_PLUS_ONE - 1;

    const fn from_u64_unchecked(value: u64) -> Self {
        Self(value)
    }

    /// The smallest value that can be represented by [`GrainId`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert_eq!(GrainId::NIL, "0000000".parse::<GrainId>()?);
    /// assert_eq!(GrainId::NIL, GrainId::try_from(0)?);
    /// # Ok(())
    /// # }
    /// ```
    pub const NIL: Self = Self::from_u64_unchecked(0);

    /// The largest value that can be represented by [`GrainId`].
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert_eq!(GrainId::MAX, "zzzzzzz".parse::<GrainId>()?);
    /// assert_eq!(GrainId::MAX, GrainId::try_from(GrainId::CAPACITY - 1)?);
    /// # Ok(())
    /// # }
    /// ```
    pub const MAX: Self = Self::from_u64_unchecked(Self::MAX_VALUE);

    /// Test if the [`GrainId`] is nil.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert!("0000000".parse::<GrainId>()?.is_nil());
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_nil(&self) -> bool {
        self == &Self::NIL
    }

    /// Test if the [`GrainId`] is max.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert!("zzzzzzz".parse::<GrainId>()?.is_max());
    /// # Ok(())
    /// # }
    /// ```
    pub fn is_max(&self) -> bool {
        self == &Self::MAX
    }

    /// "Converts an unsigned integer to [`GrainId`] by truncating bits that exceed the valid range.")]
    ///
    /// This is a lossy conversion that masks the input value to fit within the ID's bit limit.
    /// If you need to detect out-of-range values, use [`TryFrom`] instead.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// // Values within range are preserved
    /// let valid_int = 123;
    /// let id = GrainId::from_u64_lossy(valid_int);
    /// assert_eq!(u64::from(id), valid_int);
    ///
    /// // values exceeding 35 bits are truncated (MSB(s) dropped
    /// let oversized_int = valid_int + GrainId::CAPACITY;
    /// let overflowed_id = GrainId::from_u64_lossy(oversized_int);
    /// assert_ne!(u64::from(overflowed_id), oversized_int);
    /// // Only lower 35 bits retained
    /// assert_eq!(u64::from(overflowed_id), valid_int)
    /// ```
    pub const fn from_u64_lossy(int: u64) -> Self {
        Self::from_u64_unchecked(int & Self::MAX_VALUE)
    }

    /// Attempts to convert a [`u64`]  to [`GrainId`].
    ///
    ///
    /// Return error if the value is larger than [`GrainId::MAX`].
    /// If you don't need to detect out-of-range values, use [`from_uint_lossy`](Self::from_uint_lossy).
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert_eq!(GrainId::from_u64(0x7FFFFFFFF)?, GrainId::MAX);
    /// assert!(GrainId::from_u64(0x800000000).is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub const fn from_u64(value: u64) -> Result<Self, crate::Error> {
        if value < Self::MAX_VALUE_PLUS_ONE {
            Ok(Self::from_u64_unchecked(value))
        } else {
            Err(Error::ValueOutOfRange(value))
        }
    }

    /// Returns a reference of internal 64bit integer.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let id = GrainId::from_u64(0x123456789)?;
    /// assert_eq!(id.as_u64(), &0x123456789);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn as_u64(&self) -> &u64 {
        &self.0
    }
    /// Returns an internal 64bit integer value.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let id = GrainId::from_u64(0x123456789)?;
    /// assert_eq!(id.to_u64(), 0x123456789);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_u64(self) -> u64 {
        self.0
    }

    fn from_chars(value: [char; 7]) -> Result<Self, Error> {
        Ok(Self::from_u64_unchecked(u5s_to_u35_unchecked(
            chars_to_u5s(value)?,
        )))
    }

    pub(crate) fn to_chars(self) -> [char; 7] {
        [
            u5_to_char_lossy((self.0 >> 30) as u8),
            u5_to_char_lossy((self.0 >> 25) as u8),
            u5_to_char_lossy((self.0 >> 20) as u8),
            u5_to_char_lossy((self.0 >> 15) as u8),
            u5_to_char_lossy((self.0 >> 10) as u8),
            u5_to_char_lossy((self.0 >> 5) as u8),
            u5_to_char_lossy((self.0) as u8),
        ]
    }

    /// Creates a `GrainId` from a [`Duration`](std::time::Duration) based on decisecond precision.
    ///
    /// This method converts the given duration into deciseconds (0.1 second units)
    /// and generates a `GrainId` from it. The resulting 35-bit identifier can
    /// represent approximately 109 years worth of deciseconds.
    ///
    /// # Timestamp Format
    ///
    /// The entire 35 bits are used to represent the timestamp in deciseconds,
    /// allowing for chronological sorting while maintaining a compact 7-character
    /// BASE32 representation.
    ///
    /// # Overflow Behavior
    ///
    /// If the duration exceeds the maximum representable value (2^35 deciseconds,
    /// approximately 109 years), the timestamp will wrap around. This is generally
    /// not a concern for personal applications within a human lifespan.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// use std::time::Duration;
    ///
    /// # fn main() -> Result<(), Error> {
    /// let duration = Duration::new(5, 730_023_852);
    /// let id = GrainId::from_duration(duration);
    /// assert_eq!(id, GrainId::from_u64(57)?);
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_duration(duration: core::time::Duration) -> Self {
        let decisecs = duration.as_millis() / 100;
        Self::from_u64_lossy(decisecs as u64)
    }

    #[deprecated(since = "0.15.0", note = "Use `to_u64().to_be_bytes()` instead.")]
    #[doc = crate::macros::doc_to_bytes!("big endian")]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_be_bytes();
    /// assert_eq!(bytes, [0, 0, 0, 0x01, 0x23, 0x45, 0x67, 0x89]);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_be_bytes(self) -> [u8; 8] {
        self.0.to_be_bytes()
    }

    #[deprecated(since = "0.15.0", note = "Use `to_u64().to_le_bytes()` instead.")]
    #[doc = crate::macros::doc_to_bytes!("little endian")]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_le_bytes();
    /// assert_eq!(bytes, [0x89, 0x67, 0x45, 0x23, 0x01, 0, 0, 0]);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_le_bytes(self) -> [u8; 8] {
        self.0.to_le_bytes()
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64(u64::from_be_bytes(bytes))?` instead."
    )]
    #[doc = crate::macros::doc_from_bytes!("big endian", Self::from_be_bytes_lossy)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0, 0, 0, 0x01, 0x23, 0x45, 0x67, 0x89];
    /// assert_eq!(
    ///     GrainId::from_be_bytes(value)?,
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value returns error
    /// let oversized_value = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
    /// assert!(GrainId::from_be_bytes(oversized_value).is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_be_bytes(bytes: [u8; 8]) -> Result<Self, Error> {
        Self::from_u64(u64::from_be_bytes(bytes))
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64(u64::from_le_bytes(bytes))?` instead."
    )]
    #[doc = crate::macros::doc_from_bytes!("little endian", Self::from_le_bytes_lossy)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x89, 0x67, 0x45, 0x23, 0x01, 0, 0, 0];
    /// assert_eq!(
    ///     GrainId::from_le_bytes(value)?,
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value returns error
    /// let oversized_value = [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01];
    /// assert!(GrainId::from_le_bytes(oversized_value).is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_le_bytes(bytes: [u8; 8]) -> Result<Self, Error> {
        Self::from_u64(u64::from_le_bytes(bytes))
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64_lossy(u64::from_be_bytes(bytes))` instead."
    )]
    #[doc = crate::macros::doc_from_bytes_lossy!("big endian")]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    ///
    /// let value = [0, 0, 0, 0x01, 0x23, 0x45, 0x67, 0x89];
    /// assert_eq!(
    ///     GrainId::from_be_bytes_lossy(value),
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value will be truncated.
    /// let oversized_value = [0x01, 0x23, 0x45, 0x67, 0x89, 0xab, 0xcd, 0xef];
    /// assert_eq!(
    ///     GrainId::from_be_bytes_lossy(oversized_value),
    ///     GrainId::from_u64(0x789abcdef)?
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_be_bytes_lossy(bytes: [u8; 8]) -> Self {
        Self::from_u64_lossy(u64::from_be_bytes(bytes))
    }
    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64_lossy(u64::from_le_bytes(bytes))` instead."
    )]
    #[doc = crate::macros::doc_from_bytes_lossy!("little endian")]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x89, 0x67, 0x45, 0x23, 0x01, 0, 0, 0];
    /// assert_eq!(
    ///     GrainId::from_le_bytes_lossy(value),
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value will be truncated.
    /// let oversized_value = [0xef, 0xcd, 0xab, 0x89, 0x67, 0x45, 0x23, 0x01];
    /// assert_eq!(
    ///     GrainId::from_le_bytes_lossy(oversized_value),
    ///     GrainId::from_u64(0x789abcdef)?
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_le_bytes_lossy(bytes: [u8; 8]) -> Self {
        Self::from_u64_lossy(u64::from_le_bytes(bytes))
    }

    #[deprecated(since = "0.15.0", note = "Use `to_byte_suffix()` instead.")]
    #[doc = crate::macros::doc_to_bytes!("big endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_be_bytes_compact();
    /// assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x89]);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_be_bytes_compact(self) -> [u8; 5] {
        let bytes = self.0.to_be_bytes();
        [bytes[3], bytes[4], bytes[5], bytes[6], bytes[7]]
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `to_u64().to_le_bytes()` for an 8-byte little-endian representation."
    )]
    #[doc = crate::macros::doc_to_bytes!("little endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_le_bytes_compact();
    /// assert_eq!(bytes, [0x89, 0x67, 0x45, 0x23, 0x01]);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_le_bytes_compact(self) -> [u8; 5] {
        let bytes = self.0.to_le_bytes();
        [bytes[0], bytes[1], bytes[2], bytes[3], bytes[4]]
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `from_byte_suffix()` for lossy conversion, or `GrainId::from_u64(u64::from_be_bytes([0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4]]))?` for checked conversion."
    )]
    #[doc = crate::macros::doc_from_bytes!("big endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x01, 0x23, 0x45, 0x67, 0x89];
    /// assert_eq!(
    ///     GrainId::from_be_bytes_compact(value)?,
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Overflowed value returns error.
    /// let oversized_value = [0x12, 0x34, 0x56, 0x78, 0x90];
    /// assert!(GrainId::from_be_bytes_compact(oversized_value).is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_be_bytes_compact(bytes: [u8; 5]) -> Result<Self, Error> {
        Self::from_u64(u64::from_be_bytes([
            0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
        ]))
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64(u64::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0]))?` instead."
    )]
    #[doc = crate::macros::doc_from_bytes!("little endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x89, 0x67, 0x45, 0x23, 0x01];
    /// assert_eq!(
    ///     GrainId::from_le_bytes_compact(value)?,
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value returns error.
    /// let oversized_value = [0x90, 0x78, 0x56, 0x34, 0x12];
    /// assert!(GrainId::from_le_bytes_compact(oversized_value).is_err());
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_le_bytes_compact(bytes: [u8; 5]) -> Result<Self, Error> {
        Self::from_u64(u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0,
        ]))
    }

    #[deprecated(since = "0.15.0", note = "Use `from_byte_suffix()` instead.")]
    #[doc = crate::macros::doc_from_bytes_lossy!("big endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x01, 0x23, 0x45, 0x67, 0x89];
    /// assert_eq!(
    ///     GrainId::from_be_bytes_compact_lossy(value),
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value will be truncated.
    /// let oversized_value = [0x12, 0x34, 0x56, 0x78, 0x90];
    /// assert_eq!(
    ///     GrainId::from_be_bytes_compact_lossy(oversized_value),
    ///     GrainId::from_u64(0x234567890)?
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_be_bytes_compact_lossy(bytes: [u8; 5]) -> Self {
        Self::from_u64_lossy(u64::from_be_bytes([
            0, 0, 0, bytes[0], bytes[1], bytes[2], bytes[3], bytes[4],
        ]))
    }

    #[deprecated(
        since = "0.15.0",
        note = "Use `GrainId::from_u64_lossy(u64::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0]))` instead."
    )]
    #[doc = crate::macros::doc_from_bytes_lossy!("little endian", compact)]
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let value = [0x89, 0x67, 0x45, 0x23, 0x01];
    /// assert_eq!(
    ///     GrainId::from_le_bytes_compact_lossy(value),
    ///     GrainId::from_u64(0x123456789)?
    /// );
    ///
    /// // Oversized value will be truncated
    /// let oversized_value = [0x90, 0x78, 0x56, 0x34, 0x12];
    /// assert_eq!(
    ///     GrainId::from_le_bytes_compact_lossy(oversized_value),
    ///     GrainId::from_u64(0x234567890)?
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub fn from_le_bytes_compact_lossy(bytes: [u8; 5]) -> Self {
        Self::from_u64_lossy(u64::from_le_bytes([
            bytes[0], bytes[1], bytes[2], bytes[3], bytes[4], 0, 0, 0,
        ]))
    }

    /// Wrapping (modular) subtraction. Computes `self - rhs`, wrapping around at the boundary of the type.
    ///
    /// # Examples
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert_eq!(
    ///     GrainId::from_u64(100)?.wrapping_sub(GrainId::from_u64(100)?),
    ///     GrainId::NIL
    /// );
    /// assert_eq!(
    ///     GrainId::from_u64(100)?.wrapping_sub(GrainId::from_u64(101)?),
    ///     GrainId::MAX
    /// );
    /// # Ok(())
    /// # }
    /// ```
    pub const fn wrapping_sub(self, rhs: Self) -> Self {
        GrainId::from_u64_lossy(self.to_u64().wrapping_sub(rhs.to_u64()))
    }

    /// Creates a `GrainId` from the leading (most significant) 35 bits of a 5-byte array.
    ///
    /// This is useful for generating a compact identifier from a pre-computed hash,
    /// public key, or any arbitrary byte sequence.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// // Extract leading 35 bits from a byte array
    /// let bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
    /// let id = GrainId::from_byte_prefix(&bytes);
    /// assert_eq!(id, GrainId::MAX);
    ///
    /// // For longer data, slice the first 5 bytes
    /// let hash: [u8; 32] = [0; 32];
    /// let id = GrainId::from_byte_prefix(hash[..5].try_into().unwrap());
    /// assert_eq!(id, GrainId::NIL);
    /// ```
    pub const fn from_byte_prefix(bytes: &[u8; 5]) -> Self {
        let value = (bytes[0] as u64) << 27
            | (bytes[1] as u64) << 19
            | (bytes[2] as u64) << 11
            | (bytes[3] as u64) << 3
            | (bytes[4] as u64) >> 5;
        Self::from_u64_lossy(value)
    }

    /// Creates a `GrainId` from the trailing (least significant) 35 bits of a 5-byte array.
    ///
    /// This is the counterpart to [`from_byte_prefix`](Self::from_byte_prefix).
    /// The upper 5 bits of the first byte are ignored.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// // Extract trailing 35 bits from a byte array
    /// let bytes = [0xFF, 0xFF, 0xFF, 0xFF, 0xFF];
    /// let id = GrainId::from_byte_suffix(&bytes);
    /// assert_eq!(id, GrainId::MAX);
    ///
    /// // Upper 5 bits of the first byte are ignored
    /// let bytes = [0xF8, 0x00, 0x00, 0x00, 0x00];
    /// let id = GrainId::from_byte_suffix(&bytes);
    /// assert_eq!(id, GrainId::NIL);
    /// ```
    pub const fn from_byte_suffix(bytes: &[u8; 5]) -> Self {
        let value = ((bytes[0] & 0x07) as u64) << 32
            | (bytes[1] as u64) << 24
            | (bytes[2] as u64) << 16
            | (bytes[3] as u64) << 8
            | (bytes[4] as u64);
        Self::from_u64_unchecked(value)
    }

    /// Returns a 5-byte array with the `GrainId` value stored in the leading (most significant) 35 bits.
    ///
    /// This is the counterpart to [`from_byte_prefix`](Self::from_byte_prefix).
    /// The lower 5 bits of the last byte are always zero.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_byte_prefix();
    /// assert_eq!(bytes, [0x24, 0x68, 0xAC, 0xF1, 0x20]);
    ///
    /// // Round-trip
    /// let id = GrainId::from_u64(0x123456789)?;
    /// assert_eq!(GrainId::from_byte_prefix(&id.to_byte_prefix()), id);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_byte_prefix(self) -> [u8; 5] {
        let v = self.0;
        [
            (v >> 27) as u8,
            (v >> 19) as u8,
            (v >> 11) as u8,
            (v >> 3) as u8,
            ((v & 0x07) << 5) as u8,
        ]
    }

    /// Returns a 5-byte array with the `GrainId` value stored in the trailing (least significant) 35 bits.
    ///
    /// This is the counterpart to [`from_byte_suffix`](Self::from_byte_suffix).
    /// The upper 5 bits of the first byte are always zero.
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// let bytes = GrainId::from_u64(0x123456789)?.to_byte_suffix();
    /// assert_eq!(bytes, [0x01, 0x23, 0x45, 0x67, 0x89]);
    ///
    /// // Round-trip
    /// let id = GrainId::from_u64(0x123456789)?;
    /// assert_eq!(GrainId::from_byte_suffix(&id.to_byte_suffix()), id);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn to_byte_suffix(self) -> [u8; 5] {
        let v = self.0;
        [
            (v >> 32) as u8,
            (v >> 24) as u8,
            (v >> 16) as u8,
            (v >> 8) as u8,
            v as u8,
        ]
    }

    /// Increments the value by 1, wrapping around to [`GrainId::NIL`] on overflow.
    ///
    /// # Examples
    /// ```
    /// # use grain_id::*;
    /// # fn main() -> Result<(), Error> {
    /// assert_eq!(GrainId::from_u64(100)?.increment(), GrainId::from_u64(101)?);
    /// assert_eq!(GrainId::MAX.increment(), GrainId::NIL);
    /// # Ok(())
    /// # }
    /// ```
    pub const fn increment(self) -> Self {
        GrainId::from_u64_lossy(self.to_u64() + 1)
    }
}

fn chars_to_u5s(value: [char; 7]) -> Result<[u8; 7], Error> {
    let mut result = [0; 7];
    for i in 0..7 {
        result[i] = crate::alphabet::char_to_u5(value[i]).ok_or(Error::InvalidCharacter {
            character: value[i],
            index: i,
        })?;
    }
    Ok(result)
}

fn u5s_to_u35_unchecked(value: [u8; 7]) -> u64 {
    ((value[0] as u64) << 30)
        | ((value[1] as u64) << 25)
        | ((value[2] as u64) << 20)
        | ((value[3] as u64) << 15)
        | ((value[4] as u64) << 10)
        | ((value[5] as u64) << 5)
        | (value[6] as u64)
}

impl AsRef<u64> for GrainId {
    fn as_ref(&self) -> &u64 {
        self.as_u64()
    }
}

impl TryFrom<u64> for GrainId {
    type Error = Error;
    /// Attempts to convert a [`u64`]  to [`GrainId`].
    ///
    /// # Error
    /// Return error if the value is larger than [`GrainId::MAX`].
    /// If you don't need to detect out-of-range values, use [`from_u64_lossy`](GrainId::from_u64_lossy).
    ///
    /// # Examples
    ///
    /// ```
    /// # use grain_id::*;
    /// assert!(GrainId::try_from(GrainId::CAPACITY - 1).is_ok());
    /// assert!(GrainId::try_from(GrainId::CAPACITY).is_err());
    /// ```
    fn try_from(value: u64) -> Result<Self, Self::Error> {
        Self::from_u64(value)
    }
}

impl From<GrainId> for u64 {
    fn from(value: GrainId) -> Self {
        value.0
    }
}

impl Display for GrainId {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        let chars = self.to_chars();
        write!(
            f,
            "{}{}{}{}{}{}{}",
            chars[0], chars[1], chars[2], chars[3], chars[4], chars[5], chars[6]
        )
    }
}

impl FromStr for GrainId {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        let len = s.len();
        if len == 7 {
            let mut chars = s.chars();
            Self::from_chars([
                chars.next().unwrap(),
                chars.next().unwrap(),
                chars.next().unwrap(),
                chars.next().unwrap(),
                chars.next().unwrap(),
                chars.next().unwrap(),
                chars.next().unwrap(),
            ])
        } else {
            Err(Error::InvalidLength(len))
        }
    }
}