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
/// Generate a type-safe ID newtype.
///
/// Creates a distinct newtype with its own static context, inherent methods, and trait implementations.
/// Each ID type is completely independent with no shared state between types.
///
/// # Examples
///
/// ```
/// // With default configuration
/// typedflake::id!(UserId);
///
/// let id = UserId::generate();
/// ```
///
/// ```
/// // With custom configuration
/// use typedflake::{Config, BitLayout, Epoch};
///
/// const CUSTOM: Config = Config::new_unchecked(BitLayout::DISCORD, Epoch::DISCORD);
/// typedflake::id!(SessionId, CUSTOM);
///
/// let id = SessionId::generate();
/// ```
#[macro_export]
macro_rules! id {
    ($name:ident) => {
        $crate::id!($name, $crate::global::get_default_config());
    };

    ($name:ident, $config:expr) => {
        $crate::__paste! {
            #[derive(Debug, Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
            pub struct $name(u64);

            /// Typed generator wrapper that produces the correct ID type
            pub struct [<$name Generator>] {
                inner: $crate::generator::Generator,
            }

            impl [<$name Generator>] {
                /// Generate an ID using pre-injected state, blocking on sequence exhaustion
                pub fn generate(&self) -> $name {
                    let id = self.inner.generate();
                    $name(id)
                }

                /// Get the worker_id this generator is bound to
                pub fn worker_id(&self) -> u64 {
                    self.inner.worker_id()
                }

                /// Get the process_id this generator is bound to
                pub fn process_id(&self) -> u64 {
                    self.inner.process_id()
                }
            }

            impl $name {
                /// Returns the singleton IdContext for this ID type (lazy static initialization)
                fn context() -> &'static $crate::context::IdContext {
                    static CONTEXT: std::sync::OnceLock<$crate::context::IdContext> =
                        std::sync::OnceLock::new();
                    CONTEXT.get_or_init(|| $crate::context::IdContext::new($config))
                }

                /// Generate a new ID with default instance, blocking until next millisecond if sequence is exhausted
                pub fn generate() -> Self {
                    let id = Self::context().default_generator().generate();
                    Self(id)
                }

                /// Create a typed generator wrapper with specific worker_id and process_id
                pub fn instance(worker_id: u64, process_id: u64) -> Result<[<$name Generator>], $crate::config::ValidationError> {
                    let inner = Self::context().create_generator(worker_id, process_id)?;
                    Ok([<$name Generator>] { inner })
                }

                /// Create a typed generator wrapper with specific worker_id and default process_id
                pub fn worker(worker_id: u64) -> Result<[<$name Generator>], $crate::config::ValidationError> {
                    let inner = Self::context().create_worker(worker_id)?;
                    Ok([<$name Generator>] { inner })
                }

                /// Create a typed generator wrapper with default worker_id and specific process_id
                pub fn process(process_id: u64) -> Result<[<$name Generator>], $crate::config::ValidationError> {
                    let inner = Self::context().create_process(process_id)?;
                    Ok([<$name Generator>] { inner })
                }

                /// Decompose the ID into its components as a tuple
                pub fn decompose(self) -> (u64, u64, u64, u64) {
                    Self::context().default_generator().decompose(self.0)
                }

                /// Get the ID components as a struct
                pub fn components(self) -> $crate::generator::IdComponents {
                    Self::context().default_generator().components(self.0)
                }

                /// Get just the timestamp component
                pub fn timestamp(self) -> u64 {
                    Self::context().default_generator().extract_timestamp(self.0)
                }

                /// Get just the worker ID component
                pub fn worker_id(self) -> u64 {
                    Self::context().default_generator().extract_worker_id(self.0)
                }

                /// Get just the process ID component
                pub fn process_id(self) -> u64 {
                    Self::context().default_generator().extract_process_id(self.0)
                }

                /// Get just the sequence component
                pub fn sequence(self) -> u64 {
                    Self::context().default_generator().extract_sequence(self.0)
                }

                /// Compose ID with validation (uses default worker_id and process_id)
                pub fn compose(timestamp: u64, sequence: u64) -> Result<Self, $crate::config::ValidationError> {
                    let id = Self::context().default_generator().compose(timestamp, sequence)?;
                    Ok(Self(id))
                }

                /// Compose ID without validation (uses defaults, masks overflow)
                pub fn compose_unchecked(timestamp: u64, sequence: u64) -> Self {
                    let id = Self::context().default_generator().compose_unchecked(timestamp, sequence);
                    Self(id)
                }

                /// Compose an ID from all components with validation
                pub fn compose_custom(
                    timestamp: u64,
                    worker_id: u64,
                    process_id: u64,
                    sequence: u64,
                ) -> Result<Self, $crate::config::ValidationError> {
                    let id = Self::context().default_generator().compose_custom(timestamp, worker_id, process_id, sequence)?;
                    Ok(Self(id))
                }

                /// Compose from all components without validation (values exceeding limits are masked)
                pub fn compose_custom_unchecked(timestamp: u64, worker_id: u64, process_id: u64, sequence: u64) -> Self {
                    let id = Self::context().default_generator().compose_custom_unchecked(timestamp, worker_id, process_id, sequence);
                    Self(id)
                }
            }

            // Additional methods for conversions and validation
            impl $name {
                /// Create an ID from a raw u64 value without validation.
                pub fn from_u64_unchecked(id: u64) -> Self {
                    Self(id)
                }

                /// Create an ID from a raw u64 value with validation.
                pub fn try_from_u64(id: u64) -> Result<Self, $crate::config::ValidationError> {
                    Self::context().config().validate_id(id)?;
                    Ok(Self(id))
                }

                /// Get the raw u64 value of this ID
                pub fn as_u64(self) -> u64 {
                    self.0
                }
            }

            impl std::fmt::Display for $name {
                fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
                    write!(f, "{}", self.0)
                }
            }

            impl std::convert::TryFrom<u64> for $name {
                type Error = $crate::config::ValidationError;

                fn try_from(id: u64) -> Result<Self, Self::Error> {
                    Self::try_from_u64(id)
                }
            }

            impl From<$name> for u64 {
                fn from(id: $name) -> u64 {
                    id.0
                }
            }

            impl std::str::FromStr for $name {
                type Err = $crate::config::ValidationError;

                fn from_str(s: &str) -> Result<Self, Self::Err> {
                    let id = s.parse::<u64>()?;
                    Self::try_from_u64(id)
                }
            }

            // Conditionally include serde impls (uses helper macro to avoid cfg in expansion)
            $crate::__impl_serde_for_id!($name);
        }
    };
}

