typedflake 0.1.3

A Snowflake-style ID generator library with newtype-driven design
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
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
//! Configuration types for ID generation.
//!
//! Provides configuration for Snowflake-style ID generation:
//! - [`BitLayout`] - How the 64-bit ID space is divided (timestamp, worker, process, sequence)
//! - [`Config`] - Complete configuration including bit layout and epoch
//! - [`Epoch`] - Custom epoch timestamps
//!
//! Use industry presets ([`Config::TWITTER`], [`Config::DISCORD`]) or create custom configurations
//! based on your scaling and throughput needs.

use derive_more::{Display, Error, From};
use std::fmt;
use std::time::{SystemTime, UNIX_EPOCH};

/// Errors that can occur when creating or validating a BitLayout
#[derive(Display, Error, Debug, Clone, PartialEq, Eq)]
pub enum BitLayoutError {
    #[display("Bit allocation must sum to 64, got {actual}")]
    InvalidSum { actual: u8 },
    #[display("Timestamp bits must be greater than 0")]
    ZeroTimestampBits,
    #[display("Sequence bits must be greater than 0")]
    ZeroSequenceBits,
}

impl BitLayoutError {
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::InvalidSum { .. } => "Bit allocation must sum to 64",
            Self::ZeroTimestampBits => "Timestamp bits must be greater than 0",
            Self::ZeroSequenceBits => "Sequence bits must be greater than 0",
        }
    }
}

/// Errors that can occur when creating an Epoch
#[derive(Display, Error, Debug, Clone, PartialEq, Eq)]
pub enum EpochError {
    #[display("Invalid year {year}, must be between 1970 and 2100")]
    InvalidYear { year: u16 },
    #[display("Invalid month {month}, must be between 1 and 12")]
    InvalidMonth { month: u8 },
    #[display("Invalid day {day} for month {month}")]
    InvalidDay { day: u8, month: u8 },
}

impl EpochError {
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::InvalidYear { .. } => "Invalid year, must be between 1970 and 2100",
            Self::InvalidMonth { .. } => "Invalid month, must be between 1 and 12",
            Self::InvalidDay { .. } => "Invalid day for month",
        }
    }
}

/// Errors that can occur when creating a Config
#[derive(Display, Error, Debug, Clone, PartialEq, Eq)]
pub enum ConfigError {
    #[display("Epoch {epoch}ms is in the future (current time: {current}ms)")]
    FutureEpoch { epoch: u64, current: u64 },
    #[display(
        "Epoch is too old: {elapsed}ms elapsed exceeds maximum {max_duration}ms (configured with {bits} timestamp bits)"
    )]
    EpochExceedsCapacity {
        elapsed: u64,
        max_duration: u64,
        bits: u8,
    },
}

impl ConfigError {
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::FutureEpoch { .. } => "Epoch is in the future",
            Self::EpochExceedsCapacity { .. } => "Epoch is too old for timestamp bit allocation",
        }
    }
}

/// Epoch timestamp in milliseconds since UNIX epoch
///
/// Provides type-safe epoch configuration with predefined presets
/// and convenience constructors.
///
/// # Examples
///
/// ```
/// use typedflake::Epoch;
///
/// // Use presets
/// let epoch = Epoch::DISCORD;
///
/// // From raw milliseconds
/// let epoch = Epoch::new(1735689600000);
///
/// // From date (const, panics on invalid)
/// const CUSTOM_EPOCH: Epoch = Epoch::from_date(2025, 1, 1);
///
/// // From date (fallible)
/// # fn example() -> Result<(), Box<dyn std::error::Error>> {
/// let epoch = Epoch::try_from_date(2025, 1, 1)?;
/// # Ok(())
/// # }
/// ```
#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Epoch {
    millis: u64,
}

impl Epoch {
    /// Twitter's epoch (Nov 4, 2010 01:42:54 UTC)
    pub const TWITTER: Self = Self::new(1288834974657);

    /// Discord's epoch (Jan 1, 2015 00:00:00 UTC)
    pub const DISCORD: Self = Self::new(1420070400000);

