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
//! Relative IRI types.
//!
//! RFC 3987 refers [RFC 3986](https://tools.ietf.org/html/rfc3986#section-4.2)
//! for definition of relative IRI reference.

use std::error;
use std::fmt;

use opaque_typedef::{OpaqueTypedef, OpaqueTypedefUnsized};
#[cfg(feature = "serde")]
use serde;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer};
use url::ParseError as UrlParseError;
use url::Url;

use has_colon_before_slash;


/// Relative URL parse error.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum RelativeIriParseError {
    /// Got a single colon character.
    SingleColon,
    /// Has colon at first component.
    FirstComponentHasColon,
    /// Other URL parsing error.
    Url(UrlParseError),
}

impl error::Error for RelativeIriParseError {
    fn description(&self) -> &str {
        match *self {
            RelativeIriParseError::SingleColon => "Single colon cannot be a relative path",
            RelativeIriParseError::FirstComponentHasColon => {
                "First component should not have a colon"
            },
            RelativeIriParseError::Url(ref e) => e.description(),
        }
    }

    fn cause(&self) -> Option<&error::Error> {
        match *self {
            RelativeIriParseError::Url(ref e) => Some(e),
            _ => None,
        }
    }
}

impl fmt::Display for RelativeIriParseError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        use std::error::Error;

        match *self {
            RelativeIriParseError::SingleColon | RelativeIriParseError::FirstComponentHasColon => {
                f.write_str(self.description())
            },
            RelativeIriParseError::Url(ref e) => e.fmt(f),
        }
    }
}

impl From<UrlParseError> for RelativeIriParseError {
    fn from(e: UrlParseError) -> Self {
        RelativeIriParseError::Url(e)
    }
}


/// Validates the given string as relative IRI.
///
/// This is intended to be used by `opaque_typedef` and internal functions.
fn validate_relative_iri_str<S: AsRef<str>>(s: S) -> Result<S, RelativeIriParseError> {
    // See <https://tools.ietf.org/html/rfc3986#section-4.2>,
    // <https://tools.ietf.org/html/rfc3986#section-3.2> and
    // <https://tools.ietf.org/html/rfc3986#section-3.3>.
    //
    // relative-ref  = relative-part [ "?" query ] [ "#" fragment ]
    // relative-part = "//" authority path-abempty
    //               / path-absolute    ; begins with "/" but not "//"
    //               / path-noscheme    ; begins with a non-colon segment
    //               / path-empty       ; zero characters
    // authority = [ userinfo "@" ] host [ ":" port ]
    // path-abempty = *( "/" segment )
    // path-absolute = "/" [ segment-nz *( "/" segment ) ]
    //
    // `"//" authority path-abempty` can be validated by prepening `"http://"`.
    // `path-absolute` and `path-noscheme` can be validated by pretending
    // `"unknown-scheme:"` and simple string manipulation.
    //
    // scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )
    //
    // Unknown scheme can any unknown (by `url` crate) scheme.
    // However, `url` parser allocates memory, so it is better to use shorter
    // string.

    // Unknown (by `url` crate) scheme.
    const UNKNOWN_DUMMY_SCHEME: &str = "x";

    if s.as_ref().len() < 2 {
        // If "", it matches `path-empty`.
        // If "/", it matches `path-absolute`.
        // If ":", it is an error.
        // Otherwise, it matches `path-noscheme`.
        //
        // NOTE: If "%", "[" or "]", maybe it should be an error.
        // However, `url` crate currently (1.7.0) treat them as valid path
        // component, so consider them as valid here.
        if s.as_ref() == ":" {
            return Err(RelativeIriParseError::SingleColon);
        } else {
            return Ok(s);
        }
    }
    if s.as_ref().starts_with("//") {
        // It can be `"//" authority path-abempty`.
        let dummy_absolute = format!("http:{}", s.as_ref());
        let _ = Url::parse(&dummy_absolute)?;
        return Ok(s);
    } else if s.as_ref().starts_with("/") {
        // It can be `path-absolute`.
        let dummy_absolute = format!("{}:{}", UNKNOWN_DUMMY_SCHEME, s.as_ref());
        let _ = Url::parse(&dummy_absolute)?;
        return Ok(s);
    } else {
        // It can be `path-noscheme`.
        if has_colon_before_slash(s.as_ref()) {
            return Err(RelativeIriParseError::FirstComponentHasColon);
        } else {
            return Ok(s);
        }
    }
}


