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
//! URL validation types.
//!
//! Provides RFC 3986 compliant URL validation using the `url` crate (WHATWG URL Standard).
//!
//! This module demonstrates stillwater's predicate composition using the `And` combinator:
//! - `Url` - any valid RFC 3986 URL
//! - `HttpUrl` - composed as `And<ValidUrl, HttpScheme>`
//! - `SecureUrl` - composed as `And<ValidUrl, HttpsOnly>`
//!
//! # Example
//!
//! ```
//! use stilltypes::url::{Url, HttpUrl, SecureUrl};
//!
//! // Any valid URL
//! let url = Url::new("https://example.com".to_string());
//! assert!(url.is_ok());
//!
//! // HTTP or HTTPS only
//! let http = HttpUrl::new("http://example.com".to_string());
//! assert!(http.is_ok());
//!
//! // HTTPS only (secure)
//! let https = SecureUrl::new("https://example.com".to_string());
//! assert!(https.is_ok());
//!
//! let insecure = SecureUrl::new("http://example.com".to_string());
//! assert!(insecure.is_err());
//! ```

use crate::error::{DomainError, DomainErrorKind};
use stillwater::refined::{And, Predicate, Refined};
use url::Url as UrlParser;

/// Any valid RFC 3986 URL.
///
/// Uses the `url` crate for parsing, which implements the WHATWG URL Standard.
///
/// # Example
///
/// ```
/// use stilltypes::url::Url;
///
/// let url = Url::new("https://example.com/path".to_string());
/// assert!(url.is_ok());
///
/// let invalid = Url::new("not a url".to_string());
/// assert!(invalid.is_err());
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct ValidUrl;

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

    fn check(value: &String) -> Result<(), Self::Error> {
        UrlParser::parse(value)
            .map(|_| ())
            .map_err(|_| DomainError {
                format_name: "URL",
                value: value.clone(),
                reason: DomainErrorKind::InvalidFormat {
                    expected: "scheme://host/path",
                },
                example: "https://example.com",
            })
    }

    fn description() -> &'static str {
        "RFC 3986 URL"
    }
}

/// URL scheme must be http or https.
///
/// This predicate validates only the scheme, not the overall URL validity.
/// For a complete HTTP URL, use `HttpUrl` which combines `ValidUrl` and `HttpScheme`.
///
/// # Example
///
/// ```
/// use stilltypes::url::HttpUrl;
///
/// let http = HttpUrl::new("http://example.com".to_string());
/// assert!(http.is_ok());
///
/// let ftp = HttpUrl::new("ftp://example.com".to_string());
/// assert!(ftp.is_err());
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct HttpScheme;

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

    fn check(value: &String) -> Result<(), Self::Error> {
        let parsed = UrlParser::parse(value).map_err(|_| DomainError {
            format_name: "HTTP URL",
            value: value.clone(),
            reason: DomainErrorKind::InvalidFormat {
                expected: "valid URL",
            },
            example: "https://example.com",
        })?;

        match parsed.scheme() {
            "http" | "https" => Ok(()),
            scheme => Err(DomainError {
                format_name: "HTTP URL",
                value: value.clone(),
                reason: DomainErrorKind::InvalidComponent {
                    component: "scheme",
                    reason: format!("expected http or https, got {}", scheme),
                },
                example: "https://example.com",
            }),
        }
    }

    fn description() -> &'static str {
        "HTTP or HTTPS scheme"
    }
}

