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
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
#![warn(
    future_incompatible,
    missing_debug_implementations,
    missing_docs,
    nonstandard_style,
    rust_2018_idioms,
    clippy::checked_conversions,
    clippy::if_not_else,
    clippy::ignored_unit_patterns,
    clippy::map_unwrap_or,
    clippy::missing_errors_doc,
    clippy::must_use_candidate,
    // clippy::redundant_closure_for_method_calls,
    clippy::semicolon_if_nothing_returned,
    clippy::single_match_else,
)]
#![forbid(unsafe_code)]
#![cfg_attr(docsrs, feature(doc_auto_cfg))]
#![no_std]

//! A full-featured URI reference handling library compliant with [RFC 3986].
//!
//! [RFC 3986]: https://datatracker.ietf.org/doc/html/rfc3986
//!
//! **Examples:** [Parsing](UriRef#examples). [Building](Builder#examples).
//! [Reference resolution](UriRef::resolve_against). [Normalization](UriRef::normalize).
//! [Percent-decoding](EStr#examples).
//! [Percent-encoding](crate::encoding::EString#examples).
//!
//! # Terminology
//!
//! This crate provides a struct [`UriRef`] representing a *[URI reference]*,
//! that is, either a *[URI]* or a *[relative reference]*. If it starts with a *[scheme]*
//! (like `http`, `ftp`, `mailto`, etc.) followed by a colon (`:`), it is a URI. For example,
//! `http://example.com/` and `mailto:user@example.com` are URIs. Otherwise, it is
//! a relative reference. For example, `//example.org/`, `/index.html`, `../`, `foo`,
//! `?bar`, and `#baz` are relative references.
//!
//! You can combine [`parse`] and [`is_uri`] to check whether
//! a string is a valid URI, for example:
//!
//! [URI reference]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.1
//! [URI]: https://datatracker.ietf.org/doc/html/rfc3986#section-3
//! [relative reference]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.2
//! [scheme]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
//! [`parse`]: UriRef::parse
//! [`is_uri`]: UriRef::is_uri
//!
//! ```
//! fn is_valid_uri(s: &str) -> bool {
//!     fluent_uri::UriRef::parse(s).is_ok_and(|r| r.is_uri())
//! }
//!
//! assert!(is_valid_uri("http://example.com/"));
//! assert!(!is_valid_uri("foo"));
//! ```
//!
//! # Guidance for crate users
//!
//! Advice for designers of new URI schemes can be found in [RFC 7595].
//! Guidance on the specification of URI substructure in standards
//! can be found in [RFC 8820]. The crate author recommends [RFC 9413]
//! for further reading as the long-term interoperability
//! of URI schemes may be of concern.
//!
//! [RFC 7595]: https://datatracker.ietf.org/doc/html/rfc7595
//! [RFC 8820]: https://datatracker.ietf.org/doc/html/rfc8820
//! [RFC 9413]: https://datatracker.ietf.org/doc/html/rfc9413
//!
//! # Crate features
//!
//! - `net` (default): Enables [`std::net`] support.
//!   Required for IP address fields in [`Host`] and [`Authority::socket_addrs`].
//!   Disabling `net` will not affect the behavior of [`UriRef::parse`].
//!
//! - `std` (default): Enables [`std`] support. Required for [`Error`] implementations
//!   and [`Authority::socket_addrs`]. Disabling `std` while enabling `net`
//!   requires [`core::net`] and a minimum Rust version of `1.77`.
//!
//! - `serde`: Enables [`serde`] support. Required for [`Serialize`] and [`Deserialize`]
//!   implementations on [`UriRef`].
//!
//! [`Host`]: component::Host
//! [`Error`]: std::error::Error

mod builder;
pub mod component;
pub mod encoding;
pub mod error;
mod fmt;
mod internal;
mod normalizer;
mod parser;
mod resolver;

pub use builder::Builder;

#[cfg(feature = "std")]
extern crate std;