// Helper macro for serde support (only available when serde feature is enabled)
#[cfg(feature = "serde")]
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_serde_for_id {
    ($name:ident) => {
        // Serialize as string for JavaScript compatibility (IEEE 754 safety)
        impl $crate::__serde::Serialize for $name {
            fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
            where
                S: $crate::__serde::Serializer,
            {
                serializer.serialize_str(&self.to_string())
            }
        }

        impl<'de> $crate::__serde::Deserialize<'de> for $name {
            fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
            where
                D: $crate::__serde::Deserializer<'de>,
            {
                String::deserialize(deserializer)?
                    .parse()
                    .map_err($crate::__serde::de::Error::custom)
            }
        }
    };
}

// No-op version when serde is not enabled
#[cfg(not(feature = "serde"))]
#[macro_export]
#[doc(hidden)]
macro_rules! __impl_serde_for_id {
    ($name:ident) => {};
}

#[cfg(test)]
mod tests {
    use crate::{BitLayout, Config, Epoch};

    #[test]
    fn macro_generates_default_config() {
        crate::id!(TestId);

        let id1 = TestId::generate();
        let id2 = TestId::generate();

        assert_ne!(id1, id2);
        assert!(id1.as_u64() > 0);
        assert!(id2.as_u64() > 0);

        let components = id1.components();
        assert_eq!(components.worker_id, 0);
        assert_eq!(components.process_id, 0);
    }

