iri_string/types/
uri.rs

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
//! URI-specific implementations.

use crate::spec::UriSpec;
use crate::types::{
    IriAbsoluteStr, IriFragmentStr, IriQueryStr, IriReferenceStr, IriRelativeStr, IriStr,
    RiAbsoluteStr, RiFragmentStr, RiQueryStr, RiReferenceStr, RiRelativeStr, RiStr,
};
#[cfg(feature = "alloc")]
use crate::types::{
    IriAbsoluteString, IriFragmentString, IriQueryString, IriReferenceString, IriRelativeString,
    IriString, RiAbsoluteString, RiFragmentString, RiQueryString, RiReferenceString,
    RiRelativeString, RiString,
};

/// A type alias for [`RiAbsoluteStr`]`<`[`UriSpec`]`>`.
pub type UriAbsoluteStr = RiAbsoluteStr<UriSpec>;

/// A type alias for [`RiAbsoluteString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriAbsoluteString = RiAbsoluteString<UriSpec>;

/// A type alias for [`RiFragmentStr`]`<`[`UriSpec`]`>`.
pub type UriFragmentStr = RiFragmentStr<UriSpec>;

/// A type alias for [`RiFragmentString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriFragmentString = RiFragmentString<UriSpec>;

/// A type alias for [`RiStr`]`<`[`UriSpec`]`>`.
pub type UriStr = RiStr<UriSpec>;

/// A type alias for [`RiString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriString = RiString<UriSpec>;

/// A type alias for [`RiReferenceStr`]`<`[`UriSpec`]`>`.
pub type UriReferenceStr = RiReferenceStr<UriSpec>;

/// A type alias for [`RiReferenceString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriReferenceString = RiReferenceString<UriSpec>;

/// A type alias for [`RiRelativeStr`]`<`[`UriSpec`]`>`.
pub type UriRelativeStr = RiRelativeStr<UriSpec>;

/// A type alias for [`RiRelativeString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriRelativeString = RiRelativeString<UriSpec>;

/// A type alias for [`RiQueryStr`]`<`[`UriSpec`]`>`.
pub type UriQueryStr = RiQueryStr<UriSpec>;

/// A type alias for [`RiQueryString`]`<`[`UriSpec`]`>`.
#[cfg(feature = "alloc")]
pub type UriQueryString = RiQueryString<UriSpec>;

/// Implements the trivial conversions between a URI and an IRI.
macro_rules! impl_conversions_between_iri {
    (
        $borrowed_uri:ident,
        $owned_uri:ident,
        $borrowed_iri:ident,
        $owned_iri:ident,
    ) => {
        impl AsRef<$borrowed_iri> for $borrowed_uri {
            fn as_ref(&self) -> &$borrowed_iri {
                // SAFETY: A valid URI is also a valid IRI.
                unsafe { <$borrowed_iri>::new_maybe_unchecked(self.as_str()) }
            }
        }

        #[cfg(feature = "alloc")]
        impl From<$owned_uri> for $owned_iri {
            #[inline]
            fn from(uri: $owned_uri) -> Self {
                // SAFETY: A valid URI is also a valid IRI.
                unsafe { Self::new_maybe_unchecked(uri.into()) }
            }
        }

        #[cfg(feature = "alloc")]
        impl AsRef<$borrowed_iri> for $owned_uri {
            fn as_ref(&self) -> &$borrowed_iri {
                AsRef::<$borrowed_uri>::as_ref(self).as_ref()
            }
        }
    };
}

impl_conversions_between_iri!(
    UriAbsoluteStr,
    UriAbsoluteString,
    IriAbsoluteStr,
    IriAbsoluteString,
);
impl_conversions_between_iri!(
    UriReferenceStr,
    UriReferenceString,
    IriReferenceStr,
    IriReferenceString,
);
impl_conversions_between_iri!(
    UriRelativeStr,
    UriRelativeString,
    IriRelativeStr,
    IriRelativeString,
);
impl_conversions_between_iri!(UriStr, UriString, IriStr, IriString,);
impl_conversions_between_iri!(UriQueryStr, UriQueryString, IriQueryStr, IriQueryString,);
impl_conversions_between_iri!(
    UriFragmentStr,
    UriFragmentString,
    IriFragmentStr,
    IriFragmentString,
);