oxirs-ttl 0.2.4

Turtle-family RDF parser and serializer for OxiRS - ported from Oxigraph
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
//! RFC 3987 IRI validation
//!
//! This module provides validation for Internationalized Resource Identifiers (IRIs)
//! according to RFC 3987. IRIs extend URIs to support Unicode characters.
//!
//! # RFC 3987 Compliance
//!
//! An IRI consists of:
//! - scheme (required)
//! - authority (optional) - userinfo@host:port
//! - path
//! - query (optional)
//! - fragment (optional)
//!
//! # Example
//!
//! ```rust
//! use oxirs_ttl::toolkit::iri_validator::{validate_iri, IriValidationError};
//!
//! assert!(validate_iri("http://example.org/path").is_ok());
//! assert!(validate_iri("urn:isbn:0451450523").is_ok());
//! assert!(validate_iri("http://example.org/path#fragment").is_ok());
//! assert!(validate_iri("http://example.org/日本語").is_ok());
//!
//! // Invalid IRIs
//! assert!(validate_iri("not an iri").is_err());
//! assert!(validate_iri("http:// invalid").is_err());
//! ```

use std::fmt;

/// Error type for IRI validation failures
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum IriValidationError {
    /// IRI is empty
    Empty,
    /// Missing scheme (e.g., "http:", "urn:")
    MissingScheme,
    /// Invalid scheme format
    InvalidScheme(String),
    /// Invalid authority component
    InvalidAuthority(String),
    /// Invalid host
    InvalidHost(String),
    /// Invalid port
    InvalidPort(String),
    /// Invalid path
    InvalidPath(String),
    /// Invalid query
    InvalidQuery(String),
    /// Invalid fragment
    InvalidFragment(String),
    /// Invalid character in IRI
    InvalidCharacter(char, usize),
    /// Invalid percent encoding
    InvalidPercentEncoding(String),
}

impl fmt::Display for IriValidationError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            Self::Empty => write!(f, "IRI cannot be empty"),
            Self::MissingScheme => write!(f, "IRI must have a scheme"),
            Self::InvalidScheme(s) => write!(f, "Invalid scheme: {}", s),
            Self::InvalidAuthority(s) => write!(f, "Invalid authority: {}", s),
            Self::InvalidHost(s) => write!(f, "Invalid host: {}", s),
            Self::InvalidPort(s) => write!(f, "Invalid port: {}", s),
            Self::InvalidPath(s) => write!(f, "Invalid path: {}", s),
            Self::InvalidQuery(s) => write!(f, "Invalid query: {}", s),
            Self::InvalidFragment(s) => write!(f, "Invalid fragment: {}", s),
            Self::InvalidCharacter(c, pos) => {
                write!(f, "Invalid character '{}' at position {}", c, pos)
            }
            Self::InvalidPercentEncoding(s) => write!(f, "Invalid percent encoding: {}", s),
        }
    }
}

impl std::error::Error for IriValidationError {}

/// Result type for IRI validation
pub type IriValidationResult<T> = Result<T, IriValidationError>;

/// Validates an IRI according to RFC 3987
///
/// Returns Ok(()) if the IRI is valid, or an error describing the validation failure.
pub fn validate_iri(iri: &str) -> IriValidationResult<()> {
    if iri.is_empty() {
        return Err(IriValidationError::Empty);
    }

    // Parse the IRI components
    let (scheme, rest) = parse_scheme(iri)?;
    validate_scheme(&scheme)?;

    // Check for authority (starts with //)
    let (authority, path_query_fragment) = if let Some(after_slashes) = rest.strip_prefix("//") {
        let auth_end = after_slashes
            .find(['/', '?', '#'])
            .unwrap_or(after_slashes.len());
        let authority = &after_slashes[..auth_end];
        validate_authority(authority)?;
        (Some(authority), &after_slashes[auth_end..])
    } else {
        (None, rest)
    };

    // Parse path, query, and fragment
    let (path, query_fragment) = if let Some(q_pos) = path_query_fragment.find('?') {
        (&path_query_fragment[..q_pos], &path_query_fragment[q_pos..])
    } else if let Some(f_pos) = path_query_fragment.find('#') {
        (&path_query_fragment[..f_pos], &path_query_fragment[f_pos..])
    } else {
        (path_query_fragment, "")
    };

    // Validate path
    validate_path(path, authority.is_some())?;

    // Parse and validate query and fragment
    if !query_fragment.is_empty() {
        if let Some(after_question) = query_fragment.strip_prefix('?') {
            let (query, fragment) = if let Some(f_pos) = after_question.find('#') {
                (&after_question[..f_pos], &after_question[f_pos..])
            } else {
                (after_question, "")
            };
            validate_query(query)?;
            if let Some(frag) = fragment.strip_prefix('#') {
                validate_fragment(frag)?;
            }
        } else if let Some(frag) = query_fragment.strip_prefix('#') {
            validate_fragment(frag)?;
        }
    }

    Ok(())
}