extern crate alloc;

#[cfg(all(feature = "net", feature = "std"))]
use std::net;

#[cfg(all(feature = "net", not(feature = "std")))]
use core::net;

use alloc::{borrow::ToOwned, string::String};
use borrow_or_share::{BorrowOrShare, Bos};
use builder::BuilderStart;
use component::{Authority, Scheme};
use core::{
    borrow::Borrow,
    cmp::Ordering,
    hash,
    str::{self, FromStr},
};
use encoding::{
    encoder::{Fragment, Path, Query},
    EStr, Encoder,
};
use error::{ParseError, ResolveError};
use internal::{Meta, ToUriRef, Value};

#[cfg(feature = "serde")]
use serde::{de, Deserialize, Deserializer, Serialize, Serializer};

/// A URI reference, i.e., either a URI or a relative reference.
///
/// See the [crate-level documentation](crate#terminology) for an explanation of the above terms.
///
/// # Variants
///
/// Two variants of `UriRef` are available: `UriRef<&str>` (borrowed) and `UriRef<String>` (owned).
///
/// `UriRef<&'a str>` outputs references with lifetime `'a` where possible
/// (thanks to [`borrow-or-share`](borrow_or_share)):
///
/// ```
/// use fluent_uri::UriRef;
///
/// // Keep a reference to the path after dropping the `UriRef`.
/// let path = UriRef::parse("foo:bar")?.path();
/// assert_eq!(path, "bar");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
///
/// # Comparison
///
/// `UriRef`s are compared [lexicographically](Ord#lexicographical-comparison)
/// by their byte values. Normalization is **not** performed prior to comparison.
///
/// # Examples
///
/// Parse and extract components from a URI reference:
///
/// ```
/// use fluent_uri::{
///     component::{Host, Scheme},
///     encoding::EStr,
///     UriRef,
/// };
///
/// const SCHEME_FOO: &Scheme = Scheme::new_or_panic("foo");
///
/// let uri_ref = UriRef::parse("foo://user@example.com:8042/over/there?name=ferret#nose")?;
///
/// assert_eq!(uri_ref.scheme().unwrap(), SCHEME_FOO);
///
/// let auth = uri_ref.authority().unwrap();
/// assert_eq!(auth.as_str(), "user@example.com:8042");
/// assert_eq!(auth.userinfo().unwrap(), "user");
/// assert_eq!(auth.host(), "example.com");
/// assert!(matches!(auth.host_parsed(), Host::RegName(name) if name == "example.com"));
/// assert_eq!(auth.port().unwrap(), "8042");
/// assert_eq!(auth.port_to_u16(), Ok(Some(8042)));
///
/// assert_eq!(uri_ref.path(), "/over/there");
/// assert_eq!(uri_ref.query().unwrap(), "name=ferret");
/// assert_eq!(uri_ref.fragment().unwrap(), "nose");
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
///
/// Parse into and convert between `UriRef<&str>` and `UriRef<String>`:
///
/// ```
/// use fluent_uri::UriRef;
///
/// let s = "http://example.com/";
///
/// // Parse into a `UriRef<&str>` from a string slice.
/// let uri_ref: UriRef<&str> = UriRef::parse(s)?;
///
/// // Parse into a `UriRef<String>` from an owned string.
/// let uri_ref_owned: UriRef<String> = UriRef::parse(s.to_owned()).map_err(|e| e.strip_input())?;
///
/// // Convert a `UriRef<&str>` to `UriRef<String>`.
/// let uri_ref_owned: UriRef<String> = uri_ref.to_owned();
///
/// // Borrow a `UriRef<String>` as `UriRef<&str>`.
/// let uri_ref: UriRef<&str> = uri_ref_owned.borrow();
/// # Ok::<_, fluent_uri::error::ParseError>(())
/// ```
#[derive(Clone, Copy)]
pub struct UriRef<T> {
    /// Value of the URI reference.
    val: T,
    /// Metadata of the URI reference.
    /// Should be identical to parser output with `val` as input.
    meta: Meta,
}

