stilltypes 0.2.0

Domain-specific refined types for the Rust and Stillwater ecosystem
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
//! Identifier validation types.
//!
//! Provides validated types for common identifier formats like URL slugs,
//! usernames, and product SKUs. These types encode validity at the type level—
//! once you have a `Slug`, it's guaranteed to be a valid URL path component.
//!
//! # Philosophy
//!
//! This module follows the Stillwater philosophy:
//! - **Parse, Don't Validate**: `Slug` encodes URL-safety at the type level
//! - **Errors Should Tell Stories**: Errors identify invalid characters and positions
//! - **Composition Over Complexity**: Simple predicates for character validation
//! - **Pragmatism Over Purity**: `from_title()` converts prose to slugs for convenience
//!
//! # Example
//!
//! ```
//! # #[cfg(feature = "identifiers")]
//! # {
//! use stilltypes::identifiers::{Slug, SlugExt};
//!
//! // Validate existing slug
//! let slug = Slug::new("my-first-post".to_string()).unwrap();
//! assert_eq!(slug.get(), "my-first-post");
//!
//! // Convert from title
//! let slug = Slug::from_title("My First Blog Post!").unwrap();
//! assert_eq!(slug.get(), "my-first-blog-post");
//! # }
//! ```

use crate::error::{DomainError, DomainErrorKind};
use stillwater::refined::{Predicate, Refined};

/// Maximum length for a slug (reasonable for URLs).
const MAX_SLUG_LENGTH: usize = 128;

// ============================================================================
// Slug
// ============================================================================

/// Predicate for valid URL slugs.
///
/// Validates strings matching the pattern `^[a-z0-9]+(-[a-z0-9]+)*$`:
/// - Only lowercase letters, digits, and hyphens allowed
/// - Must start with a letter or digit
/// - Must end with a letter or digit
/// - No consecutive hyphens (`--`)
/// - Length: 1-128 characters
///
/// # Example
///
/// ```
/// # #[cfg(feature = "identifiers")]
/// # {
/// use stilltypes::identifiers::Slug;
///
/// let slug = Slug::new("my-first-post".to_string());
/// assert!(slug.is_ok());
///
/// let invalid = Slug::new("My First Post".to_string());
/// assert!(invalid.is_err());
/// # }
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct ValidSlug;

impl Predicate<String> for ValidSlug {
    type Error = DomainError;

    fn check(value: &String) -> Result<(), Self::Error> {
        // Empty check
        if value.is_empty() {
            return Err(DomainError {
                format_name: "slug",
                value: value.clone(),
                reason: DomainErrorKind::Empty,
                example: "my-first-post",
            });
        }

        // Length check
        if value.len() > MAX_SLUG_LENGTH {
            return Err(DomainError {
                format_name: "slug",
                value: format!("{}...", &value[..32.min(value.len())]),
                reason: DomainErrorKind::TooLong {
                    max: MAX_SLUG_LENGTH,
                    actual: value.len(),
                },
                example: "my-first-post",
            });
        }

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

        // Check first character - must be alphanumeric
        if !chars[0].is_ascii_lowercase() && !chars[0].is_ascii_digit() {
            return Err(DomainError {
                format_name: "slug",
                value: value.clone(),
                reason: DomainErrorKind::InvalidCharacter {
                    char: chars[0],
                    position: 0,
                },
                example: "my-first-post",
            });
        }

        // Check last character - must be alphanumeric
        let last = chars.len() - 1;
        if !chars[last].is_ascii_lowercase() && !chars[last].is_ascii_digit() {
            return Err(DomainError {
                format_name: "slug",
                value: value.clone(),
                reason: DomainErrorKind::InvalidCharacter {
                    char: chars[last],
                    position: last,
                },
                example: "my-first-post",
            });
        }

        // Check all characters and consecutive hyphens
        let mut prev_hyphen = false;
        for (i, c) in chars.iter().enumerate() {
            if *c == '-' {
                if prev_hyphen {
                    return Err(DomainError {
                        format_name: "slug",
                        value: value.clone(),
                        reason: DomainErrorKind::InvalidFormat {
                            expected: "no consecutive hyphens",
                        },
                        example: "my-first-post",
                    });
                }
                prev_hyphen = true;
            } else if c.is_ascii_lowercase() || c.is_ascii_digit() {
                prev_hyphen = false;
            } else {
                return Err(DomainError {
                    format_name: "slug",
                    value: value.clone(),
                    reason: DomainErrorKind::InvalidCharacter {
                        char: *c,
                        position: i,
                    },
                    example: "my-first-post",
                });
            }
        }

        Ok(())
    }