/// Relative IRI string slice.
// derive_hash_xor_eq (clippy lint):
//  This does not matter because explicit `PartialEq` impls are only doing
//  type conversion (`&RelativeIriStr` to `&str`) and equalness is preserved.
#[allow(unknown_lints, derive_hash_xor_eq)]
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash, OpaqueTypedefUnsized)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[repr(C)]
#[opaque_typedef(
    derive(
        AsRef(Deref, Self_),
        Deref,
        Display,
        Into(Arc, Box, Rc, Inner),
        PartialEq(Inner, InnerRev, InnerCow, SelfCow, SelfCowRev),
        PartialOrd(Inner, InnerRev, InnerCow, SelfCow, SelfCowRev)
    )
)]
#[opaque_typedef(
    validation(
        validator = "validate_relative_iri_str",
        error_type = "RelativeIriParseError",
        error_msg = "Failed to create `RelativeIriString`"
    )
)]
pub struct RelativeIriStr(str);

impl RelativeIriStr {
    /// Creates a new `RelativeIriStr`.
    pub fn new(s: &str) -> Result<&RelativeIriStr, RelativeIriParseError> {
        <Self as OpaqueTypedefUnsized>::try_from_inner(s)
    }

    /// Creates a new `RelativeIriStr` from the given string without validation.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it does not check that the string passed
    /// to it is successfully parsable as relative IRI.
    /// If this constraint is violated, undefined behavior results, as the rest
    /// of Rust assumes that `&RelativeIriStr` is surely a relative IRI.
    ///
    /// So, the argument should fulfill:
    ///
    /// * it is relative IRI defined at [RFC
    ///   3339](https://tools.ietf.org/html/rfc3986#section-4.2).
    ///     + Precisely, `RelativeIriStr` is a bit torelant and it can accept
    ///       wrong strings, but that behavior may change in future.
    pub unsafe fn from_str_unchecked(s: &str) -> &Self {
        // It is caller's responsibility to ensure that this is safe.
        <Self as OpaqueTypedefUnsized>::from_inner_unchecked(s)
    }

    /// Creates `RelativeIriStr` from the string slice.
    ///
    /// This is intended to be used by `opaque_typedef`, and it is because this
    /// function is not `unsafe`.
    fn from_str_unchecked_implicitly_unsafe(s: &str) -> &Self {
        let iri = unsafe {
            // It is caller's (`opaque_typedef`'s) responsibility to ensure that
            // this is safe.
            <Self as OpaqueTypedefUnsized>::from_inner_unchecked(s)
        };
        // Validation requires parsing and it will cost, so run this assert only
        // for debug build.
        debug_assert!(
            validate_relative_iri_str(s).is_ok(),
            "Valid IRI string should be passed to \
             `RelativeIriStr::from_str_unchecked_implicitly_unsafe()`, but got {:?}",
            s
        );
        iri
    }
}

impl ToOwned for RelativeIriStr {
    type Owned = RelativeIriString;

    fn to_owned(&self) -> Self::Owned {
        unsafe {
            // This is safe because `&self.0` is validated at `self` creation.
            <RelativeIriString as OpaqueTypedef>::from_inner_unchecked(self.0.to_owned())
        }
    }
}

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for &'de RelativeIriStr {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = <&str>::deserialize(deserializer)?;
        RelativeIriStr::new(s).map_err(serde::de::Error::custom)
    }
}


/// Owned relative IRI string.
// derive_hash_xor_eq (clippy lint):
//  This does not matter because explicit `PartialEq` impls are only doing
//  type conversion (`&RelativeIriString` to `&str`) and equalness is preserved.
#[allow(unknown_lints, derive_hash_xor_eq)]
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, OpaqueTypedef)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[opaque_typedef(
    derive(
        AsRef(Deref, Inner),
        Deref,
        Display,
        IntoInner,
        PartialEq(Inner, InnerRev),
        PartialOrd(Inner, InnerRev)
    )
)]
#[opaque_typedef(
    deref(
        target = "RelativeIriStr", deref = "RelativeIriStr::from_str_unchecked_implicitly_unsafe"
    )
)]
#[opaque_typedef(
    validation(
        validator = "validate_relative_iri_str",
        error_type = "RelativeIriParseError",
        error_msg = "Failed to create `RelativeIriString`"
    )
)]
pub struct RelativeIriString(String);

impl RelativeIriString {
    /// Creates a new `RelativeIriString`.
    pub fn new(s: String) -> Result<RelativeIriString, RelativeIriParseError> {
        <Self as OpaqueTypedef>::try_from_inner(s)
    }