    #[test]
    fn macro_with_custom_config() {
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));

        crate::id!(CustomId, CUSTOM_CONFIG);

        // Test default instance (unconfigured default is 0, 0)
        let id = CustomId::generate();
        let components = id.components();

        assert_eq!(components.worker_id, 0);
        assert_eq!(components.process_id, 0);

        // Test specific instance
        let instance = CustomId::instance(50, 5).unwrap();
        let instance_id = instance.generate();
        let instance_components = instance_id.components();

        assert_eq!(instance_components.worker_id, 50);
        assert_eq!(instance_components.process_id, 5);
    }

    #[test]
    fn compose_decompose_roundtrip() {
        crate::id!(ComposeId);

        let timestamp = 123456789;
        let sequence = 100;

        // Test compose (uses default worker=0, process=0)
        let id = ComposeId::compose(timestamp, sequence).unwrap();
        let (dec_timestamp, dec_worker_id, dec_process_id, dec_sequence) = id.decompose();

        assert_eq!(dec_timestamp, timestamp);
        assert_eq!(dec_worker_id, 0);
        assert_eq!(dec_process_id, 0);
        assert_eq!(dec_sequence, sequence);

        // Test compose_custom
        let id_custom = ComposeId::compose_custom(timestamp, 5, 3, sequence).unwrap();
        let (ts, w, p, s) = id_custom.decompose();
        assert_eq!(ts, timestamp);
        assert_eq!(w, 5);
        assert_eq!(p, 3);
        assert_eq!(s, sequence);
    }

    #[test]
    fn extract_individual_components() {
        crate::id!(ComponentId);

        let id = ComponentId::generate();

        let timestamp = id.timestamp();
        let worker_id = id.worker_id();
        let process_id = id.process_id();
        let sequence = id.sequence();

        let components = id.components();

        assert_eq!(timestamp, components.timestamp);
        assert_eq!(worker_id, components.worker_id);
        assert_eq!(process_id, components.process_id);
        assert_eq!(sequence, components.sequence);
    }

    #[test]
    fn u64_conversions() {
        crate::id!(ConversionId);

        let id = ConversionId::generate();
        let raw = id.as_u64();

        let id2 = ConversionId::from_u64_unchecked(raw);
        assert_eq!(id, id2);

        let id3: ConversionId = raw.try_into().unwrap();
        assert_eq!(id, id3);

        let raw2: u64 = id.into();
        assert_eq!(raw, raw2);
    }

    #[test]
    fn string_display_and_parsing() {
        crate::id!(ParseId);

        let id = ParseId::generate();
        let id_str = id.to_string();

        let parsed_id: ParseId = id_str.parse().unwrap();
        assert_eq!(id, parsed_id);
    }

    #[test]
    fn multiple_types_independent() {
        crate::id!(UserId);
        crate::id!(OrderId);

        let user_id1 = UserId::generate();
        let user_id2 = UserId::generate();
        let order_id1 = OrderId::generate();
        let order_id2 = OrderId::generate();

        // Verify that each type generates different IDs
        assert_ne!(user_id1, user_id2);
        assert_ne!(order_id1, order_id2);

        // Verify that IDs are valid u64s
        assert!(user_id1.as_u64() > 0);
        assert!(order_id1.as_u64() > 0);

        // Different types can't be compared directly - they're different types!
        // But we can verify their raw u64 values are different due to different timing
        // or at least that the system is working correctly
        assert!(user_id1.as_u64() != user_id2.as_u64());
        assert!(order_id1.as_u64() != order_id2.as_u64());
    }

    #[test]
    fn instance_convenience_methods() {
        const CUSTOM_ALGORITHM: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));

        crate::id!(InstanceMethodId, CUSTOM_ALGORITHM);

        // Test worker method
        let worker_instance = InstanceMethodId::worker(50).unwrap();
        let worker_id = worker_instance.generate();
        let worker_components = worker_id.components();
        assert_eq!(worker_components.worker_id, 50);
        assert_eq!(worker_components.process_id, 0);

        // Test process method
        let process_instance = InstanceMethodId::process(5).unwrap();
        let process_id = process_instance.generate();
        let process_components = process_id.components();
        assert_eq!(process_components.worker_id, 0);
        assert_eq!(process_components.process_id, 5);

        // Test instance method
        let full_instance = InstanceMethodId::instance(50, 5).unwrap();
        let full_id = full_instance.generate();
        let full_components = full_id.components();
        assert_eq!(full_components.worker_id, 50);
        assert_eq!(full_components.process_id, 5);
    }

    #[test]
    fn multiple_instances_independent_state() {
        crate::id!(MultiInstanceId);

        // Test that different instances work independently
        let instance1 = MultiInstanceId::instance(10, 5).unwrap();
        let instance2 = MultiInstanceId::instance(1, 2).unwrap();

        // Generate IDs from both instances
        let id1 = instance1.generate();
        let id2 = instance2.generate();

        let components1 = id1.components();
        let components2 = id2.components();

        assert_eq!(components1.worker_id, 10);
        assert_eq!(components1.process_id, 5);
        assert_eq!(components2.worker_id, 1);
        assert_eq!(components2.process_id, 2);
    }

    #[test]
    fn generator_with_bound_ids() {
        crate::id!(FactoryTestId);

        // Test Generator creation and usage
        let stateful_gen = FactoryTestId::instance(15, 7).unwrap();

        // Verify bound worker and process IDs
        assert_eq!(stateful_gen.worker_id(), 15);
        assert_eq!(stateful_gen.process_id(), 7);

        // Generate IDs with pre-injected state (no lookup overhead)
        let id1 = stateful_gen.generate();
        let id2 = stateful_gen.generate();

        // Verify IDs have correct components
        let components1 = id1.components();
        let components2 = id2.components();

        assert_eq!(components1.worker_id, 15);
        assert_eq!(components1.process_id, 7);
        assert_eq!(components2.worker_id, 15);
        assert_eq!(components2.process_id, 7);

        // IDs should be different
        assert_ne!(id1, id2);
    }

    #[test]
    fn try_from_u64_validates_components() {
        const CUSTOM_CONFIG: Config = Config::new_unchecked(
            BitLayout::new(42, 8, 4, 10), // max: ts=4.4T, worker=255, process=15, seq=1023
            Epoch::new(1_600_000_000_000),
        );
        crate::id!(ValidationTestId, CUSTOM_CONFIG);

        // Generate a valid ID
        let valid_id = ValidationTestId::generate();
        let raw = valid_id.as_u64();

        // Valid ID should pass validation
        assert!(ValidationTestId::try_from_u64(raw).is_ok());

        // Note: Manually crafted IDs with simple bit shifts get masked during composition,
        // so they appear valid after extraction. Validation catches component overflow
        // during compose() rather than from arbitrary u64 values.
        // Generated IDs are always valid by construction.
    }

    #[test]
    fn try_from_trait_validates() {
        crate::id!(TryFromTestId);

        let valid_id = TryFromTestId::generate();
        let raw = valid_id.as_u64();

        // TryFrom should work for valid IDs
        let converted: Result<TryFromTestId, _> = raw.try_into();
        assert!(converted.is_ok());
        assert_eq!(converted.unwrap(), valid_id);
    }

    #[test]
    fn compose_validates_components() {
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));
        crate::id!(ComposeValidationId, CUSTOM_CONFIG);

        // Valid composition should succeed
        let result = ComposeValidationId::compose_custom(1000, 100, 5, 500);
        assert!(result.is_ok());

        // Invalid worker_id (256 > 255)
        let result = ComposeValidationId::compose_custom(1000, 256, 5, 500);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(crate::config::ValidationError::WorkerIdOutOfRange { .. })
        ));

        // Invalid process_id (16 > 15)
        let result = ComposeValidationId::compose_custom(1000, 100, 16, 500);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(crate::config::ValidationError::ProcessIdOutOfRange { .. })
        ));

        // Invalid sequence (1024 > 1023)
        let result = ComposeValidationId::compose_custom(1000, 100, 5, 1024);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(crate::config::ValidationError::SequenceOutOfRange { .. })
        ));

        // Invalid timestamp (2^42 > 2^42-1)
        let result = ComposeValidationId::compose_custom(1u64 << 42, 100, 5, 500);
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(crate::config::ValidationError::TimestampOutOfRange { .. })
        ));

        // Test compose (2-param) validates timestamp and sequence
        let result = ComposeValidationId::compose(1000, 500);
        assert!(result.is_ok());

        let result = ComposeValidationId::compose(1u64 << 42, 500);
        assert!(result.is_err());

        let result = ComposeValidationId::compose(1000, 1024);
        assert!(result.is_err());
    }

    #[test]
    fn compose_unchecked_masks_overflow() {
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));
        crate::id!(UncheckedComposeId, CUSTOM_CONFIG);

        // compose_custom_unchecked should mask values that exceed limits
        let id = UncheckedComposeId::compose_custom_unchecked(1u64 << 42, 256, 16, 1024);

        // Verify components are masked (not validated)
        let components = id.components();
        assert_eq!(components.timestamp, 0); // (1 << 42) masked to 42 bits = 0
        assert_eq!(components.worker_id, 0); // 256 masked to 8 bits = 0
        assert_eq!(components.process_id, 0); // 16 masked to 4 bits = 0
        assert_eq!(components.sequence, 0); // 1024 masked to 10 bits = 0

        // compose_unchecked (2-param) should mask timestamp/sequence
        let id2 = UncheckedComposeId::compose_unchecked(1u64 << 42, 1024);
        let comp2 = id2.components();
        assert_eq!(comp2.timestamp, 0);
        assert_eq!(comp2.sequence, 0);
    }

    #[test]
    fn from_str_validates() {
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));
        crate::id!(ParseValidationId, CUSTOM_CONFIG);

        // Valid ID string should parse
        let valid_id = ParseValidationId::generate();
        let id_str = valid_id.to_string();
        let parsed: Result<ParseValidationId, _> = id_str.parse();
        assert!(parsed.is_ok());
        assert_eq!(parsed.unwrap(), valid_id);

        // Invalid format should fail
        let result: Result<ParseValidationId, _> = "not_a_number".parse();
        assert!(result.is_err());
        assert!(matches!(
            result,
            Err(crate::config::ValidationError::StringParseError(_))
        ));
    }

    #[test]
    fn from_u64_unchecked_no_validation() {
        const CUSTOM_CONFIG: Config =
            Config::new_unchecked(BitLayout::new(42, 8, 4, 10), Epoch::new(1_600_000_000_000));
        crate::id!(UncheckedFromId, CUSTOM_CONFIG);

        // from_u64_unchecked should accept any value
        let layout = CUSTOM_CONFIG.layout();
        let invalid_value = 256u64 << layout.worker_shift(); // worker_id=256 > max=255

        // Should not panic or error
        let id = UncheckedFromId::from_u64_unchecked(invalid_value);
        assert_eq!(id.as_u64(), invalid_value);
    }
}