bare-types 0.3.0

A zero-cost foundation for type-safe domain modeling in Rust. Implements the 'Parse, don't validate' philosophy to eliminate primitive obsession and ensure data integrity at the system boundary.
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
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
//! UUID type for network programming.
//!
//! This module provides a type-safe abstraction for UUIDs,
//! ensuring compliance with RFC 4122 UUID specifications.
//!
//! # RFC 4122 UUID Rules
//!
//! According to [RFC 4122](https://datatracker.ietf.org/doc/html/rfc4122):
//!
//! - Length: 16 bytes (128 bits)
//! - Format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX (8-4-4-4-12 hex digits)
//! - Versions: 1 (time-based), 3 (MD5), 4 (random), 5 (SHA-1)
//! - Variants: NCS, RFC 4122, Microsoft, Future
//! - Nil UUID: 00000000-0000-0000-0000-000000000000
//!
//! # Examples
//!
//! ```rust
//! use bare_types::net::Uuid;
//!
//! // Create a UUID from bytes
//! let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
//!                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
//!
//! // Check if it's nil
//! assert!(!uuid.is_nil());
//!
//! // Get the version
//! assert_eq!(uuid.version(), None);
//!
//! // Get the string representation
//! assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
//!
//! // Parse from string
//! let uuid: Uuid = "00112233-4455-6677-8899-aabbccddeeff".parse()?;
//! # Ok::<(), bare_types::net::UuidError>(())
//! ```

use core::fmt;
use core::str::FromStr;

#[cfg(feature = "serde")]
use serde::{Deserialize, Serialize};

#[cfg(feature = "zeroize")]
use zeroize::Zeroize;

/// Error type for UUID validation.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[non_exhaustive]
pub enum UuidError {
    /// Invalid length
    ///
    /// UUIDs must be exactly 16 bytes (128 bits).
    /// This variant contains the actual length.
    InvalidLength(usize),
    /// Invalid format
    ///
    /// The UUID string is not in a valid format.
    /// Expected format: XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX
    InvalidFormat,
    /// Invalid character
    ///
    /// The UUID contains an invalid character.
    /// This variant contains the invalid character.
    InvalidChar(char),
}

impl fmt::Display for UuidError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::InvalidLength(len) => {
                write!(f, "UUID must be 16 bytes (got {len})")
            }
            Self::InvalidFormat => {
                write!(
                    f,
                    "UUID must be in format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
                )
            }
            Self::InvalidChar(c) => {
                write!(f, "UUID contains invalid character '{c}'")
            }
        }
    }
}

#[cfg(feature = "std")]
impl std::error::Error for UuidError {}

/// A UUID.
///
/// This type provides type-safe UUIDs with RFC 4122 validation.
/// It uses the newtype pattern with `#[repr(transparent)]` for zero-cost abstraction.
///
/// # Invariants
///
/// - Exactly 16 bytes (128 bits)
///
/// # Examples
///
/// ```rust
/// use bare_types::net::Uuid;
///
/// // Create a UUID from bytes
/// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
///
/// // Access the string representation
/// assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
///
/// // Check if it's nil
/// assert!(!uuid.is_nil());
///
/// // Get the bytes
/// assert_eq!(uuid.bytes(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
///                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
///
/// // Parse from string
/// let uuid: Uuid = "00112233-4455-6677-8899-aabbccddeeff".parse()?;
/// # Ok::<(), bare_types::net::UuidError>(())
/// ```
#[repr(transparent)]
#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "zeroize", derive(Zeroize))]
pub struct Uuid([u8; 16]);

#[cfg(feature = "arbitrary")]
impl<'a> arbitrary::Arbitrary<'a> for Uuid {
    fn arbitrary(u: &mut arbitrary::Unstructured<'a>) -> arbitrary::Result<Self> {
        let mut bytes = [0x8; 16];
        for byte in &mut bytes {
            *byte = u8::arbitrary(u)?;
        }
        Ok(Self(bytes))
    }
}

impl Uuid {
    /// Creates a new UUID from an array of 16 bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    /// ```
    #[inline]
    #[must_use]
    pub const fn new(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }

    /// Creates a new UUID from a slice of bytes.
    ///
    /// # Errors
    ///
    /// Returns `UuidError` if the slice is not exactly 16 bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let bytes = vec![0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff];
    /// let uuid = Uuid::from_bytes(&bytes)?;
    /// assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    /// # Ok::<(), bare_types::net::UuidError>(())
    /// ```
    pub fn from_bytes(bytes: &[u8]) -> Result<Self, UuidError> {
        if bytes.len() != 16 {
            return Err(UuidError::InvalidLength(bytes.len()));
        }
        let mut arr = [0u8; 16];
        arr.copy_from_slice(bytes);
        Ok(Self(arr))
    }

