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
//! Absolute IRI (without fragment part).

use std::convert::TryFrom;

#[cfg(feature = "serde")]
use serde::Serialize;
use validated_slice::{OwnedSliceSpec, SliceSpec};

use crate::{
    types::{IriCreationError, IriReferenceStr, IriReferenceString, IriStr, IriString},
    validate::iri::{absolute_iri, Error},
};

/// A borrowed slice of an absolute IRI without fragment part.
///
/// This corresponds to `absolute-IRI` rule in [RFC 3987].
/// This is `scheme ":" ihier-part [ "?" iquery ]`.
/// In other words, this is [`IriStr`] without fragment part.
///
/// If you want to accept fragment part, use [`IriStr`].
///
/// [RFC 3987]: https://tools.ietf.org/html/rfc3987
/// [`IriStr`]: struct.IriStr.html
#[derive(Debug, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[repr(transparent)]
#[allow(clippy::derive_hash_xor_eq)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct AbsoluteIriStr(str);

impl AbsoluteIriStr {
    /// Creates a new `&AbsoluteIriStr`.
    ///
    /// # Examples
    ///
    /// ```
    /// # use iri_string::types::AbsoluteIriStr;
    /// assert!(AbsoluteIriStr::new("https://example.com/foo?bar=baz").is_ok());
    /// assert!(AbsoluteIriStr::new("foo:bar").is_ok());
    ///
    /// // Relative IRI is not allowed.
    /// assert!(AbsoluteIriStr::new("foo/bar").is_err());
    /// assert!(AbsoluteIriStr::new("/foo/bar").is_err());
    /// assert!(AbsoluteIriStr::new("//foo/bar").is_err());
    /// // Fragment part is not allowed.
    /// assert!(AbsoluteIriStr::new("https://example.com/foo?bar=baz#qux").is_err());
    /// // > When authority is not present, the path cannot begin with two slash characters ("//").
    /// // >
    /// // > --- [RFC 3986 section 3](https://tools.ietf.org/html/rfc3986#section-3)
    /// assert!(AbsoluteIriStr::new("foo:////").is_err());
    /// ```
    pub fn new(s: &str) -> Result<&Self, Error> {
        TryFrom::try_from(s)
    }

    /// Creates a new `&AbsoluteIriStr` maybe without validation.
    ///
    /// This does validation on debug build.
    pub(crate) unsafe fn new_unchecked(s: &str) -> &Self {
        debug_assert_eq!(StrSpec::validate(s), Ok(()));
        StrSpec::from_inner_unchecked(s)
    }

    /// Returns `&str`.
    pub fn as_str(&self) -> &str {
        self.as_ref()
    }
}

/// An owned string of an absolute IRI without fragment part.
///
/// This corresponds to `absolute-IRI` rule in [RFC 3987].
/// This is `scheme ":" ihier-part [ "?" iquery ]`.
/// In other words, this is [`IriString`] without fragment part.
///
/// If you want to accept fragment part, use [`IriString`].
///
///
/// See documentation for [`IriString`].
///
/// [RFC 3987]: https://tools.ietf.org/html/rfc3987
/// [`AbsoluteIriStr`]: struct.AbsoluteIriStr.html
/// [`IriString`]: struct.IriString.html
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
#[cfg_attr(feature = "serde", derive(Serialize))]
#[cfg_attr(feature = "serde", serde(transparent))]
pub struct AbsoluteIriString(String);

impl AbsoluteIriString {
    /// Creates a new `AbsoluteIriString` maybe without validation.
    ///
    /// This does validation on debug build.
    pub(crate) unsafe fn new_unchecked(s: String) -> Self {
        debug_assert_eq!(StrSpec::validate(&s), Ok(()));
        StringSpec::from_inner_unchecked(s)
    }

    /// Shrinks the capacity of the inner buffer to match its length.
    #[inline]
    pub fn shrink_to_fit(&mut self) {
        self.0.shrink_to_fit()
    }
}

impl_basics! {
    Slice {
        spec: StrSpec,
        custom: AbsoluteIriStr,
        validator: absolute_iri,
        error: Error,
    },
    Owned {
        spec: StringSpec,
        custom: AbsoluteIriString,
        error: IriCreationError<String>,
    },
}

impl std::ops::Deref for AbsoluteIriStr {
    type Target = IriStr;

    fn deref(&self) -> &Self::Target {
        self.as_ref()
    }
}

impl_conv_and_cmp! {
    source: {
        owned: AbsoluteIriString,
        slice: AbsoluteIriStr,
        creation_error: IriCreationError,
        validation_error: Error,
    },
    target: [
        {
            owned: IriString,
            slice: IriStr,
        },
        {
            owned: IriReferenceString,
            slice: IriReferenceStr,
        },
    ],
}

impl_serde! {
    expecting: "an absolute IRI",
    slice: AbsoluteIriStr,
    owned: AbsoluteIriString,
}