/// Validates an IRI reference (can be relative)
pub fn validate_iri_reference(iri_ref: &str) -> IriValidationResult<()> {
    if iri_ref.is_empty() {
        return Ok(()); // Empty reference is valid
    }

    // Check if it has a scheme
    if let Some(colon_pos) = iri_ref.find(':') {
        // Check if the part before : is a valid scheme
        let potential_scheme = &iri_ref[..colon_pos];
        if is_valid_scheme_chars(potential_scheme) {
            return validate_iri(iri_ref);
        }
    }

    // It's a relative reference - validate path, query, fragment
    let (path, query_fragment) = if let Some(after_slashes) = iri_ref.strip_prefix("//") {
        // Network-path reference
        let auth_end = after_slashes
            .find(['/', '?', '#'])
            .unwrap_or(after_slashes.len());
        let authority = &after_slashes[..auth_end];
        validate_authority(authority)?;
        (&after_slashes[auth_end..], "")
    } else if let Some(q_pos) = iri_ref.find('?') {
        (&iri_ref[..q_pos], &iri_ref[q_pos..])
    } else if let Some(f_pos) = iri_ref.find('#') {
        (&iri_ref[..f_pos], &iri_ref[f_pos..])
    } else {
        (iri_ref, "")
    };

    // Validate relative path
    if !path.is_empty() {
        validate_path(path, false)?;
    }

    // Validate query and fragment
    if !query_fragment.is_empty() {
        if let Some(after_question) = query_fragment.strip_prefix('?') {
            let (query, fragment) = if let Some(f_pos) = after_question.find('#') {
                (&after_question[..f_pos], &after_question[f_pos..])
            } else {
                (after_question, "")
            };
            validate_query(query)?;
            if let Some(frag) = fragment.strip_prefix('#') {
                validate_fragment(frag)?;
            }
        } else if let Some(frag) = query_fragment.strip_prefix('#') {
            validate_fragment(frag)?;
        }
    }

    Ok(())
}

/// Parse the scheme from an IRI
fn parse_scheme(iri: &str) -> IriValidationResult<(String, &str)> {
    let colon_pos = iri.find(':').ok_or(IriValidationError::MissingScheme)?;

    let scheme = &iri[..colon_pos];
    let rest = &iri[colon_pos + 1..];

    Ok((scheme.to_string(), rest))
}

/// Check if a string contains only valid scheme characters
fn is_valid_scheme_chars(s: &str) -> bool {
    if s.is_empty() {
        return false;
    }
    let mut chars = s.chars();

    // First character must be ASCII letter
    let first = chars.next().expect("iterator should have next element");
    if !first.is_ascii_alphabetic() {
        return false;
    }

    // Rest can be ASCII letter, digit, +, -, .
    chars.all(|c| c.is_ascii_alphanumeric() || c == '+' || c == '-' || c == '.')
}

/// Validate the scheme component
fn validate_scheme(scheme: &str) -> IriValidationResult<()> {
    if !is_valid_scheme_chars(scheme) {
        return Err(IriValidationError::InvalidScheme(scheme.to_string()));
    }
    Ok(())
}