    /// Creates a new UUID from a string.
    ///
    /// # Errors
    ///
    /// Returns `UuidError` if string is not in a valid format.
    ///
    /// # Panics
    ///
    /// Panics if a hex character is invalid (this should never happen due to prior validation).
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::from_str("00112233-4455-6677-8899-aabbccddeeff")?;
    /// assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    /// # Ok::<(), bare_types::net::UuidError>(())
    /// ```
    #[allow(clippy::should_implement_trait)]
    pub fn from_str(s: &str) -> Result<Self, UuidError> {
        if s.len() != 36 {
            return Err(UuidError::InvalidFormat);
        }

        let chars: Vec<char> = s.chars().collect();

        if chars[8] != '-' || chars[13] != '-' || chars[18] != '-' || chars[23] != '-' {
            return Err(UuidError::InvalidFormat);
        }

        let mut bytes = [0u8; 16];
        let mut byte_idx = 0;
        let mut hex_idx = 0;

        for c in &chars {
            if *c == '-' {
                continue;
            }

            if !c.is_ascii_hexdigit() {
                return Err(UuidError::InvalidChar(*c));
            }

            let hex = u8::try_from(c.to_digit(16).unwrap()).unwrap();

            if hex_idx % 2 == 0 {
                bytes[byte_idx] = hex << 4;
            } else {
                bytes[byte_idx] |= hex;
                byte_idx += 1;
            }
            hex_idx += 1;
        }

        Ok(Self(bytes))
    }

    /// Returns the nil UUID.
    ///
    /// The nil UUID is 00000000-0000-0000-0000-000000000000.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::nil();
    /// assert!(uuid.is_nil());
    /// assert_eq!(uuid.as_str(), "00000000-0000-0000-0000-000000000000");
    /// ```
    #[must_use]
    #[inline]
    pub const fn nil() -> Self {
        Self([0u8; 16])
    }

    /// Returns the UUID as a string in standard format.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    /// ```
    #[must_use]
    pub fn as_str(&self) -> String {
        format!(
            "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            self.0[0],
            self.0[1],
            self.0[2],
            self.0[3],
            self.0[4],
            self.0[5],
            self.0[6],
            self.0[7],
            self.0[8],
            self.0[9],
            self.0[10],
            self.0[11],
            self.0[12],
            self.0[13],
            self.0[14],
            self.0[15]
        )
    }

    /// Returns the UUID as a string in uppercase format.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.as_str_upper(), "00112233-4455-6677-8899-AABBCCDDEEFF");
    /// ```
    #[must_use]
    pub fn as_str_upper(&self) -> String {
        format!(
            "{:02X}{:02X}{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}-{:02X}{:02X}{:02X}{:02X}{:02X}{:02X}",
            self.0[0],
            self.0[1],
            self.0[2],
            self.0[3],
            self.0[4],
            self.0[5],
            self.0[6],
            self.0[7],
            self.0[8],
            self.0[9],
            self.0[10],
            self.0[11],
            self.0[12],
            self.0[13],
            self.0[14],
            self.0[15]
        )
    }

    /// Returns a reference to the underlying array of 16 bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// let bytes: &[u8; 16] = uuid.as_inner();
    /// assert_eq!(bytes, &[0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                       0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// ```
    #[must_use]
    #[inline]
    pub const fn as_inner(&self) -> &[u8; 16] {
        &self.0
    }

    /// Consumes this UUID and returns the underlying array of 16 bytes.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// let bytes = uuid.into_inner();
    /// assert_eq!(bytes, [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                  0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// ```
    #[must_use]
    #[inline]
    pub const fn into_inner(self) -> [u8; 16] {
        self.0
    }

    /// Returns the bytes of the UUID.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.bytes(), [0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                          0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// ```
    #[must_use]
    #[inline]
    pub const fn bytes(&self) -> [u8; 16] {
        self.0
    }

    /// Returns `true` if this is a nil UUID.
    ///
    /// The nil UUID is 00000000-0000-0000-0000-000000000000.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// assert!(Uuid::nil().is_nil());
    /// ```
    #[must_use]
    #[inline]
    pub fn is_nil(&self) -> bool {
        self.0.iter().all(|&b| b == 0)
    }