/// URL scheme must be https (secure).
///
/// This predicate validates only the scheme, not the overall URL validity.
/// For a complete secure URL, use `SecureUrl` which combines `ValidUrl` and `HttpsOnly`.
///
/// # Example
///
/// ```
/// use stilltypes::url::SecureUrl;
///
/// let https = SecureUrl::new("https://example.com".to_string());
/// assert!(https.is_ok());
///
/// let http = SecureUrl::new("http://example.com".to_string());
/// assert!(http.is_err());
/// ```
#[derive(Debug, Clone, Copy, Default)]
pub struct HttpsOnly;

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

    fn check(value: &String) -> Result<(), Self::Error> {
        let parsed = UrlParser::parse(value).map_err(|_| DomainError {
            format_name: "HTTPS URL",
            value: value.clone(),
            reason: DomainErrorKind::InvalidFormat {
                expected: "valid URL",
            },
            example: "https://example.com",
        })?;

        if parsed.scheme() == "https" {
            Ok(())
        } else {
            Err(DomainError {
                format_name: "HTTPS URL",
                value: value.clone(),
                reason: DomainErrorKind::InvalidComponent {
                    component: "scheme",
                    reason: format!("expected https, got {}", parsed.scheme()),
                },
                example: "https://example.com",
            })
        }
    }

    fn description() -> &'static str {
        "HTTPS scheme only"
    }
}

/// Any valid URL (RFC 3986).
///
/// A `String` that has been validated to be a properly formatted URL
/// according to RFC 3986 (via the WHATWG URL Standard).
///
/// # Example
///
/// ```
/// use stilltypes::url::Url;
///
/// let url = Url::new("https://example.com/path?query=value".to_string()).unwrap();
/// assert_eq!(url.get(), "https://example.com/path?query=value");
/// ```
pub type Url = Refined<String, ValidUrl>;

/// HTTP or HTTPS URL.
///
/// Composed using stillwater's `And` combinator to validate both
/// URL structure and scheme.
///
/// # Example
///
/// ```
/// use stilltypes::url::HttpUrl;
///
/// let http = HttpUrl::new("http://example.com".to_string()).unwrap();
/// let https = HttpUrl::new("https://example.com".to_string()).unwrap();
///
/// // FTP is rejected
/// let ftp = HttpUrl::new("ftp://files.example.com".to_string());
/// assert!(ftp.is_err());
/// ```
pub type HttpUrl = Refined<String, And<ValidUrl, HttpScheme>>;