    fn description() -> &'static str {
        "URL-safe slug (lowercase alphanumeric with hyphens)"
    }
}

/// A validated URL-safe slug.
///
/// A `String` that has been validated to be a properly formatted slug:
/// - Only lowercase letters, digits, and hyphens
/// - Starts and ends with alphanumeric characters
/// - No consecutive hyphens
/// - 1-128 characters
///
/// # Example
///
/// ```
/// # #[cfg(feature = "identifiers")]
/// # {
/// use stilltypes::identifiers::Slug;
///
/// let slug = Slug::new("my-first-post".to_string()).unwrap();
/// assert_eq!(slug.get(), "my-first-post");
/// # }
/// ```
pub type Slug = Refined<String, ValidSlug>;

/// Extension trait for slug operations.
///
/// Provides helper methods for working with slugs, including conversion
/// from prose titles.
pub trait SlugExt {
    /// Convert a title or prose string into a valid slug.
    ///
    /// Performs the following transformations:
    /// - Converts to lowercase
    /// - Replaces spaces, underscores, and non-alphanumeric chars with hyphens
    /// - Collapses consecutive hyphens into single hyphens
    /// - Trims leading and trailing hyphens
    ///
    /// # Example
    ///
    /// ```
    /// # #[cfg(feature = "identifiers")]
    /// # {
    /// use stilltypes::identifiers::{Slug, SlugExt};
    ///
    /// let slug = Slug::from_title("My First Blog Post!").unwrap();
    /// assert_eq!(slug.get(), "my-first-blog-post");
    ///
    /// let slug = Slug::from_title("  Spaces  Everywhere  ").unwrap();
    /// assert_eq!(slug.get(), "spaces-everywhere");
    /// # }
    /// ```
    ///
    /// # Errors
    ///
    /// Returns an error if the resulting slug would be empty (e.g., input
    /// contains only special characters).
    fn from_title(title: &str) -> Result<Slug, DomainError>;
}

impl SlugExt for Slug {
    fn from_title(title: &str) -> Result<Slug, DomainError> {
        // Convert to lowercase and replace non-alphanumeric with hyphens
        let slug: String = title
            .to_lowercase()
            .chars()
            .map(|c| if c.is_ascii_alphanumeric() { c } else { '-' })
            .collect::<String>()
            // Split by hyphens, filter empty parts, rejoin
            .split('-')
            .filter(|s| !s.is_empty())
            .collect::<Vec<_>>()
            .join("-");

        if slug.is_empty() {
            return Err(DomainError {
                format_name: "slug",
                value: title.to_string(),
                reason: DomainErrorKind::Empty,
                example: "my-first-post",
            });
        }

        Slug::new(slug)
    }
}