    /// Returns the version of the UUID.
    ///
    /// Returns `None` if the UUID is not a valid RFC 4122 UUID.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::Uuid;
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.version(), None);
    /// ```
    #[must_use]
    #[inline]
    pub fn version(&self) -> Option<u8> {
        let version = self.0[6] >> 4;
        if (1..=5).contains(&version) {
            Some(version)
        } else {
            None
        }
    }

    /// Returns the variant of the UUID.
    ///
    /// Returns `None` if the UUID is not a valid RFC 4122 UUID.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use bare_types::net::{Uuid, UuidVariant};
    ///
    /// let uuid = Uuid::new([0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77,
    ///                        0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd, 0xee, 0xff]);
    /// assert_eq!(uuid.variant(), Some(UuidVariant::Rfc4122));
    /// ```
    #[must_use]
    #[inline]
    pub const fn variant(&self) -> Option<UuidVariant> {
        let variant = self.0[8] >> 6;
        match variant {
            0b00 => Some(UuidVariant::Ncs),
            0b10 => Some(UuidVariant::Rfc4122),
            0b110 => Some(UuidVariant::Microsoft),
            0b111 => Some(UuidVariant::Future),
            _ => None,
        }
    }
}

/// UUID variant.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[non_exhaustive]
pub enum UuidVariant {
    /// NCS backward compatibility
    Ncs,
    /// RFC 4122
    Rfc4122,
    /// Microsoft GUID
    Microsoft,
    /// Future
    Future,
}

impl TryFrom<&[u8]> for Uuid {
    type Error = UuidError;

    fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
        Self::from_bytes(bytes)
    }
}

impl TryFrom<&str> for Uuid {
    type Error = UuidError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        Self::from_str(s)
    }
}

impl From<Uuid> for [u8; 16] {
    fn from(uuid: Uuid) -> Self {
        uuid.0
    }
}

impl From<[u8; 16]> for Uuid {
    fn from(bytes: [u8; 16]) -> Self {
        Self(bytes)
    }
}

impl FromStr for Uuid {
    type Err = UuidError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_str(s)
    }
}

