reliakit-primitives 0.2.0

Reusable type-safe primitives for constrained and reliability-oriented Rust values.
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
use crate::{PrimitiveError, PrimitiveResult};
use alloc::string::String;
use core::{fmt, ops::Deref};

// ── Slug ─────────────────────────────────────────────────────────────────────

/// URL-safe slug: lowercase ASCII alphanumeric characters and hyphens.
///
/// Rules: non-empty, only `[a-z0-9-]`, does not start or end with `-`,
/// no consecutive `--`.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Slug(String);

impl Slug {
    /// Creates a new `Slug`. Returns `Invalid` if the value violates slug rules.
    pub fn new(value: impl Into<String>) -> PrimitiveResult<Self> {
        let value = value.into();
        if value.is_empty() {
            return Err(PrimitiveError::Empty);
        }
        if !is_valid_slug(&value) {
            return Err(PrimitiveError::Invalid {
                message: "slug must be lowercase alphanumeric with hyphens, must not start or end with a hyphen, and must not contain consecutive hyphens",
            });
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn into_inner(self) -> String {
        self.0
    }
}

fn is_valid_slug(s: &str) -> bool {
    if s.starts_with('-') || s.ends_with('-') {
        return false;
    }
    let mut prev = ' ';
    for c in s.chars() {
        if !matches!(c, 'a'..='z' | '0'..='9' | '-') {
            return false;
        }
        if c == '-' && prev == '-' {
            return false;
        }
        prev = c;
    }
    true
}

impl fmt::Display for Slug {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for Slug {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl Deref for Slug {
    type Target = str;

    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl TryFrom<&str> for Slug {
    type Error = PrimitiveError;

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

impl TryFrom<String> for Slug {
    type Error = PrimitiveError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::new(value)
    }
}

// ── Email ─────────────────────────────────────────────────────────────────────

/// Email address with basic structural validation.
///
/// Checks: exactly one `@`, non-empty local part and domain, domain contains
/// at least one `.`, no whitespace. Not a full RFC 5321 validator.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct Email(String);

impl Email {
    /// Creates a new `Email`. Returns `Invalid` if the value fails structural checks.
    pub fn new(value: impl Into<String>) -> PrimitiveResult<Self> {
        let value = value.into();
        if value.is_empty() {
            return Err(PrimitiveError::Empty);
        }
        if !is_valid_email(&value) {
            return Err(PrimitiveError::Invalid {
                message: "invalid email address",
            });
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns the local part (before `@`).
    pub fn local(&self) -> &str {
        self.0.split('@').next().unwrap_or("")
    }

    /// Returns the domain part (after `@`).
    pub fn domain(&self) -> &str {
        self.0.split('@').nth(1).unwrap_or("")
    }
}

fn is_valid_email(s: &str) -> bool {
    if s.contains(' ') {
        return false;
    }
    let at_count = s.chars().filter(|&c| c == '@').count();
    if at_count != 1 {
        return false;
    }
    let mut parts = s.splitn(2, '@');
    let local = parts.next().unwrap_or("");
    let domain = parts.next().unwrap_or("");
    if local.is_empty() || domain.is_empty() {
        return false;
    }
    if !domain.contains('.') || domain.starts_with('.') || domain.ends_with('.') {
        return false;
    }
    true
}

impl fmt::Display for Email {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for Email {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl TryFrom<&str> for Email {
    type Error = PrimitiveError;

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

// ── HttpUrl ───────────────────────────────────────────────────────────────────

/// HTTP or HTTPS URL with scheme validation.
///
/// Must start with `http://` or `https://` and have a non-empty host.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HttpUrl(String);

impl HttpUrl {
    /// Creates a new `HttpUrl`. Returns `Invalid` if the scheme is missing or
    /// the host is empty.
    pub fn new(value: impl Into<String>) -> PrimitiveResult<Self> {
        let value = value.into();
        if value.is_empty() {
            return Err(PrimitiveError::Empty);
        }
        let lower = value.to_lowercase();
        let after_scheme = if let Some(rest) = lower.strip_prefix("https://") {
            rest
        } else if let Some(rest) = lower.strip_prefix("http://") {
            rest
        } else {
            return Err(PrimitiveError::Invalid {
                message: "URL must start with http:// or https://",
            });
        };
        if after_scheme.is_empty() {
            return Err(PrimitiveError::Invalid {
                message: "URL must have a non-empty host",
            });
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns `true` if the URL uses `https`.
    pub fn is_https(&self) -> bool {
        self.0.len() >= 8 && self.0[..8].eq_ignore_ascii_case("https://")
    }
}

impl fmt::Display for HttpUrl {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for HttpUrl {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl TryFrom<&str> for HttpUrl {
    type Error = PrimitiveError;

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

// ── HexString ─────────────────────────────────────────────────────────────────

/// String of valid hexadecimal characters, with optional `0x`/`0X` prefix.
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct HexString(String);

impl HexString {
    /// Creates a new `HexString`. Returns `Invalid` if any character is not a
    /// valid hex digit (after stripping an optional `0x`/`0X` prefix).
    pub fn new(value: impl Into<String>) -> PrimitiveResult<Self> {
        let value = value.into();
        if value.is_empty() {
            return Err(PrimitiveError::Empty);
        }
        let hex_part = value
            .strip_prefix("0x")
            .or_else(|| value.strip_prefix("0X"))
            .unwrap_or(&value);
        if hex_part.is_empty() {
            return Err(PrimitiveError::Invalid {
                message: "hex string must not be empty after prefix",
            });
        }
        if !hex_part.chars().all(|c| c.is_ascii_hexdigit()) {
            return Err(PrimitiveError::Invalid {
                message: "hex string must contain only hexadecimal characters (0-9, a-f, A-F)",
            });
        }
        Ok(Self(value))
    }

    pub fn as_str(&self) -> &str {
        &self.0
    }

    pub fn into_inner(self) -> String {
        self.0
    }

    /// Returns `true` if the value was stored with a `0x`/`0X` prefix.
    pub fn has_prefix(&self) -> bool {
        self.0.starts_with("0x") || self.0.starts_with("0X")
    }

    /// Returns only the hex digit characters, without any `0x`/`0X` prefix.
    pub fn hex_digits(&self) -> &str {
        self.0
            .strip_prefix("0x")
            .or_else(|| self.0.strip_prefix("0X"))
            .unwrap_or(&self.0)
    }
}

impl fmt::Display for HexString {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(&self.0)
    }
}

impl AsRef<str> for HexString {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl TryFrom<&str> for HexString {
    type Error = PrimitiveError;

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

// ── Tests ─────────────────────────────────────────────────────────────────────

#[cfg(test)]
mod tests {
    use super::{Email, HexString, HttpUrl, Slug};
    use crate::PrimitiveError;

    // Slug
    #[test]
    fn slug_accepts_valid() {
        assert_eq!(Slug::new("my-service").unwrap().as_str(), "my-service");
        assert_eq!(Slug::new("api-v2").unwrap().as_str(), "api-v2");
        assert_eq!(Slug::new("user123").unwrap().as_str(), "user123");
    }

    #[test]
    fn slug_rejects_empty() {
        assert_eq!(Slug::new("").unwrap_err(), PrimitiveError::Empty);
    }

    #[test]
    fn slug_rejects_uppercase() {
        assert!(Slug::new("MySlug").is_err());
    }

    #[test]
    fn slug_rejects_leading_hyphen() {
        assert!(Slug::new("-bad").is_err());
    }

    #[test]
    fn slug_rejects_trailing_hyphen() {
        assert!(Slug::new("bad-").is_err());
    }

    #[test]
    fn slug_rejects_consecutive_hyphens() {
        assert!(Slug::new("bad--slug").is_err());
    }

    #[test]
    fn slug_rejects_spaces() {
        assert!(Slug::new("has space").is_err());
    }

    #[test]
    fn slug_display() {
        use alloc::string::ToString;
        assert_eq!(Slug::new("hello").unwrap().to_string(), "hello");
    }

    #[test]
    fn slug_deref() {
        let s = Slug::new("hello").unwrap();
        assert_eq!(&*s, "hello");
    }

    // Email
    #[test]
    fn email_accepts_valid() {
        let e = Email::new("user@example.com").unwrap();
        assert_eq!(e.local(), "user");
        assert_eq!(e.domain(), "example.com");
    }

    #[test]
    fn email_rejects_empty() {
        assert_eq!(Email::new("").unwrap_err(), PrimitiveError::Empty);
    }

    #[test]
    fn email_rejects_missing_at() {
        assert!(Email::new("nodomain").is_err());
    }

    #[test]
    fn email_rejects_multiple_at() {
        assert!(Email::new("a@b@c.com").is_err());
    }

    #[test]
    fn email_rejects_no_dot_in_domain() {
        assert!(Email::new("user@nodot").is_err());
    }

    #[test]
    fn email_rejects_spaces() {
        assert!(Email::new("us er@example.com").is_err());
    }

    #[test]
    fn email_display() {
        use alloc::string::ToString;
        assert_eq!(Email::new("a@b.com").unwrap().to_string(), "a@b.com");
    }

    // HttpUrl
    #[test]
    fn url_accepts_http() {
        let u = HttpUrl::new("http://example.com").unwrap();
        assert!(!u.is_https());
    }

    #[test]
    fn url_accepts_https() {
        let u = HttpUrl::new("https://example.com/path").unwrap();
        assert!(u.is_https());
    }

    #[test]
    fn url_rejects_empty() {
        assert_eq!(HttpUrl::new("").unwrap_err(), PrimitiveError::Empty);
    }

    #[test]
    fn url_rejects_missing_scheme() {
        assert!(HttpUrl::new("ftp://example.com").is_err());
    }

    #[test]
    fn url_rejects_empty_host() {
        assert!(HttpUrl::new("https://").is_err());
    }

    #[test]
    fn url_display() {
        use alloc::string::ToString;
        let u = HttpUrl::new("https://example.com").unwrap();
        assert_eq!(u.to_string(), "https://example.com");
    }

    #[test]
    fn url_is_https_uppercase_scheme() {
        let u = HttpUrl::new("HTTPS://example.com").unwrap();
        assert!(u.is_https());
    }

    #[test]
    fn url_is_http_not_https() {
        let u = HttpUrl::new("http://example.com").unwrap();
        assert!(!u.is_https());
    }

    // HexString
    #[test]
    fn hex_accepts_plain() {
        let h = HexString::new("deadbeef").unwrap();
        assert_eq!(h.hex_digits(), "deadbeef");
        assert!(!h.has_prefix());
    }

    #[test]
    fn hex_accepts_prefixed() {
        let h = HexString::new("0xdeadbeef").unwrap();
        assert_eq!(h.hex_digits(), "deadbeef");
        assert!(h.has_prefix());
    }

    #[test]
    fn hex_accepts_uppercase() {
        assert!(HexString::new("DEADBEEF").is_ok());
    }

    #[test]
    fn hex_rejects_empty() {
        assert_eq!(HexString::new("").unwrap_err(), PrimitiveError::Empty);
    }

    #[test]
    fn hex_rejects_prefix_only() {
        assert!(HexString::new("0x").is_err());
    }

    #[test]
    fn hex_rejects_invalid_chars() {
        assert!(HexString::new("xyz").is_err());
    }

    #[test]
    fn hex_display() {
        use alloc::string::ToString;
        assert_eq!(HexString::new("ff00").unwrap().to_string(), "ff00");
    }
}