    /// Instagram's epoch (Sep 20, 2011 00:00:21 UTC)
    pub const INSTAGRAM: Self = Self::new(1314220021721);

    /// Default epoch (Jan 1, 2025 00:00:00 UTC)
    pub const DEFAULT: Self = Self::new(1735689600000);

    /// Create a new Epoch from milliseconds since UNIX epoch
    pub const fn new(millis: u64) -> Self {
        Self { millis }
    }

    /// Create an Epoch from a date with validation (year, month, day)
    ///
    /// # Arguments
    /// - `year`: Year (1970-2100)
    /// - `month`: Month (1-12)
    /// - `day`: Day of month (1-31)
    pub const fn try_from_date(year: u16, month: u8, day: u8) -> Result<Self, EpochError> {
        // Validate year
        if year < 1970 || year > 2100 {
            return Err(EpochError::InvalidYear { year });
        }

        // Validate month
        if month < 1 || month > 12 {
            return Err(EpochError::InvalidMonth { month });
        }

        // Days in each month (non-leap year)
        let days_in_month = match month {
            1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
            4 | 6 | 9 | 11 => 30,
            2 => {
                // Check for leap year
                if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) {
                    29
                } else {
                    28
                }
            }
            _ => unreachable!(),
        };

        // Validate day
        if day < 1 || day > days_in_month {
            return Err(EpochError::InvalidDay { day, month });
        }

        // Calculate days since UNIX epoch (Jan 1, 1970)
        let mut days = 0i64;

        // Add days for complete years
        let mut y: u16 = 1970;
        while y < year {
            days += if (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0) {
                366
            } else {
                365
            };
            y += 1;
        }

        // Add days for complete months in current year
        let mut m = 1;
        while m < month {
            days += match m {
                1 | 3 | 5 | 7 | 8 | 10 | 12 => 31,
                4 | 6 | 9 | 11 => 30,
                2 => {
                    if (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0) {
                        29
                    } else {
                        28
                    }
                }
                _ => unreachable!(),
            } as i64;
            m += 1;
        }

        // Add remaining days (subtract 1 because day 1 = 0 days elapsed)
        days += (day - 1) as i64;

        // Convert to milliseconds
        let millis = (days * 24 * 60 * 60 * 1000) as u64;

        Ok(Self::new(millis))
    }

    /// Create an Epoch from a date (year, month, day)
    ///
    /// Panics if the date is invalid. Use `try_from_date` for fallible construction.
    pub const fn from_date(year: u16, month: u8, day: u8) -> Self {
        match Self::try_from_date(year, month, day) {
            Ok(epoch) => epoch,
            Err(error) => panic!("{}", error.as_str()),
        }
    }

    /// Create an Epoch from UNIX timestamp in seconds
    pub const fn from_seconds(secs: i64) -> Self {
        Self::new((secs * 1000) as u64)
    }

    /// Get milliseconds since UNIX epoch
    pub const fn as_millis(&self) -> u64 {
        self.millis
    }
}

impl fmt::Display for Epoch {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}ms", self.millis)
    }
}

/// Bit allocation configuration for ID generation
///
/// Specifies how the 64-bit ID space is divided between timestamp, worker, process, and sequence components.
/// All bits must sum to 64.
///
/// # Examples
///
/// ```
/// use typedflake::BitLayout;
///
/// // Use industry-standard presets
/// let twitter = BitLayout::TWITTER;  // 42t|5w|5p|12s
/// let discord = BitLayout::DISCORD;  // 42t|5w|5p|12s
///
/// // Create custom allocation
/// let custom = BitLayout::new(45, 8, 4, 7);
///
/// // Check capacity
/// assert_eq!(custom.worker_max(), 255);
/// assert_eq!(custom.ids_per_millisecond(), 128);
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct BitLayout {
    timestamp: u8,
    worker: u8,
    process: u8,
    sequence: u8,
}