impl<T> UriRef<T> {
    /// Parses a URI reference from a string into a `UriRef`.
    ///
    /// The return type is
    ///
    /// - `Result<UriRef<&str>, ParseError>` for `I = &str`;
    /// - `Result<UriRef<String>, ParseError<String>>` for `I = String`.
    ///
    /// # Errors
    ///
    /// Returns `Err` if the string does not match
    /// the [`URI-reference`] ABNF rule from RFC 3986.
    ///
    /// From a [`ParseError<String>`], you may recover or strip the input
    /// by calling [`into_input`] or [`strip_input`] on it.
    ///
    /// [`URI-reference`]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.1
    /// [`into_input`]: ParseError::into_input
    /// [`strip_input`]: ParseError::strip_input
    pub fn parse<I>(input: I) -> Result<Self, I::Err>
    where
        I: ToUriRef<Val = T>,
    {
        input.to_uri_ref()
    }
}

impl UriRef<String> {
    /// Creates a new builder for URI reference.
    #[inline]
    pub fn builder() -> BuilderStart {
        Builder::new()
    }

    /// Borrows this `UriRef<String>` as `UriRef<&str>`.
    #[allow(clippy::should_implement_trait)]
    #[inline]
    #[must_use]
    pub fn borrow(&self) -> UriRef<&str> {
        UriRef {
            val: &self.val,
            meta: self.meta,
        }
    }

    /// Consumes this `UriRef<String>` and yields the underlying [`String`].
    #[inline]
    #[must_use]
    pub fn into_string(self) -> String {
        self.val
    }
}

impl UriRef<&str> {
    /// Creates a new `UriRef<String>` by cloning the contents of this `UriRef<&str>`.
    #[inline]
    #[must_use]
    pub fn to_owned(&self) -> UriRef<String> {
        UriRef {
            val: self.val.to_owned(),
            meta: self.meta,
        }
    }
}

impl<T: Bos<str>> UriRef<T> {
    fn len(&self) -> usize {
        self.as_str().len()
    }

    fn as_ref(&self) -> UriRef<&str> {
        UriRef {
            val: self.as_str(),
            meta: self.meta,
        }
    }
}