/// Validate the authority component
fn validate_authority(authority: &str) -> IriValidationResult<()> {
    if authority.is_empty() {
        return Ok(());
    }

    // Split userinfo@host:port
    let (userinfo, host_port) = if let Some(at_pos) = authority.rfind('@') {
        let userinfo = &authority[..at_pos];
        validate_userinfo(userinfo)?;
        (Some(userinfo), &authority[at_pos + 1..])
    } else {
        (None, authority)
    };

    // Split host:port
    let (host, port) = if let Some(_bracket_start) = host_port.find('[') {
        // IPv6 address
        let bracket_end = host_port
            .find(']')
            .ok_or_else(|| IriValidationError::InvalidHost("Unclosed IPv6 bracket".to_string()))?;
        let host = &host_port[..bracket_end + 1];
        let rest = &host_port[bracket_end + 1..];
        let port = if let Some(port_str) = rest.strip_prefix(':') {
            Some(port_str)
        } else if rest.is_empty() {
            None
        } else {
            return Err(IriValidationError::InvalidHost(host_port.to_string()));
        };
        (host, port)
    } else if let Some(colon_pos) = host_port.rfind(':') {
        let potential_port = &host_port[colon_pos + 1..];
        // Only treat as port if it's all digits
        if potential_port.chars().all(|c| c.is_ascii_digit()) {
            (&host_port[..colon_pos], Some(potential_port))
        } else {
            (host_port, None)
        }
    } else {
        (host_port, None)
    };

    validate_host(host)?;

    if let Some(port) = port {
        validate_port(port)?;
    }

    let _ = userinfo; // Suppress unused warning

    Ok(())
}

/// Validate userinfo component
fn validate_userinfo(userinfo: &str) -> IriValidationResult<()> {
    for (i, c) in userinfo.chars().enumerate() {
        if !is_valid_userinfo_char(c) {
            return Err(IriValidationError::InvalidCharacter(c, i));
        }
    }
    Ok(())
}

/// Validate host component
fn validate_host(host: &str) -> IriValidationResult<()> {
    if host.is_empty() {
        return Ok(());
    }

    // IPv6 address
    if host.starts_with('[') && host.ends_with(']') {
        return validate_ipv6(&host[1..host.len() - 1]);
    }

    // Reg-name or IPv4
    for (i, c) in host.chars().enumerate() {
        if !is_valid_host_char(c) {
            return Err(IriValidationError::InvalidCharacter(c, i));
        }
    }

    Ok(())
}

/// Validate IPv6 address
fn validate_ipv6(addr: &str) -> IriValidationResult<()> {
    // Basic IPv6 validation
    let parts: Vec<&str> = addr.split(':').collect();
    if parts.len() > 8 {
        return Err(IriValidationError::InvalidHost(format!(
            "IPv6 address has too many parts: {}",
            addr
        )));
    }

    for part in parts {
        if !part.is_empty() && !part.chars().all(|c| c.is_ascii_hexdigit()) {
            // Allow for IPv4-mapped addresses
            if !part.contains('.') {
                return Err(IriValidationError::InvalidHost(format!(
                    "Invalid IPv6 part: {}",
                    part
                )));
            }
        }
    }

    Ok(())
}

/// Validate port component
fn validate_port(port: &str) -> IriValidationResult<()> {
    if port.is_empty() {
        return Ok(());
    }

    if !port.chars().all(|c| c.is_ascii_digit()) {
        return Err(IriValidationError::InvalidPort(port.to_string()));
    }

    Ok(())
}

/// Validate path component
fn validate_path(path: &str, has_authority: bool) -> IriValidationResult<()> {
    if path.is_empty() {
        return Ok(());
    }

    // Path validation depends on whether authority is present
    if has_authority && !path.is_empty() && !path.starts_with('/') {
        return Err(IriValidationError::InvalidPath(
            "Path must start with / when authority is present".to_string(),
        ));
    }

    // Check each character
    let mut chars = path.chars().enumerate().peekable();
    while let Some((i, c)) = chars.next() {
        if c == '%' {
            // Validate percent encoding
            let hex1 = chars.next();
            let hex2 = chars.next();
            match (hex1, hex2) {
                (Some((_, h1)), Some((_, h2)))
                    if h1.is_ascii_hexdigit() && h2.is_ascii_hexdigit() => {}
                _ => {
                    return Err(IriValidationError::InvalidPercentEncoding(format!(
                        "Invalid percent encoding at position {}",
                        i
                    )));
                }
            }
        } else if !is_valid_path_char(c) {
            return Err(IriValidationError::InvalidCharacter(c, i));
        }
    }

    Ok(())
}

