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
// this module is transparently re-exported by its parent `term`

use std::hash::{Hash, Hasher};
use std::io::{Result as IoResult, Write};

use self::iri_rfc3987::{is_absolute_iri, is_relative_iri};
use super::*;

/// Internal representation of an IRI.
///
/// May be encountered when pattern-matching on [`Term`](enum.Term.html)s
/// of the [`Iri`](enum.Term.html#variant.Iri) variant.
/// For that purpose, note that `IriData`
///  - can be directly compared to a `&str` with the `==` operator;
///  - can be directly compared to a [`Term`](enum.Term.html) with the `==` operator;
///  - provides some identical methods to what `&str` provides (see below);
///  - can otherwise be converted to a `String` with [`to_string`](#method.to_string);
///
/// See [module documentation](index.html)
/// for more detail.
#[derive(Clone, Copy, Debug, Eq)]
pub struct IriData<T: AsRef<str>> {
    pub(crate) ns: T,
    pub(crate) suffix: Option<T>,
    pub(crate) absolute: bool,
}

impl<T> IriData<T>
where
    T: AsRef<str>,
{
    /// The length of this IRI.
    pub fn len(&self) -> usize {
        self.ns.as_ref().len() + self.suffix_borrow().len()
    }

    pub fn is_empty(&self) -> bool {
        self.ns.as_ref().is_empty() && self.suffix_borrow().is_empty()
    }

    /// Iterate over the bytes representing this IRI.
    pub fn bytes<'a>(&'a self) -> impl Iterator<Item = u8> + 'a {
        self.ns.as_ref().bytes().chain(self.suffix_borrow().bytes())
    }

    /// Iterate over the characters representing this IRI.
    pub fn chars<'a>(&'a self) -> impl Iterator<Item = char> + 'a {
        self.ns.as_ref().chars().chain(self.suffix_borrow().chars())
    }

    /// Construct a copy of this IRI as a `String`.
    pub fn to_string(&self) -> String {
        let ns = self.ns.as_ref();
        let suffix = self.suffix_borrow();
        let mut ret = String::with_capacity(ns.len() + suffix.len());
        ret.push_str(ns);
        ret.push_str(suffix);
        ret
    }

    /// Whether this IRI is absolute or relative.
    pub fn is_absolute(&self) -> bool {
        self.absolute
    }

    /// Write this IRI to `w`.
    pub fn write_to<W>(&self, w: &mut W) -> IoResult<()>
    where
        W: Write,
    {
        w.write_all(self.ns.as_ref().as_bytes())?;
        if let Some(ref suffix) = self.suffix {
            w.write_all(suffix.as_ref().as_bytes())?;
        }
        Ok(())
    }

    pub(crate) fn new(ns: T, suffix: Option<T>) -> Result<IriData<T>> {
        let mut ret = IriData {
            ns,
            suffix,
            absolute: false,
        };
        let val = ret.to_string();
        ret.absolute = is_absolute_iri(&val);
        if ret.absolute || is_relative_iri(&val) {
            Ok(ret)
        } else {
            Err(ErrorKind::InvalidIri("IRI is invalid".to_string()).into())
        }
    }

    pub(crate) unsafe fn new_unchecked(
        ns: T,
        suffix: Option<T>,
        absolute: Option<bool>,
    ) -> IriData<T> {
        match absolute {
            Some(absolute) => IriData {
                ns,
                suffix,
                absolute,
            },
            None => IriData::new(ns, suffix).unwrap(),
        }
    }

    pub(crate) fn from_with<'a, U, F>(other: &'a IriData<U>, mut factory: F) -> IriData<T>
    where
        U: AsRef<str>,
        F: FnMut(&'a str) -> T,
    {
        let ns = factory(other.ns.as_ref());
        let suffix = match other.suffix {
            Some(ref suffix) => Some(factory(suffix.as_ref())),
            None => None,
        };
        IriData {
            ns,
            suffix,
            absolute: other.absolute,
        }
    }

    pub(crate) fn normalized_with<'a, U, F>(
        other: &'a IriData<U>,
        factory: F,
        norm: Normalization,
    ) -> IriData<T>
    where
        U: AsRef<str>,
        F: FnMut(&str) -> T,
    {
        match norm {
            Normalization::NoSuffix => Self::no_suffix_with(other, factory),
            Normalization::LastHashOrSlash => Self::last_hash_or_slash_with(other, factory),
        }
    }

    fn no_suffix_with<'a, U, F>(other: &'a IriData<U>, mut factory: F) -> IriData<T>
    where
        U: AsRef<str>,
        F: FnMut(&str) -> T,
    {
        let ns = match other.suffix {
            Some(_) => factory(&other.to_string()),
            None => factory(other.ns.as_ref()),
        };
        IriData {
            ns,
            suffix: None,
            absolute: other.absolute,
        }
    }

    fn last_hash_or_slash_with<'a, U, F>(other: &'a IriData<U>, mut factory: F) -> IriData<T>
    where
        U: AsRef<str>,
        F: FnMut(&str) -> T,
    {
        let sep = ['#', '/'];
        let ns = other.ns.as_ref();
        let absolute = other.absolute;
        if let Some(ref suffix) = other.suffix {
            let suffix = suffix.as_ref();
            if let Some(spos) = suffix.rfind(&sep[..]) {
                let mut new_ns = String::with_capacity(ns.len() + spos + 1);
                new_ns.push_str(ns);
                new_ns.push_str(&suffix[..=spos]);
                IriData {
                    ns: factory(&new_ns),
                    suffix: Some(factory(&suffix[spos + 1..])),
                    absolute,
                }
            } else if let Some(npos) = ns.rfind(&sep[..]) {
                let mut new_suffix = String::with_capacity(ns.len() - npos - 1 + suffix.len());
                new_suffix.push_str(&ns[npos + 1..]);
                new_suffix.push_str(suffix);
                IriData {
                    ns: factory(&ns[..=npos]),
                    suffix: Some(factory(&new_suffix)),
                    absolute,
                }
            } else {
                IriData {
                    ns: factory(&other.to_string()),
                    suffix: None,
                    absolute,
                }
            }
        } else if let Some(npos) = ns.rfind(&sep[..]) {
            IriData {
                ns: factory(&ns[..=npos]),
                suffix: Some(factory(&ns[npos + 1..])),
                absolute,
            }
        } else {
            IriData {
                ns: factory(ns),
                suffix: None,
                absolute,
            }
        }
    }

    fn suffix_borrow(&self) -> &str {
        match self.suffix {
            Some(ref suffix) => suffix.as_ref(),
            None => "",
        }
    }
}