impl fmt::Display for Uuid {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "{:02x}{:02x}{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}-{:02x}{:02x}{:02x}{:02x}{:02x}{:02x}",
            self.0[0],
            self.0[1],
            self.0[2],
            self.0[3],
            self.0[4],
            self.0[5],
            self.0[6],
            self.0[7],
            self.0[8],
            self.0[9],
            self.0[10],
            self.0[11],
            self.0[12],
            self.0[13],
            self.0[14],
            self.0[15]
        )
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_new_valid_uuid() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    }

    #[test]
    fn test_from_bytes_valid() {
        let bytes = vec![
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ];
        assert!(Uuid::from_bytes(&bytes).is_ok());
    }

    #[test]
    fn test_from_bytes_invalid_length() {
        let bytes = vec![0x00, 0x11, 0x22];
        assert_eq!(Uuid::from_bytes(&bytes), Err(UuidError::InvalidLength(3)));
    }

    #[test]
    fn test_from_str_valid() {
        assert!(Uuid::from_str("00112233-4455-6677-8899-aabbccddeeff").is_ok());
        assert!(Uuid::from_str("00000000-0000-0000-0000-000000000000").is_ok());
        assert!(Uuid::from_str("ffffffff-ffff-ffff-ffff-ffffffffffff").is_ok());
    }

    #[test]
    fn test_from_str_invalid_format() {
        assert_eq!(
            Uuid::from_str("00112233-4455-6677-8899-aabbccddeef"),
            Err(UuidError::InvalidFormat)
        );
        assert_eq!(
            Uuid::from_str("00112233-4455-6677-8899-aabbccddeeff-"),
            Err(UuidError::InvalidFormat)
        );
    }

    #[test]
    fn test_from_str_invalid_char() {
        assert_eq!(
            Uuid::from_str("00112233-4455-6677-8899-aabbccddeegg"),
            Err(UuidError::InvalidChar('g'))
        );
        assert_eq!(
            Uuid::from_str("00112233-4455-6677-8899-aabbccddeeGG"),
            Err(UuidError::InvalidChar('G'))
        );
    }

    #[test]
    fn test_nil() {
        let uuid = Uuid::nil();
        assert!(uuid.is_nil());
        assert_eq!(uuid.as_str(), "00000000-0000-0000-0000-000000000000");
    }

    #[test]
    fn test_as_str() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    }

    #[test]
    fn test_as_str_upper() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(uuid.as_str_upper(), "00112233-4455-6677-8899-AABBCCDDEEFF");
    }

    #[test]
    fn test_as_inner() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let bytes = uuid.as_inner();
        assert_eq!(
            bytes,
            &[
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }

    #[test]
    fn test_into_inner() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let bytes = uuid.into_inner();
        assert_eq!(
            bytes,
            [
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }

    #[test]
    fn test_bytes() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(
            uuid.bytes(),
            [
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }

    #[test]
    fn test_is_nil() {
        assert!(Uuid::nil().is_nil());
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert!(!uuid.is_nil());
    }

    #[test]
    fn test_version() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(uuid.version(), None);
    }

    #[test]
    fn test_variant() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(uuid.variant(), Some(UuidVariant::Rfc4122));
    }

    #[test]
    fn test_try_from_bytes() {
        let bytes = vec![
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ];
        let uuid = Uuid::try_from(bytes.as_slice()).unwrap();
        assert_eq!(
            uuid.bytes(),
            [
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }

    #[test]
    fn test_try_from_str() {
        let uuid = Uuid::try_from("00112233-4455-6677-8899-aabbccddeeff").unwrap();
        assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    }

    #[test]
    fn test_from_uuid_to_array() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let bytes: [u8; 16] = uuid.into();
        assert_eq!(
            bytes,
            [
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }

    #[test]
    fn test_from_array_to_uuid() {
        let bytes = [
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ];
        let uuid: Uuid = bytes.into();
        assert_eq!(uuid.bytes(), bytes);
    }

    #[test]
    fn test_from_str() {
        let uuid: Uuid = "00112233-4455-6677-8899-aabbccddeeff".parse().unwrap();
        assert_eq!(uuid.as_str(), "00112233-4455-6677-8899-aabbccddeeff");
    }

    #[test]
    fn test_from_str_invalid() {
        assert!(
            "00112233-4455-6677-8899-aabbccddeef"
                .parse::<Uuid>()
                .is_err()
        );
        assert!(
            "00112233-4455-6677-8899-aabbccddeeg!p"
                .parse::<Uuid>()
                .is_err()
        );
    }

    #[test]
    fn test_display() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(format!("{uuid}"), "00112233-4455-6677-8899-aabbccddeeff");
    }

    #[test]
    fn test_equality() {
        let uuid1 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let uuid2 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let uuid3 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xfe,
        ]);

        assert_eq!(uuid1, uuid2);
        assert_ne!(uuid1, uuid3);
    }

    #[test]
    fn test_ordering() {
        let uuid1 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xfe,
        ]);
        let uuid2 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);

        assert!(uuid1 < uuid2);
    }

    #[test]
    fn test_clone() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let uuid2 = uuid.clone();
        assert_eq!(uuid, uuid2);
    }

    #[test]
    fn test_error_display() {
        assert_eq!(
            format!("{}", UuidError::InvalidLength(3)),
            "UUID must be 16 bytes (got 3)"
        );
        assert_eq!(
            format!("{}", UuidError::InvalidFormat),
            "UUID must be in format XXXXXXXX-XXXX-XXXX-XXXX-XXXXXXXXXXXX"
        );
        assert_eq!(
            format!("{}", UuidError::InvalidChar('g')),
            "UUID contains invalid character 'g'"
        );
    }

    #[test]
    fn test_hash() {
        use core::hash::Hash;
        use core::hash::Hasher;

        #[derive(Default)]
        struct SimpleHasher(u64);

        impl Hasher for SimpleHasher {
            fn finish(&self) -> u64 {
                self.0
            }

            fn write(&mut self, bytes: &[u8]) {
                for byte in bytes {
                    self.0 = self.0.wrapping_mul(31).wrapping_add(*byte as u64);
                }
            }
        }

        let uuid1 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let uuid2 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let uuid3 = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xfe,
        ]);

        let mut hasher1 = SimpleHasher::default();
        let mut hasher2 = SimpleHasher::default();
        let mut hasher3 = SimpleHasher::default();

        uuid1.hash(&mut hasher1);
        uuid2.hash(&mut hasher2);
        uuid3.hash(&mut hasher3);

        assert_eq!(hasher1.finish(), hasher2.finish());
        assert_ne!(hasher1.finish(), hasher3.finish());
    }

    #[test]
    fn test_debug() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        assert_eq!(
            format!("{:?}", uuid),
            "Uuid([0, 17, 34, 51, 68, 85, 102, 119, 136, 153, 170, 187, 204, 221, 238, 255])"
        );
    }

    #[test]
    fn test_from_into_inner_roundtrip() {
        let uuid = Uuid::new([
            0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
            0xee, 0xff,
        ]);
        let bytes = uuid.into_inner();
        let uuid2 = Uuid::new(bytes);
        assert_eq!(
            uuid2.bytes(),
            [
                0x00, 0x11, 0x22, 0x33, 0x44, 0x55, 0x66, 0x77, 0x88, 0x99, 0xaa, 0xbb, 0xcc, 0xdd,
                0xee, 0xff
            ]
        );
    }
}