/// Validate query component
fn validate_query(query: &str) -> IriValidationResult<()> {
    for (i, c) in query.chars().enumerate() {
        if c == '%' {
            // Already validated in the main loop, skip
            continue;
        }
        if !is_valid_query_char(c) {
            return Err(IriValidationError::InvalidCharacter(c, i));
        }
    }
    Ok(())
}

/// Validate fragment component
fn validate_fragment(fragment: &str) -> IriValidationResult<()> {
    for (i, c) in fragment.chars().enumerate() {
        if c == '%' {
            // Already validated in the main loop, skip
            continue;
        }
        if !is_valid_fragment_char(c) {
            return Err(IriValidationError::InvalidCharacter(c, i));
        }
    }
    Ok(())
}

/// Check if character is valid for userinfo
fn is_valid_userinfo_char(c: char) -> bool {
    is_unreserved(c) || is_sub_delim(c) || c == ':' || c == '%'
}

/// Check if character is valid for host (reg-name)
fn is_valid_host_char(c: char) -> bool {
    is_unreserved(c) || is_sub_delim(c) || c == '%'
}

/// Check if character is valid for path
fn is_valid_path_char(c: char) -> bool {
    is_pchar(c) || c == '/'
}

/// Check if character is valid for query
fn is_valid_query_char(c: char) -> bool {
    is_pchar(c) || is_iprivate(c) || c == '/' || c == '?'
}

/// Check if character is valid for fragment
fn is_valid_fragment_char(c: char) -> bool {
    is_pchar(c) || c == '/' || c == '?'
}

/// RFC 3987 pchar
fn is_pchar(c: char) -> bool {
    is_unreserved(c) || is_sub_delim(c) || c == ':' || c == '@' || c == '%'
}

/// RFC 3986 unreserved characters plus RFC 3987 ucschar
fn is_unreserved(c: char) -> bool {
    c.is_ascii_alphanumeric() || c == '-' || c == '.' || c == '_' || c == '~' || is_ucschar(c)
}

/// RFC 3987 ucschar (Unicode characters allowed in IRIs)
fn is_ucschar(c: char) -> bool {
    let cp = c as u32;
    (0xA0..=0xD7FF).contains(&cp)
        || (0xF900..=0xFDCF).contains(&cp)
        || (0xFDF0..=0xFFEF).contains(&cp)
        || (0x10000..=0x1FFFD).contains(&cp)
        || (0x20000..=0x2FFFD).contains(&cp)
        || (0x30000..=0x3FFFD).contains(&cp)
        || (0x40000..=0x4FFFD).contains(&cp)
        || (0x50000..=0x5FFFD).contains(&cp)
        || (0x60000..=0x6FFFD).contains(&cp)
        || (0x70000..=0x7FFFD).contains(&cp)
        || (0x80000..=0x8FFFD).contains(&cp)
        || (0x90000..=0x9FFFD).contains(&cp)
        || (0xA0000..=0xAFFFD).contains(&cp)
        || (0xB0000..=0xBFFFD).contains(&cp)
        || (0xC0000..=0xCFFFD).contains(&cp)
        || (0xD0000..=0xDFFFD).contains(&cp)
        || (0xE1000..=0xEFFFD).contains(&cp)
}

/// RFC 3987 iprivate (private use characters allowed in query)
fn is_iprivate(c: char) -> bool {
    let cp = c as u32;
    (0xE000..=0xF8FF).contains(&cp)
        || (0xF0000..=0xFFFFD).contains(&cp)
        || (0x100000..=0x10FFFD).contains(&cp)
}