impl<T, U> PartialEq<IriData<U>> for IriData<T>
where
    T: AsRef<str>,
    U: AsRef<str>,
{
    fn eq(&self, other: &IriData<U>) -> bool {
        let s_ns = self.ns.as_ref();
        let s_sf = self.suffix_borrow();
        let o_ns = other.ns.as_ref();
        let o_sf = other.suffix_borrow();
        (s_ns.len() + s_sf.len()) == (o_ns.len() + o_sf.len()) && {
            let mut eq = true;
            let it1 = s_ns.chars().chain(s_sf.chars());
            let it2 = o_ns.chars().chain(o_sf.chars());
            for (c1, c2) in it1.zip(it2) {
                if c1 != c2 {
                    eq = false;
                    break;
                }
            }
            eq
        }
    }
}

impl<'a, T> PartialEq<&'a str> for IriData<T>
where
    T: AsRef<str>,
{
    fn eq(&self, other: &&'a str) -> bool {
        let s_ns = self.ns.as_ref();
        let s_sf = self.suffix_borrow();
        (s_ns.len() + s_sf.len()) == (other.len()) && {
            let mut eq = true;
            let it1 = s_ns.chars().chain(s_sf.chars());
            let it2 = other.chars();
            for (c1, c2) in it1.zip(it2) {
                if c1 != c2 {
                    eq = false;
                    break;
                }
            }
            eq
        }
    }
}

impl<T, U> PartialEq<Term<U>> for IriData<T>
where
    T: AsRef<str>,
    U: TermData,
{
    #[inline]
    fn eq(&self, other: &Term<U>) -> bool {
        match other {
            Iri(other_iri) => other_iri == self,
            _ => false,
        }
    }
}

impl<T> Hash for IriData<T>
where
    T: AsRef<str>,
{
    fn hash<H: Hasher>(&self, state: &mut H) {
        state.write(self.ns.as_ref().as_bytes());
        state.write(self.suffix_borrow().as_bytes());
        state.write_u8(0xff);
    }
}

/// Noramlization policies are used to ensure that
/// IRIs are represented in a given format.
///
/// They are applied by copying terms with
/// [`Term::noramlized_with`](enum.Term.html#method.noramlized_with).
#[derive(Clone, Copy)]
pub enum Normalization {
    /// IRIs are represented as a single string (`ns`) with an empty `suffix`.
    NoSuffix,
    /// IRIs are represented with a prefix `ns` extending to the last hash (`#`) or slash (`/`),
    /// and a `suffix` containing the remaining characters.
    LastHashOrSlash,
}

impl<'a> ParsedIri<'a> {
    pub fn join_iri<T>(&self, iri_term: &IriData<T>) -> IriData<T>
    where
        T: AsRef<str> + Clone + From<String>,
    {
        let parsed_ns = ParsedIri::new(iri_term.ns.as_ref()).unwrap();
        let abs_ns = T::from(self.join(&parsed_ns).to_string());
        IriData {
            ns: abs_ns,
            suffix: iri_term.suffix.clone(),
            absolute: true,
        }
    }
}

#[cfg(test)]
mod test {
    // The code from this module is tested through its use in other modules
    // (especially the ::term::test module).
}