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
//! IRI validator.

use std::{error, fmt};

use nom::combinator::all_consuming;

use crate::parser::{self, IriRule};

/// IRI validation error.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct Error(());

impl fmt::Display for Error {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "Invalid IRI")
    }
}

impl error::Error for Error {}

/// Converts the given result into a validation result.
fn conv_err<T, E>(res: Result<T, E>) -> Result<(), Error> {
    match res {
        Ok(_) => Ok(()),
        Err(_) => Err(Error(())),
    }
}

/// Validates RFC 3987 IRI.
pub fn iri(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::uri::<(), IriRule>)(s))
}

/// Validates RFC 3987 IRI reference.
pub fn iri_reference(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::uri_reference::<(), IriRule>)(s))
}

/// Validates RFC 3987 absolute IRI.
pub fn absolute_iri(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::absolute_uri::<(), IriRule>)(s))
}

/// Validates RFC 3987 relative reference.
pub fn relative_ref(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::relative_ref::<(), IriRule>)(s))
}

/// Validates RFC 3987 IRI path.
pub fn path(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::path::<(), IriRule>)(s))
}

/// Validates RFC 3987 IRI fragment.
pub fn fragment(s: &str) -> Result<(), Error> {
    conv_err(all_consuming(parser::fragment::<(), IriRule>)(s))
}