/// RFC 3986 sub-delims
fn is_sub_delim(c: char) -> bool {
    matches!(
        c,
        '!' | '$' | '&' | '\'' | '(' | ')' | '*' | '+' | ',' | ';' | '='
    )
}

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

    #[test]
    fn test_valid_iris() {
        assert!(validate_iri("http://example.org").is_ok());
        assert!(validate_iri("http://example.org/").is_ok());
        assert!(validate_iri("http://example.org/path").is_ok());
        assert!(validate_iri("http://example.org/path?query").is_ok());
        assert!(validate_iri("http://example.org/path#fragment").is_ok());
        assert!(validate_iri("http://example.org/path?query#fragment").is_ok());
        assert!(validate_iri("https://example.org:8080/path").is_ok());
        assert!(validate_iri("ftp://user:pass@example.org/file").is_ok());
        assert!(validate_iri("urn:isbn:0451450523").is_ok());
        assert!(validate_iri("mailto:user@example.org").is_ok());
        assert!(validate_iri("file:///path/to/file").is_ok());
    }

    #[test]
    fn test_unicode_iris() {
        assert!(validate_iri("http://example.org/日本語").is_ok());
        assert!(validate_iri("http://example.org/中文").is_ok());
        assert!(validate_iri("http://example.org/العربية").is_ok());
        assert!(validate_iri("http://example.org/한국어").is_ok());
        assert!(validate_iri("http://example.org/path?q=日本語").is_ok());
    }

    #[test]
    fn test_ipv6_iris() {
        assert!(validate_iri("http://[::1]/").is_ok());
        assert!(validate_iri("http://[2001:db8::1]:8080/").is_ok());
        assert!(validate_iri("http://[::ffff:192.168.1.1]/").is_ok());
    }

    #[test]
    fn test_invalid_iris() {
        assert!(validate_iri("").is_err());
        assert!(validate_iri("not-an-iri").is_err());
        assert!(validate_iri("://missing-scheme").is_err());
        assert!(validate_iri("1http://invalid-scheme").is_err());
        assert!(validate_iri("http:// invalid").is_err());
        assert!(validate_iri("http://example.org/path with spaces").is_err());
    }

    #[test]
    fn test_iri_references() {
        assert!(validate_iri_reference("").is_ok());
        assert!(validate_iri_reference("/relative/path").is_ok());
        assert!(validate_iri_reference("relative/path").is_ok());
        assert!(validate_iri_reference("?query").is_ok());
        assert!(validate_iri_reference("#fragment").is_ok());
        assert!(validate_iri_reference("//authority/path").is_ok());
    }

    #[test]
    fn test_percent_encoding() {
        assert!(validate_iri("http://example.org/path%20with%20spaces").is_ok());
        assert!(validate_iri("http://example.org/path%2F").is_ok());
        assert!(validate_iri("http://example.org/path%").is_err());
        assert!(validate_iri("http://example.org/path%G0").is_err());
    }

    #[test]
    fn test_complex_iris() {
        assert!(validate_iri(
            "http://user:password@example.org:8080/path/to/resource?key=value&foo=bar#section"
        )
        .is_ok());
        assert!(validate_iri("http://example.org/~user/file.html").is_ok());
        assert!(validate_iri("http://example.org/a/b/c/d/e/f/g/h/i/j/k").is_ok());
    }

    #[test]
    fn test_rdf_common_schemes() {
        // Common RDF schemes
        assert!(validate_iri("http://www.w3.org/2001/XMLSchema#string").is_ok());
        assert!(validate_iri("http://www.w3.org/1999/02/22-rdf-syntax-ns#type").is_ok());
        assert!(validate_iri("http://www.w3.org/2000/01/rdf-schema#label").is_ok());
        assert!(validate_iri("http://purl.org/dc/terms/title").is_ok());
        assert!(validate_iri("http://xmlns.com/foaf/0.1/name").is_ok());
    }

    #[test]
    fn test_error_messages() {
        let err = validate_iri("").unwrap_err();
        assert_eq!(err.to_string(), "IRI cannot be empty");

        let err = validate_iri("no-scheme").unwrap_err();
        assert!(err.to_string().contains("scheme"));
    }
}