impl BitLayout {
    /// Twitter Snowflake-inspired allocation (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond
    ///
    /// Note: Twitter's original uses a sign bit; we allocate it to timestamp
    pub const TWITTER: Self = Self::new(42, 5, 5, 12);

    /// Discord's allocation (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond
    pub const DISCORD: Self = Self::new(42, 5, 5, 12);

    /// Default allocation matching Config::DEFAULT (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond
    pub const DEFAULT: Self = Self::new(42, 5, 5, 12);

    /// Create a new BitLayout from individual components
    ///
    /// # Panics
    ///
    /// Panics if the bit allocation is invalid (in const context).
    /// Use `validate()` for runtime checking.
    pub const fn new(timestamp: u8, worker: u8, process: u8, sequence: u8) -> Self {
        // Const validation - will panic at compile time for const contexts
        match Self::try_new(timestamp, worker, process, sequence) {
            Ok(layout) => layout,
            Err(error) => panic!("{}", error.as_str()),
        }
    }

    /// Create a new BitLayout with runtime validation
    pub const fn try_new(
        timestamp: u8,
        worker: u8,
        process: u8,
        sequence: u8,
    ) -> Result<Self, BitLayoutError> {
        let sum = timestamp as u16 + worker as u16 + process as u16 + sequence as u16;
        if sum != 64 {
            return Err(BitLayoutError::InvalidSum { actual: sum as u8 });
        }
        if timestamp == 0 {
            return Err(BitLayoutError::ZeroTimestampBits);
        }
        if sequence == 0 {
            return Err(BitLayoutError::ZeroSequenceBits);
        }
        Ok(Self {
            timestamp,
            worker,
            process,
            sequence,
        })
    }

    /// Get timestamp bits
    pub const fn timestamp(&self) -> u8 {
        self.timestamp
    }

    /// Get worker bits
    pub const fn worker(&self) -> u8 {
        self.worker
    }

    /// Get process bits
    pub const fn process(&self) -> u8 {
        self.process
    }

    /// Get sequence bits
    pub const fn sequence(&self) -> u8 {
        self.sequence
    }

    /// Total number of possible instances (2^worker_bits × 2^process_bits)
    pub const fn total_instances(&self) -> u64 {
        (1u64 << self.worker) * (1u64 << self.process)
    }

    /// Number of IDs that can be generated per millisecond (2^sequence_bits)
    pub const fn ids_per_millisecond(&self) -> u64 {
        1u64 << self.sequence
    }

    /// Duration in milliseconds before timestamp wraps around
    pub const fn timestamp_duration_ms(&self) -> u64 {
        self.timestamp_max()
    }

    /// Duration in years before timestamp wraps around (approximate)
    pub fn timestamp_duration_years(&self) -> f64 {
        // ms -> seconds -> minutes -> hours -> days -> years
        // Avoid overflow by doing division first
        let ms = self.timestamp_max() as f64;
        ms / 1000.0 / 60.0 / 60.0 / 24.0 / 365.25
    }

    // ===== Bit Manipulation: Shifts =====

    /// Shift amount for sequence component (always 0 - sequence is in LSB)
    pub const fn sequence_shift(&self) -> u8 {
        0
    }

    /// Shift amount for process component
    pub const fn process_shift(&self) -> u8 {
        self.sequence
    }

    /// Shift amount for worker component
    pub const fn worker_shift(&self) -> u8 {
        self.sequence + self.process
    }

    /// Shift amount for timestamp component
    pub const fn timestamp_shift(&self) -> u8 {
        self.sequence + self.process + self.worker
    }

    // ===== Bit Manipulation: Maximum Values =====

    /// Maximum sequence value (2^sequence_bits - 1)
    /// Also serves as bitmask for extracting sequence component
    pub const fn sequence_max(&self) -> u64 {
        (1u64 << self.sequence) - 1
    }

    /// Maximum process ID value (2^process_bits - 1)
    /// Also serves as bitmask for extracting process component
    pub const fn process_max(&self) -> u64 {
        (1u64 << self.process) - 1
    }