impl<'i, 'o, T: BorrowOrShare<'i, 'o, str>> UriRef<T> {
    /// Returns the URI reference as a string slice.
    #[must_use]
    pub fn as_str(&'i self) -> &'o str {
        self.val.borrow_or_share()
    }

    /// Returns a string slice of the `UriRef` between the given indexes.
    fn slice(&'i self, start: usize, end: usize) -> &'o str {
        &self.as_str()[start..end]
    }

    /// Returns an `EStr` slice of the `UriRef` between the given indexes.
    fn eslice<E: Encoder>(&'i self, start: usize, end: usize) -> &'o EStr<E> {
        EStr::new_validated(self.slice(start, end))
    }

    /// Returns the optional [scheme] component.
    ///
    /// Note that the scheme component is *case-insensitive*.
    /// See the documentation of [`Scheme`] for more details on comparison.
    ///
    /// [scheme]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.1
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::{component::Scheme, UriRef};
    ///
    /// const SCHEME_HTTP: &Scheme = Scheme::new_or_panic("http");
    ///
    /// let uri_ref = UriRef::parse("http://example.com/")?;
    /// assert_eq!(uri_ref.scheme(), Some(SCHEME_HTTP));
    ///
    /// let uri_ref = UriRef::parse("/path/to/file")?;
    /// assert_eq!(uri_ref.scheme(), None);
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn scheme(&'i self) -> Option<&'o Scheme> {
        let end = self.meta.scheme_end?.get();
        Some(Scheme::new_validated(self.slice(0, end)))
    }

    /// Returns the optional [authority] component.
    ///
    /// [authority]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.2
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// let uri_ref = UriRef::parse("http://example.com/")?;
    /// assert!(uri_ref.authority().is_some());
    ///
    /// let uri_ref = UriRef::parse("mailto:user@example.com")?;
    /// assert!(uri_ref.authority().is_none());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn authority(&'i self) -> Option<Authority<'o>> {
        let mut meta = self.meta.auth_meta?;
        let start = match self.meta.scheme_end {
            Some(i) => i.get() + 3,
            None => 2,
        };
        let end = self.meta.path_bounds.0;

        meta.host_bounds.0 -= start;
        meta.host_bounds.1 -= start;

        Some(Authority::new(self.slice(start, end), meta))
    }

    /// Returns the [path] component.
    ///
    /// The path component is always present, although it may be empty.
    ///
    /// The returned [`EStr`] slice has [extension methods] for the path component.
    ///
    /// [path]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.3
    /// [extension methods]: EStr#impl-EStr<Path>
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// let uri_ref = UriRef::parse("http://example.com/")?;
    /// assert_eq!(uri_ref.path(), "/");
    ///
    /// let uri_ref = UriRef::parse("mailto:user@example.com")?;
    /// assert_eq!(uri_ref.path(), "user@example.com");
    ///
    /// let uri_ref = UriRef::parse("?lang=en")?;
    /// assert_eq!(uri_ref.path(), "");
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn path(&'i self) -> &'o EStr<Path> {
        self.eslice(self.meta.path_bounds.0, self.meta.path_bounds.1)
    }

    /// Returns the optional [query] component.
    ///
    /// [query]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.4
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::{encoding::EStr, UriRef};
    ///
    /// let uri_ref = UriRef::parse("http://example.com/?lang=en")?;
    /// assert_eq!(uri_ref.query(), Some(EStr::new_or_panic("lang=en")));
    ///
    /// let uri_ref = UriRef::parse("ftp://192.0.2.1/")?;
    /// assert_eq!(uri_ref.query(), None);
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn query(&'i self) -> Option<&'o EStr<Query>> {
        let end = self.meta.query_end?.get();
        Some(self.eslice(self.meta.path_bounds.1 + 1, end))
    }

    fn fragment_start(&self) -> Option<usize> {
        let query_or_path_end = match self.meta.query_end {
            Some(i) => i.get(),
            None => self.meta.path_bounds.1,
        };
        (query_or_path_end != self.len()).then_some(query_or_path_end + 1)
    }

    /// Returns the optional [fragment] component.
    ///
    /// [fragment]: https://datatracker.ietf.org/doc/html/rfc3986#section-3.5
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::{encoding::EStr, UriRef};
    ///
    /// let uri_ref = UriRef::parse("http://example.com/#usage")?;
    /// assert_eq!(uri_ref.fragment(), Some(EStr::new_or_panic("usage")));
    ///
    /// let uri_ref = UriRef::parse("ftp://192.0.2.1/")?;
    /// assert_eq!(uri_ref.fragment(), None);
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn fragment(&'i self) -> Option<&'o EStr<Fragment>> {
        self.fragment_start().map(|i| self.eslice(i, self.len()))
    }

    /// Resolves the URI reference against the given base URI
    /// and returns the target URI.
    ///
    /// The base URI **must** contain a scheme and no fragment, i.e.,
    /// match the [`absolute-URI`] ABNF rule from RFC 3986.
    ///
    /// This method applies the reference resolution algorithm defined in
    /// [Section 5 of RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986#section-5),
    /// except for the following deviations:
    ///
    /// - If `base` contains no authority and its path is [rootless], then
    ///   `self` **must** either contain a scheme, be empty, or start with `'#'`.
    /// - When the target URI contains no authority and its path would start
    ///   with `"//"`, the string `"/."` is prepended to the path. This closes a
    ///   loophole in the original algorithm that resolving `".//@@"` against
    ///   `"foo:/"` yields `"foo://@@"` which is not a valid URI.
    /// - Percent-encoded dot segments (e.g. `"%2E"` and `".%2e"`) are also removed.
    ///   This closes a loophole in the original algorithm that resolving `".."`
    ///   against `"foo:/bar/.%2E/"` yields `"foo:/bar/"`, while first
    ///   normalizing the base URI and then resolving `".."` against it yields `"foo:/"`.
    /// - A slash (`'/'`) is appended to the base URI when it ends with a double-dot
    ///   segment. This closes a loophole in the original algorithm that resolving
    ///   `"."` against `"foo:/bar/.."` yields `"foo:/bar/"`, while first
    ///   normalizing the base URI and then resolving `"."` against it yields `"foo:/"`.
    ///
    /// No normalization except the removal of dot segments will be performed.
    /// Use [`normalize`] if necessary.
    ///
    /// [`absolute-URI`]: https://datatracker.ietf.org/doc/html/rfc3986#section-4.3
    /// [rootless]: EStr::<Path>::is_rootless
    /// [`normalize`]: Self::normalize
    ///
    /// This method has the property that
    /// `self.resolve_against(base).map(|r| r.normalize()).ok()` equals
    /// `self.normalize().resolve_against(&base.normalize()).ok()`.
    ///
    /// # Errors
    ///
    /// Returns `Err` if any of the above two **must**s is violated.
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// let base = UriRef::parse("http://example.com/foo/bar")?;
    ///
    /// let uri_ref = UriRef::parse("baz")?;
    /// assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/foo/baz");
    /// let uri_ref = UriRef::parse("../baz")?;
    /// assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/baz");
    /// let uri_ref = UriRef::parse("?baz")?;
    /// assert_eq!(uri_ref.resolve_against(&base).unwrap(), "http://example.com/foo/bar?baz");
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    pub fn resolve_against<U: Bos<str>>(
        &self,
        base: &UriRef<U>,
    ) -> Result<UriRef<String>, ResolveError> {
        resolver::resolve(base.as_ref(), self.as_ref())
    }

    /// Normalizes the URI reference.
    ///
    /// This method applies the syntax-based normalization described in
    /// [Section 6.2.2 of RFC 3986](https://datatracker.ietf.org/doc/html/rfc3986#section-6.2.2),
    /// which is effectively equivalent to taking the following steps in order:
    ///
    /// - Decode any percent-encoded octet that corresponds to an unreserved character.
    /// - Uppercase the hexadecimal digits within all percent-encoded octets.
    /// - Lowercase the scheme and the host except the percent-encoded octets.
    /// - Turn any IPv6 literal address into its canonical form as per
    ///   [RFC 5952](https://datatracker.ietf.org/doc/html/rfc5952).
    /// - If the port is empty, remove its `':'` delimiter.
    /// - If the URI reference contains a scheme and an absolute path,
    ///   apply the [`remove_dot_segments`] algorithm to the path, taking account of
    ///   percent-encoded dot segments as described at [`resolve_against`].
    /// - If the URI reference contains no authority and its path would start with
    ///   `"//"`, prepend `"/."` to the path.
    ///
    /// This method is idempotent: `self.normalize()` equals `self.normalize().normalize()`.
    ///
    /// [`remove_dot_segments`]: https://datatracker.ietf.org/doc/html/rfc3986#section-5.2.4
    /// [`resolve_against`]: Self::resolve_against
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// let uri_ref = UriRef::parse("eXAMPLE://a/./b/../b/%63/%7bfoo%7d")?;
    /// assert_eq!(uri_ref.normalize(), "example://a/b/c/%7Bfoo%7D");
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn normalize(&self) -> UriRef<String> {
        normalizer::normalize(self.as_ref())
    }

    /// Checks whether the URI reference is a [URI], i.e., contains a scheme.
    ///
    /// This method is equivalent to [`has_scheme`].
    ///
    /// [URI]: https://datatracker.ietf.org/doc/html/rfc3986#section-3
    /// [`has_scheme`]: Self::has_scheme
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// assert!(UriRef::parse("http://example.com/")?.is_uri());
    /// assert!(!UriRef::parse("/path/to/file")?.is_uri());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn is_uri(&self) -> bool {
        self.has_scheme()
    }

    /// Checks whether the URI reference contains a scheme component.
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// assert!(UriRef::parse("http://example.com/")?.has_scheme());
    /// assert!(!UriRef::parse("/path/to/file")?.has_scheme());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn has_scheme(&self) -> bool {
        self.meta.scheme_end.is_some()
    }

    /// Checks whether the URI reference contains an authority component.
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// assert!(UriRef::parse("http://example.com/")?.has_authority());
    /// assert!(!UriRef::parse("mailto:user@example.com")?.has_authority());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn has_authority(&self) -> bool {
        self.meta.auth_meta.is_some()
    }

    /// Checks whether the URI reference contains a query component.
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// assert!(UriRef::parse("http://example.com/?lang=en")?.has_query());
    /// assert!(!UriRef::parse("ftp://192.0.2.1/")?.has_query());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn has_query(&self) -> bool {
        self.meta.query_end.is_some()
    }

    /// Checks whether the URI reference contains a fragment component.
    ///
    /// # Examples
    ///
    /// ```
    /// use fluent_uri::UriRef;
    ///
    /// assert!(UriRef::parse("http://example.com/#usage")?.has_fragment());
    /// assert!(!UriRef::parse("ftp://192.0.2.1/")?.has_fragment());
    /// # Ok::<_, fluent_uri::error::ParseError>(())
    /// ```
    #[must_use]
    pub fn has_fragment(&self) -> bool {
        self.fragment_start().is_some()
    }
}

impl<T: Value> Default for UriRef<T> {
    /// Creates an empty URI reference.
    fn default() -> Self {
        UriRef {
            val: T::default(),
            meta: Meta::default(),
        }
    }
}

impl<T: Bos<str>, U: Bos<str>> PartialEq<UriRef<U>> for UriRef<T> {
    fn eq(&self, other: &UriRef<U>) -> bool {
        self.as_str() == other.as_str()
    }
}

impl<T: Bos<str>> PartialEq<str> for UriRef<T> {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl<T: Bos<str>> PartialEq<UriRef<T>> for str {
    fn eq(&self, other: &UriRef<T>) -> bool {
        self == other.as_str()
    }
}

impl<T: Bos<str>> PartialEq<&str> for UriRef<T> {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl<T: Bos<str>> PartialEq<UriRef<T>> for &str {
    fn eq(&self, other: &UriRef<T>) -> bool {
        *self == other.as_str()
    }
}

impl<T: Bos<str>> Eq for UriRef<T> {}

impl<T: Bos<str>> hash::Hash for UriRef<T> {
    fn hash<H: hash::Hasher>(&self, state: &mut H) {
        self.as_str().hash(state);
    }
}

impl<T: Bos<str>> PartialOrd for UriRef<T> {
    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        Some(self.cmp(other))
    }
}

impl<T: Bos<str>> Ord for UriRef<T> {
    fn cmp(&self, other: &Self) -> Ordering {
        self.as_str().cmp(other.as_str())
    }
}

impl<T: Bos<str>> AsRef<str> for UriRef<T> {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl<T: Bos<str>> Borrow<str> for UriRef<T> {
    fn borrow(&self) -> &str {
        self.as_str()
    }
}

impl From<UriRef<&str>> for UriRef<String> {
    #[inline]
    fn from(uri_ref: UriRef<&str>) -> Self {
        uri_ref.to_owned()
    }
}

impl FromStr for UriRef<String> {
    type Err = ParseError;

    #[inline]
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        UriRef::parse(s).map(|r| r.to_owned())
    }
}

#[cfg(feature = "serde")]
impl<T: Bos<str>> Serialize for UriRef<T> {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        serializer.serialize_str(self.as_str())
    }
}

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

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