    /// Creates a new `RelativeIriString` from the given string without
    /// validation.
    ///
    /// # Safety
    ///
    /// This function is unsafe because it does not check that the string passed
    /// to it is successfully parsable as relative IRI.
    /// If this constraint is violated, undefined behavior results, as the rest
    /// of Rust assumes that `RelativeIriString` is surely a relative IRI.
    ///
    /// So, the argument should fulfill:
    ///
    /// * it is relative IRI defined at [RFC
    ///   3339](https://tools.ietf.org/html/rfc3986#section-4.2).
    ///     + Precisely, `RelativeIriStr` is a bit torelant and it can accept
    ///       wrong strings, but that behavior may change in future.
    pub unsafe fn new_unchecked(s: String) -> RelativeIriString {
        <Self as OpaqueTypedef>::from_inner_unchecked(s)
    }

    /// Returns [`&RelativeIriStr`][`RelativeIriStr`] slice for the inner IRI
    /// string.
    pub fn as_iri_str(&self) -> &RelativeIriStr {
        self.as_ref()
    }

    /// Returns a reference to the inner string as `&str`.
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }
}

impl ::std::borrow::Borrow<RelativeIriStr> for RelativeIriString {
    fn borrow(&self) -> &RelativeIriStr {
        self.as_ref()
    }
}

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

#[cfg(feature = "serde")]
impl<'de> Deserialize<'de> for RelativeIriString {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let s = String::deserialize(deserializer)?;
        RelativeIriString::new(s).map_err(serde::de::Error::custom)
    }
}

macro_rules! impl_cmp {
    ($Lhs:ty, $Rhs:ty) => {
        impl PartialEq<$Rhs> for $Lhs {
            fn eq(&self, rhs: &$Rhs) -> bool {
                AsRef::<str>::as_ref(self).eq(AsRef::<str>::as_ref(rhs))
            }
        }
        impl PartialEq<$Lhs> for $Rhs {
            fn eq(&self, rhs: &$Lhs) -> bool {
                AsRef::<str>::as_ref(self).eq(AsRef::<str>::as_ref(rhs))
            }
        }
        impl PartialOrd<$Rhs> for $Lhs {
            fn partial_cmp(&self, rhs: &$Rhs) -> Option<::std::cmp::Ordering> {
                AsRef::<str>::as_ref(self).partial_cmp(AsRef::<str>::as_ref(rhs))
            }
        }
        impl PartialOrd<$Lhs> for $Rhs {
            fn partial_cmp(&self, rhs: &$Lhs) -> Option<::std::cmp::Ordering> {
                AsRef::<str>::as_ref(self).partial_cmp(AsRef::<str>::as_ref(rhs))
            }
        }
    };
    ($Lhs:ty, $Rhs:ty, $($lts:tt)*) => {
        impl<$($lts)*> PartialEq<$Rhs> for $Lhs {
            fn eq(&self, rhs: &$Rhs) -> bool {
                AsRef::<str>::as_ref(self).eq(AsRef::<str>::as_ref(rhs))
            }
        }
        impl<$($lts)*> PartialEq<$Lhs> for $Rhs {
            fn eq(&self, rhs: &$Lhs) -> bool {
                AsRef::<str>::as_ref(self).eq(AsRef::<str>::as_ref(rhs))
            }
        }
        impl<$($lts)*> PartialOrd<$Rhs> for $Lhs {
            fn partial_cmp(&self, rhs: &$Rhs) -> Option<::std::cmp::Ordering> {
                AsRef::<str>::as_ref(self).partial_cmp(AsRef::<str>::as_ref(rhs))
            }
        }
        impl<$($lts)*> PartialOrd<$Lhs> for $Rhs {
            fn partial_cmp(&self, rhs: &$Lhs) -> Option<::std::cmp::Ordering> {
                AsRef::<str>::as_ref(self).partial_cmp(AsRef::<str>::as_ref(rhs))
            }
        }
    };
}

// RelativeIriStr - RelativeIriStr
impl_cmp!(&'a RelativeIriStr, RelativeIriStr, 'a);
// RelativeIriString - RelativeIriStr
impl_cmp!(RelativeIriString, RelativeIriStr);
impl_cmp!(&'a RelativeIriString, RelativeIriStr, 'a);
impl_cmp!(RelativeIriString, &'a RelativeIriStr, 'a);
// RelativeIriString - RelativeIriString
impl_cmp!(&'a RelativeIriString, RelativeIriString, 'a);
// RelativeIriStr - String
impl_cmp!(RelativeIriStr, String);
impl_cmp!(&'a RelativeIriStr, String, 'a);
impl_cmp!(RelativeIriStr, &'a String, 'a);
// RelativeIriString - str
impl_cmp!(RelativeIriString, str);
impl_cmp!(&'a RelativeIriString, str, 'a);
impl_cmp!(RelativeIriString, &'a str, 'a);