// ============================================================================
// Tests
// ============================================================================

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

    // ========================================================================
    // Slug Validation Tests
    // ========================================================================

    mod valid_slugs {
        use super::*;

        #[test]
        fn single_letter() {
            assert!(Slug::new("a".to_string()).is_ok());
        }

        #[test]
        fn single_digit() {
            assert!(Slug::new("1".to_string()).is_ok());
        }

        #[test]
        fn simple_word() {
            assert!(Slug::new("hello".to_string()).is_ok());
        }

        #[test]
        fn hyphenated() {
            assert!(Slug::new("hello-world".to_string()).is_ok());
        }

        #[test]
        fn with_numbers() {
            assert!(Slug::new("post-123".to_string()).is_ok());
        }

        #[test]
        fn multiple_segments() {
            assert!(Slug::new("a1-b2-c3".to_string()).is_ok());
        }

        #[test]
        fn starts_with_digit() {
            assert!(Slug::new("123-numbers-first".to_string()).is_ok());
        }

        #[test]
        fn ends_with_digit() {
            assert!(Slug::new("chapter-1".to_string()).is_ok());
        }

        #[test]
        fn max_length() {
            let slug = "a".repeat(128);
            assert!(Slug::new(slug).is_ok());
        }

        #[test]
        fn mixed_alphanumeric_segments() {
            assert!(Slug::new("v1-alpha-2-beta".to_string()).is_ok());
        }
    }

    mod invalid_slugs {
        use super::*;

        #[test]
        fn empty() {
            let result = Slug::new(String::new());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(err.reason, DomainErrorKind::Empty));
        }

        #[test]
        fn uppercase() {
            let result = Slug::new("Hello".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidCharacter {
                    char: 'H',
                    position: 0
                }
            ));
        }

        #[test]
        fn all_uppercase() {
            let result = Slug::new("HELLO".to_string());
            assert!(result.is_err());
        }

        #[test]
        fn spaces() {
            let result = Slug::new("hello world".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidCharacter {
                    char: ' ',
                    position: 5
                }
            ));
        }

        #[test]
        fn special_characters() {
            let result = Slug::new("hello@world".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidCharacter {
                    char: '@',
                    position: 5
                }
            ));
        }

        #[test]
        fn dot() {
            let result = Slug::new("hello.world".to_string());
            assert!(result.is_err());
        }

        #[test]
        fn consecutive_hyphens() {
            let result = Slug::new("hello--world".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidFormat {
                    expected: "no consecutive hyphens"
                }
            ));
        }

        #[test]
        fn triple_hyphens() {
            let result = Slug::new("a---b".to_string());
            assert!(result.is_err());
        }

        #[test]
        fn leading_hyphen() {
            let result = Slug::new("-hello".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidCharacter {
                    char: '-',
                    position: 0
                }
            ));
        }

        #[test]
        fn trailing_hyphen() {
            let result = Slug::new("hello-".to_string());
            assert!(result.is_err());
            let err = result.unwrap_err();
            let len = "hello-".len() - 1;
            assert!(matches!(
                err.reason,
                DomainErrorKind::InvalidCharacter { char: '-', position: p } if p == len
            ));
        }

        #[test]
        fn too_long() {
            let slug = "a".repeat(129);
            let result = Slug::new(slug);
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(
                err.reason,
                DomainErrorKind::TooLong {
                    max: 128,
                    actual: 129
                }
            ));
        }

        #[test]
        fn underscore() {
            let result = Slug::new("hello_world".to_string());
            assert!(result.is_err());
        }
    }

    // ========================================================================
    // from_title() Tests
    // ========================================================================

    mod from_title {
        use super::*;

        #[test]
        fn simple_title() {
            let slug = Slug::from_title("My First Post").unwrap();
            assert_eq!(slug.get(), "my-first-post");
        }

        #[test]
        fn with_extra_spaces() {
            let slug = Slug::from_title("  Spaces  Everywhere  ").unwrap();
            assert_eq!(slug.get(), "spaces-everywhere");
        }

        #[test]
        fn with_special_characters() {
            let slug = Slug::from_title("Special!@#Characters").unwrap();
            assert_eq!(slug.get(), "special-characters");
        }

        #[test]
        fn already_a_slug() {
            let slug = Slug::from_title("already-a-slug").unwrap();
            assert_eq!(slug.get(), "already-a-slug");
        }

        #[test]
        fn mixed_case_slug() {
            let slug = Slug::from_title("Already-A-Slug").unwrap();
            assert_eq!(slug.get(), "already-a-slug");
        }

        #[test]
        fn numbers_first() {
            let slug = Slug::from_title("123 Numbers First").unwrap();
            assert_eq!(slug.get(), "123-numbers-first");
        }

        #[test]
        fn with_apostrophe() {
            let slug = Slug::from_title("It's a Test").unwrap();
            assert_eq!(slug.get(), "it-s-a-test");
        }

        #[test]
        fn with_punctuation() {
            let slug = Slug::from_title("Hello, World! How's it going?").unwrap();
            assert_eq!(slug.get(), "hello-world-how-s-it-going");
        }

        #[test]
        fn empty_result_error() {
            let result = Slug::from_title("!@#$%^&*()");
            assert!(result.is_err());
            let err = result.unwrap_err();
            assert!(matches!(err.reason, DomainErrorKind::Empty));
        }

        #[test]
        fn empty_input() {
            let result = Slug::from_title("");
            assert!(result.is_err());
        }

        #[test]
        fn only_spaces() {
            let result = Slug::from_title("   ");
            assert!(result.is_err());
        }

        #[test]
        fn underscores_converted() {
            let slug = Slug::from_title("snake_case_title").unwrap();
            assert_eq!(slug.get(), "snake-case-title");
        }

        #[test]
        fn tabs_and_newlines() {
            let slug = Slug::from_title("tabs\tand\nnewlines").unwrap();
            assert_eq!(slug.get(), "tabs-and-newlines");
        }

        #[test]
        fn unicode_removed() {
            let slug = Slug::from_title("Café Résumé").unwrap();
            assert_eq!(slug.get(), "caf-r-sum");
        }

        #[test]
        fn emoji_removed() {
            let slug = Slug::from_title("Hello 🎉 World").unwrap();
            assert_eq!(slug.get(), "hello-world");
        }
    }

    // ========================================================================
    // Error Message Tests
    // ========================================================================

    mod error_messages {
        use super::*;

        #[test]
        fn error_includes_format_name() {
            let result = Slug::new("Invalid Slug".to_string());
            let err = result.unwrap_err();
            assert_eq!(err.format_name, "slug");
        }

        #[test]
        fn error_includes_example() {
            let result = Slug::new("Invalid".to_string());
            let err = result.unwrap_err();
            assert_eq!(err.example, "my-first-post");
        }

        #[test]
        fn error_display_format() {
            let result = Slug::new("Hello World".to_string());
            let err = result.unwrap_err();
            let msg = err.to_string();
            assert!(msg.contains("slug"));
            assert!(msg.contains("my-first-post"));
        }

        #[test]
        fn description_returns_expected() {
            assert_eq!(
                ValidSlug::description(),
                "URL-safe slug (lowercase alphanumeric with hyphens)"
            );
        }
    }

    // ========================================================================
    // Edge Cases
    // ========================================================================

    mod edge_cases {
        use super::*;

        #[test]
        fn single_char_slug() {
            assert!(Slug::new("a".to_string()).is_ok());
            assert!(Slug::new("1".to_string()).is_ok());
            assert!(Slug::new("-".to_string()).is_err());
        }

        #[test]
        fn two_char_slug_with_hyphen() {
            // "a-" is invalid (trailing hyphen)
            assert!(Slug::new("a-".to_string()).is_err());
            // "-a" is invalid (leading hyphen)
            assert!(Slug::new("-a".to_string()).is_err());
        }

        #[test]
        fn three_char_slug_with_hyphen() {
            // "a-b" is valid
            assert!(Slug::new("a-b".to_string()).is_ok());
        }

        #[test]
        fn boundary_length_127() {
            let slug = "a".repeat(127);
            assert!(Slug::new(slug).is_ok());
        }

        #[test]
        fn boundary_length_128() {
            let slug = "a".repeat(128);
            assert!(Slug::new(slug).is_ok());
        }

        #[test]
        fn boundary_length_129() {
            let slug = "a".repeat(129);
            assert!(Slug::new(slug).is_err());
        }
    }
}