    /// Maximum worker ID value (2^worker_bits - 1)
    /// Also serves as bitmask for extracting worker component
    pub const fn worker_max(&self) -> u64 {
        (1u64 << self.worker) - 1
    }

    /// Maximum timestamp value (2^timestamp_bits - 1)
    /// Also serves as bitmask for extracting timestamp component
    pub const fn timestamp_max(&self) -> u64 {
        (1u64 << self.timestamp) - 1
    }
}

impl Default for BitLayout {
    fn default() -> Self {
        Self::DEFAULT
    }
}

impl fmt::Debug for BitLayout {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BitLayout {{ timestamp: {}, worker: {}, process: {}, sequence: {} }}\n\
             Capacity: {:.1} years, {} instances, {} IDs/ms",
            self.timestamp(),
            self.worker(),
            self.process(),
            self.sequence(),
            self.timestamp_duration_years(),
            self.total_instances(),
            self.ids_per_millisecond()
        )
    }
}

impl fmt::Display for BitLayout {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(
            f,
            "BitLayout[{}t|{}w|{}p|{}s]",
            self.timestamp(),
            self.worker(),
            self.process(),
            self.sequence()
        )
    }
}

impl From<(u8, u8, u8, u8)> for BitLayout {
    fn from((timestamp, worker, process, sequence): (u8, u8, u8, u8)) -> Self {
        Self::new(timestamp, worker, process, sequence)
    }
}

/// Validation error for component bounds checking
#[derive(Display, Error, From, Debug, Clone, PartialEq, Eq)]
pub enum ValidationError {
    #[display("Timestamp {provided} exceeds maximum {maximum} (configured with {bits} bits)")]
    TimestampOutOfRange {
        provided: u64,
        maximum: u64,
        bits: u8,
    },
    #[display("Worker ID {provided} exceeds maximum {maximum} (configured with {bits} bits)")]
    WorkerIdOutOfRange {
        provided: u64,
        maximum: u64,
        bits: u8,
    },
    #[display("Process ID {provided} exceeds maximum {maximum} (configured with {bits} bits)")]
    ProcessIdOutOfRange {
        provided: u64,
        maximum: u64,
        bits: u8,
    },
    #[display("Sequence {provided} exceeds maximum {maximum} (configured with {bits} bits)")]
    SequenceOutOfRange {
        provided: u64,
        maximum: u64,
        bits: u8,
    },
    #[display("Failed to parse ID from string")]
    #[from]
    StringParseError(std::num::ParseIntError),
}

impl ValidationError {
    pub const fn as_str(&self) -> &'static str {
        match self {
            Self::TimestampOutOfRange { .. } => "Timestamp exceeds maximum",
            Self::WorkerIdOutOfRange { .. } => "Worker ID exceeds maximum",
            Self::ProcessIdOutOfRange { .. } => "Process ID exceeds maximum",
            Self::SequenceOutOfRange { .. } => "Sequence exceeds maximum",
            Self::StringParseError(_) => "Failed to parse ID from string",
        }
    }
}

/// Complete ID generation configuration (bit layout + epoch)
///
/// Combines a [`BitLayout`] (how to divide the 64-bit ID space) with an [`Epoch`]
/// (time reference point).
///
/// # Examples
///
/// ```
/// use typedflake::{BitLayout, Config, Epoch};
///
/// // Use presets
/// const TWITTER: Config = Config::TWITTER;
/// const DISCORD: Config = Config::DISCORD;
///
/// // Custom configuration
/// const CUSTOM: Config = Config::new_unchecked(
///     BitLayout::new(42, 5, 5, 12),
///     Epoch::from_date(2025, 1, 1)
/// );
/// ```
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct Config {
    layout: BitLayout,
    epoch: Epoch,
}

impl Config {
    /// Twitter Snowflake-inspired configuration (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond per worker
    /// - Epoch: Nov 4, 2010 01:42:54 UTC
    pub const TWITTER: Self = Self::new_unchecked(BitLayout::TWITTER, Epoch::TWITTER);