/// HTTPS-only URL (secure).
///
/// Composed using stillwater's `And` combinator to validate both
/// URL structure and secure scheme.
///
/// # Example
///
/// ```
/// use stilltypes::url::SecureUrl;
///
/// let secure = SecureUrl::new("https://api.example.com".to_string()).unwrap();
///
/// // HTTP is rejected
/// let insecure = SecureUrl::new("http://example.com".to_string());
/// assert!(insecure.is_err());
/// ```
pub type SecureUrl = Refined<String, And<ValidUrl, HttpsOnly>>;

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

    // ValidUrl tests
    #[test]
    fn valid_https_url() {
        assert!(Url::new("https://example.com".to_string()).is_ok());
    }

    #[test]
    fn valid_http_url() {
        assert!(Url::new("http://example.com".to_string()).is_ok());
    }

    #[test]
    fn valid_with_path() {
        assert!(Url::new("https://example.com/path/to/resource".to_string()).is_ok());
    }

    #[test]
    fn valid_with_query() {
        assert!(Url::new("https://example.com?foo=bar&baz=qux".to_string()).is_ok());
    }

    #[test]
    fn valid_with_fragment() {
        assert!(Url::new("https://example.com#section".to_string()).is_ok());
    }

    #[test]
    fn valid_with_port() {
        assert!(Url::new("https://example.com:8080".to_string()).is_ok());
    }

    #[test]
    fn valid_ftp_url() {
        // ValidUrl accepts any scheme
        assert!(Url::new("ftp://files.example.com".to_string()).is_ok());
    }

    #[test]
    fn invalid_missing_scheme() {
        assert!(Url::new("example.com".to_string()).is_err());
    }

    #[test]
    fn invalid_malformed() {
        assert!(Url::new("not a url at all".to_string()).is_err());
    }

    #[test]
    fn valid_url_description() {
        assert_eq!(ValidUrl::description(), "RFC 3986 URL");
    }

    // HttpUrl tests
    #[test]
    fn http_url_accepts_http() {
        assert!(HttpUrl::new("http://example.com".to_string()).is_ok());
    }

    #[test]
    fn http_url_accepts_https() {
        assert!(HttpUrl::new("https://example.com".to_string()).is_ok());
    }

    #[test]
    fn http_url_rejects_ftp() {
        let result = HttpUrl::new("ftp://example.com".to_string());
        assert!(result.is_err());
        // HttpUrl uses And combinator which returns AndError
        // FTP passes ValidUrl but fails HttpScheme, so it's AndError::Second
        let err = result.unwrap_err();
        match err {
            stillwater::refined::AndError::Second(domain_err) => {
                assert!(matches!(
                    domain_err.reason,
                    DomainErrorKind::InvalidComponent { .. }
                ));
            }
            _ => panic!("Expected AndError::Second for scheme rejection"),
        }
    }

    #[test]
    fn http_url_rejects_file() {
        assert!(HttpUrl::new("file:///path/to/file".to_string()).is_err());
    }

    #[test]
    fn http_scheme_description() {
        assert_eq!(HttpScheme::description(), "HTTP or HTTPS scheme");
    }

    // SecureUrl tests
    #[test]
    fn secure_url_accepts_https() {
        assert!(SecureUrl::new("https://example.com".to_string()).is_ok());
    }

    #[test]
    fn secure_url_rejects_http() {
        let result = SecureUrl::new("http://example.com".to_string());
        assert!(result.is_err());
        // SecureUrl uses And combinator which returns AndError
        // HTTP passes ValidUrl but fails HttpsOnly, so it's AndError::Second
        let err = result.unwrap_err();
        match err {
            stillwater::refined::AndError::Second(domain_err) => {
                assert!(matches!(
                    domain_err.reason,
                    DomainErrorKind::InvalidComponent { .. }
                ));
            }
            _ => panic!("Expected AndError::Second for scheme rejection"),
        }
    }

    #[test]
    fn https_only_description() {
        assert_eq!(HttpsOnly::description(), "HTTPS scheme only");
    }

    // Test standalone predicates with malformed URLs
    // (normally unreachable when composed with ValidUrl via And)
    #[test]
    fn https_only_standalone_rejects_malformed() {
        let result = HttpsOnly::check(&"not a url".to_string());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.format_name, "HTTPS URL");
        assert!(matches!(err.reason, DomainErrorKind::InvalidFormat { .. }));
    }

    #[test]
    fn http_scheme_standalone_rejects_malformed() {
        let result = HttpScheme::check(&"invalid".to_string());
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert_eq!(err.format_name, "HTTP URL");
    }

    // Composition tests
    #[test]
    fn and_combinator_validates_both_predicates() {
        // Invalid URL should fail ValidUrl
        assert!(HttpUrl::new("not a url".to_string()).is_err());

        // Valid FTP should fail HttpScheme
        assert!(HttpUrl::new("ftp://example.com".to_string()).is_err());

        // Valid HTTPS should pass both
        assert!(HttpUrl::new("https://example.com".to_string()).is_ok());
    }

    // Error message tests
    #[test]
    fn invalid_url_error_includes_format_name() {
        let result = Url::new("invalid".to_string());
        let err = result.unwrap_err();
        assert_eq!(err.format_name, "URL");
    }

    #[test]
    fn invalid_url_error_includes_example() {
        let result = Url::new("invalid".to_string());
        let err = result.unwrap_err();
        assert_eq!(err.example, "https://example.com");
    }

    #[test]
    fn scheme_error_is_invalid_component() {
        let result = SecureUrl::new("http://example.com".to_string());
        let err = result.unwrap_err();
        // SecureUrl uses And combinator, extract the underlying DomainError
        match err {
            stillwater::refined::AndError::Second(domain_err) => match domain_err.reason {
                DomainErrorKind::InvalidComponent { component, reason } => {
                    assert_eq!(component, "scheme");
                    assert!(reason.contains("https"));
                }
                _ => panic!("Expected InvalidComponent error"),
            },
            _ => panic!("Expected AndError::Second"),
        }
    }
}