    /// Discord's configuration (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond per instance
    /// - Epoch: Jan 1, 2015 00:00:00 UTC
    pub const DISCORD: Self = Self::new_unchecked(BitLayout::DISCORD, Epoch::DISCORD);

    /// Default configuration (42t|5w|5p|12s)
    /// - 139 years lifespan
    /// - 1024 instances (32 workers × 32 processes)
    /// - 4096 IDs per millisecond per instance
    /// - Epoch: Jan 1, 2025 00:00:00 UTC
    pub const DEFAULT: Self = Self::new_unchecked(BitLayout::DEFAULT, Epoch::DEFAULT);

    /// Create a new Config without validation (const-compatible)
    pub const fn new_unchecked(layout: BitLayout, epoch: Epoch) -> Self {
        Config { layout, epoch }
    }

    /// Create a new Config with validation
    ///
    /// Validates that the epoch is not in the future and that elapsed time
    /// since epoch doesn't exceed the timestamp bit capacity.
    pub fn try_new(layout: BitLayout, epoch: Epoch) -> Result<Self, ConfigError> {
        // Get current time in milliseconds since UNIX epoch
        let current_ms = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("System time is before UNIX epoch")
            .as_millis() as u64;

        let epoch_ms = epoch.as_millis();

        // Validate epoch is not in the future
        if epoch_ms > current_ms {
            return Err(ConfigError::FutureEpoch {
                epoch: epoch_ms,
                current: current_ms,
            });
        }

        // Validate elapsed time doesn't exceed timestamp capacity
        let elapsed = current_ms - epoch_ms;
        let max_duration = layout.timestamp_max();

        if elapsed > max_duration {
            return Err(ConfigError::EpochExceedsCapacity {
                elapsed,
                max_duration,
                bits: layout.timestamp(),
            });
        }

        Ok(Config { layout, epoch })
    }

    /// Get the bit layout configuration
    pub const fn layout(&self) -> BitLayout {
        self.layout
    }

    /// Get the epoch timestamp
    pub const fn epoch(&self) -> Epoch {
        self.epoch
    }

    /// Get current timestamp relative to epoch
    pub(crate) fn current_timestamp_ms(&self) -> u64 {
        SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .expect("Time went backwards")
            .as_millis() as u64
            - self.epoch().as_millis()
    }

    /// Validate worker_id/process_id against bit limits
    pub fn validate_instance(
        &self,
        worker_id: u64,
        process_id: u64,
    ) -> Result<(), ValidationError> {
        if worker_id > self.layout().worker_max() {
            return Err(ValidationError::WorkerIdOutOfRange {
                provided: worker_id,
                maximum: self.layout().worker_max(),
                bits: self.layout().worker(),
            });
        }
        if process_id > self.layout().process_max() {
            return Err(ValidationError::ProcessIdOutOfRange {
                provided: process_id,
                maximum: self.layout().process_max(),
                bits: self.layout().process(),
            });
        }
        Ok(())
    }

    /// Validate all ID components against bit limits
    pub fn validate_components(
        &self,
        timestamp: u64,
        worker_id: u64,
        process_id: u64,
        sequence: u64,
    ) -> Result<(), ValidationError> {
        if timestamp > self.layout().timestamp_max() {
            return Err(ValidationError::TimestampOutOfRange {
                provided: timestamp,
                maximum: self.layout().timestamp_max(),
                bits: self.layout().timestamp(),
            });
        }
        if worker_id > self.layout().worker_max() {
            return Err(ValidationError::WorkerIdOutOfRange {
                provided: worker_id,
                maximum: self.layout().worker_max(),
                bits: self.layout().worker(),
            });
        }
        if process_id > self.layout().process_max() {
            return Err(ValidationError::ProcessIdOutOfRange {
                provided: process_id,
                maximum: self.layout().process_max(),
                bits: self.layout().process(),
            });
        }
        if sequence > self.layout().sequence_max() {
            return Err(ValidationError::SequenceOutOfRange {
                provided: sequence,
                maximum: self.layout().sequence_max(),
                bits: self.layout().sequence(),
            });
        }
        Ok(())
    }

    /// Validate a raw u64 ID by decomposing and checking component ranges
    pub fn validate_id(&self, id: u64) -> Result<(), ValidationError> {
        let timestamp = (id >> self.layout().timestamp_shift()) & self.layout().timestamp_max();
        let worker_id = (id >> self.layout().worker_shift()) & self.layout().worker_max();
        let process_id = (id >> self.layout().process_shift()) & self.layout().process_max();
        let sequence = (id >> self.layout().sequence_shift()) & self.layout().sequence_max();

        self.validate_components(timestamp, worker_id, process_id, sequence)
    }
}

impl Default for Config {
    fn default() -> Self {
        Self::new_unchecked(BitLayout::DEFAULT, Epoch::DEFAULT)
    }
}

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

    #[test]
    fn config_default() {
        let config = Config::default();
        let layout = config.layout();
        assert_eq!(
            layout.timestamp() + layout.worker() + layout.process() + layout.sequence(),
            64
        );
        assert!(layout.timestamp() > 0);
        assert!(layout.sequence() > 0);
    }

    #[test]
    fn config_new() {
        let config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));

        assert_eq!(config.layout().timestamp(), 42);
        assert_eq!(config.layout().worker(), 8);
        assert_eq!(config.layout().process(), 4);
        assert_eq!(config.layout().sequence(), 10);
        assert_eq!(config.epoch().as_millis(), 1_600_000_000_000);

        // Check max values are calculated correctly via BitLayout methods
        assert_eq!(config.layout().worker_max(), (1u64 << 8) - 1); // 255
        assert_eq!(config.layout().process_max(), (1u64 << 4) - 1); // 15
        assert_eq!(config.layout().sequence_max(), (1u64 << 10) - 1); // 1023
    }

    #[test]
    fn config_zero_process_bits() {
        let config = Config::new_unchecked(
            BitLayout::new(41, 10, 0, 13), // process_bits = 0
            Epoch::DEFAULT,
        );

        assert_eq!(config.layout().process(), 0);
        assert_eq!(config.layout().process_max(), 0);
    }

    #[test]
    fn validate_instance_boundaries_and_errors() {
        let layout = BitLayout::new(42, 8, 4, 10); // max_worker = 255, max_process = 15
        let config = Config::new_unchecked(layout, Epoch::new(1_600_000_000_000));

        // Valid instances - boundaries
        assert!(config.validate_instance(255, 15).is_ok());
        assert!(config.validate_instance(0, 0).is_ok());

        // Invalid worker_id - verify error details
        let worker_error = config.validate_instance(256, 0);
        assert!(worker_error.is_err());
        if let Err(ValidationError::WorkerIdOutOfRange {
            provided,
            maximum,
            bits,
        }) = worker_error
        {
            assert_eq!(provided, 256);
            assert_eq!(maximum, 255);
            assert_eq!(bits, 8);

            // Verify error message format
            let error_msg = format!(
                "{}",
                ValidationError::WorkerIdOutOfRange {
                    provided,
                    maximum,
                    bits,
                }
            );
            assert!(error_msg.contains("Worker ID"));
            assert!(error_msg.contains("256"));
            assert!(error_msg.contains("255"));
            assert!(error_msg.contains("8 bits"));
        } else {
            panic!("Expected WorkerIdOutOfRange error");
        }

        // Invalid process_id - verify error details
        let process_error = config.validate_instance(0, 16);
        assert!(process_error.is_err());
        if let Err(ValidationError::ProcessIdOutOfRange {
            provided,
            maximum,
            bits,
        }) = process_error
        {
            assert_eq!(provided, 16);
            assert_eq!(maximum, 15);
            assert_eq!(bits, 4);

            // Verify error message format
            let error_msg = format!(
                "{}",
                ValidationError::ProcessIdOutOfRange {
                    provided,
                    maximum,
                    bits,
                }
            );
            assert!(error_msg.contains("Process ID"));
            assert!(error_msg.contains("16"));
            assert!(error_msg.contains("15"));
            assert!(error_msg.contains("4 bits"));
        } else {
            panic!("Expected ProcessIdOutOfRange error");
        }
    }

    #[test]
    fn validate_components_success() {
        let layout = BitLayout::new(42, 8, 4, 10);
        let config = Config::new_unchecked(layout, Epoch::new(1_600_000_000_000));

        // Valid at boundaries
        let max_timestamp = (1u64 << 42) - 1;
        let max_worker = (1u64 << 8) - 1;
        let max_process = (1u64 << 4) - 1;
        let max_sequence = (1u64 << 10) - 1;

        assert!(
            config
                .validate_components(max_timestamp, max_worker, max_process, max_sequence)
                .is_ok()
        );

        // Valid at zero
        assert!(config.validate_components(0, 0, 0, 0).is_ok());

        // Valid at typical values
        assert!(config.validate_components(1000000, 50, 5, 100).is_ok());
    }

    #[test]
    fn validate_components_errors() {
        let layout = BitLayout::new(42, 8, 4, 10);
        let config = Config::new_unchecked(layout, Epoch::new(1_600_000_000_000));

        // Timestamp overflow
        let timestamp_err = config.validate_components(1u64 << 42, 0, 0, 0);
        assert!(matches!(
            timestamp_err,
            Err(ValidationError::TimestampOutOfRange { .. })
        ));

        // Worker overflow
        let worker_err = config.validate_components(0, 256, 0, 0);
        assert!(matches!(
            worker_err,
            Err(ValidationError::WorkerIdOutOfRange { .. })
        ));

        // Process overflow
        let process_err = config.validate_components(0, 0, 16, 0);
        assert!(matches!(
            process_err,
            Err(ValidationError::ProcessIdOutOfRange { .. })
        ));

        // Sequence overflow
        let sequence_err = config.validate_components(0, 0, 0, 1024);
        assert!(matches!(
            sequence_err,
            Err(ValidationError::SequenceOutOfRange { .. })
        ));
    }

    #[test]
    fn validate_id_from_raw_u64() {
        let layout = BitLayout::new(42, 8, 4, 10);
        let config = Config::new_unchecked(layout, Epoch::new(1_600_000_000_000));

        // Create a valid ID manually
        let timestamp = 1000u64;
        let worker_id = 50u64;
        let process_id = 5u64;
        let sequence = 100u64;

        let valid_id = (timestamp << layout.timestamp_shift())
            | (worker_id << layout.worker_shift())
            | (process_id << layout.process_shift())
            | (sequence << layout.sequence_shift());

        assert!(config.validate_id(valid_id).is_ok());

        // Zero ID should be valid
        assert!(config.validate_id(0).is_ok());

        // Maximum valid ID - create with all components at their max values
        let max_timestamp = (1u64 << 42) - 1;
        let max_worker = (1u64 << 8) - 1;
        let max_process = (1u64 << 4) - 1;
        let max_sequence = (1u64 << 10) - 1;

        let max_valid_id = (max_timestamp << layout.timestamp_shift())
            | (max_worker << layout.worker_shift())
            | (max_process << layout.process_shift())
            | (max_sequence << layout.sequence_shift());

        assert!(config.validate_id(max_valid_id).is_ok());
    }

    #[test]
    fn epoch_const_from_date() {
        // Test const fn from_date (panicking version)
        const EPOCH_2025: Epoch = Epoch::from_date(2025, 1, 1);
        assert_eq!(EPOCH_2025.as_millis(), 1735689600000);

        const EPOCH_2024: Epoch = Epoch::from_date(2024, 6, 15);
        assert!(EPOCH_2024.as_millis() > 0);

        // Test const fn try_from_date (Result version)
        const EPOCH_RESULT: Result<Epoch, EpochError> = Epoch::try_from_date(2025, 12, 31);
        assert!(EPOCH_RESULT.is_ok());

        // Test in Config
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::DEFAULT, Epoch::from_date(2025, 3, 15));
        assert!(CUSTOM_CONFIG.epoch().as_millis() > 0);
    }

    #[test]
    fn epoch_try_from_date_errors() {
        // Invalid year
        assert!(Epoch::try_from_date(1969, 1, 1).is_err());
        assert!(Epoch::try_from_date(2101, 1, 1).is_err());

        // Invalid month
        assert!(Epoch::try_from_date(2025, 0, 1).is_err());
        assert!(Epoch::try_from_date(2025, 13, 1).is_err());

        // Invalid day
        assert!(Epoch::try_from_date(2025, 2, 30).is_err());
        assert!(Epoch::try_from_date(2025, 4, 31).is_err());

        // Leap year edge case
        assert!(Epoch::try_from_date(2024, 2, 29).is_ok()); // 2024 is leap year
        assert!(Epoch::try_from_date(2025, 2, 29).is_err()); // 2025 is not
    }

    #[test]
    fn config_try_new_valid() {
        // Valid config with past epoch
        let layout = BitLayout::DEFAULT;
        let epoch = Epoch::new(1_600_000_000_000); // Sept 2020
        let config = Config::try_new(layout, epoch);
        assert!(config.is_ok());

        let config = config.unwrap();
        assert_eq!(config.layout(), layout);
        assert_eq!(config.epoch(), epoch);
    }

    #[test]
    fn config_try_new_future_epoch() {
        let layout = BitLayout::DEFAULT;
        // Far future epoch (year 2286)
        let future_epoch = Epoch::new(9_999_999_999_999);

        let result = Config::try_new(layout, future_epoch);
        assert!(result.is_err());

        if let Err(ConfigError::FutureEpoch { epoch, current }) = result {
            assert_eq!(epoch, 9_999_999_999_999);
            assert!(current < epoch);
        } else {
            panic!("Expected FutureEpoch error");
        }
    }

    #[test]
    fn config_try_new_epoch_exceeds_capacity() {
        // Use small timestamp bits to force capacity overflow
        let layout = BitLayout::new(20, 10, 10, 24); // Only 20 timestamp bits (~12 days)

        // Epoch from 2 years ago will exceed 20-bit capacity
        let current = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        let two_years_ago = current - (2 * 365 * 24 * 60 * 60 * 1000);
        let old_epoch = Epoch::new(two_years_ago);

        let result = Config::try_new(layout, old_epoch);
        assert!(result.is_err());

        if let Err(ConfigError::EpochExceedsCapacity {
            elapsed,
            max_duration,
            bits,
        }) = result
        {
            assert!(elapsed > max_duration);
            assert_eq!(bits, 20);
            assert_eq!(max_duration, (1u64 << 20) - 1);
        } else {
            panic!("Expected EpochExceedsCapacity error");
        }
    }

    #[test]
    fn config_try_new_edge_cases() {
        let layout = BitLayout::DEFAULT;

        // Epoch exactly at current time should work (edge case)
        let current = SystemTime::now()
            .duration_since(UNIX_EPOCH)
            .unwrap()
            .as_millis() as u64;

        let now_epoch = Epoch::new(current);
        assert!(Config::try_new(layout, now_epoch).is_ok());

        // Epoch 1 millisecond ago should work
        let one_ms_ago = Epoch::new(current - 1);
        assert!(Config::try_new(layout, one_ms_ago).is_ok());
    }

    #[test]
    fn config_try_new_with_presets() {
        // All preset epochs should be valid with their corresponding layouts
        assert!(Config::try_new(BitLayout::TWITTER, Epoch::TWITTER).is_ok());
        assert!(Config::try_new(BitLayout::DISCORD, Epoch::DISCORD).is_ok());

        // Cross-combination should also work (both are 42-bit layouts from ~2010-2015)
        assert!(Config::try_new(BitLayout::TWITTER, Epoch::DISCORD).is_ok());
        assert!(Config::try_new(BitLayout::DISCORD, Epoch::TWITTER).is_ok());
    }
}