lwuri 0.1.1

Crate for using and manipulating Uniform Resource Identifiers with a minimal memory footprint and strong typing.
Documentation
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
// Copyright 2019 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     https://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

use super::*;
use core::convert::TryFrom;
use core::ops::Deref;
use core::str::FromStr;

use alloc::string::{String, ToString};

/// Sized, heap-allocated string type containing either a URI or a relative-reference.
///
/// Similar to `String`, but with additional guarantees on internal structure.
/// The unsized counterpart is `UriRef`.
///
/// This type implements [`std::ops::Deref<UriRef>`], so you can also use all of the
/// methods from [`UriRef`](crate::UriRef) on this type.
#[derive(Clone, Eq, Hash)]
pub struct UriRefBuf(pub(super) String);

_impl_uri_buf_traits_base!(UriRefBuf, UriRef);

impl FromStr for UriRefBuf {
    type Err = ParseError;

    fn from_str(input: &str) -> Result<Self, Self::Err> {
        Self::from_str(input)
    }
}

impl TryFrom<&str> for UriRefBuf {
    type Error = ParseError;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        Self::from_str(value)
    }
}

impl TryFrom<String> for UriRefBuf {
    type Error = ParseError;

    fn try_from(value: String) -> Result<Self, Self::Error> {
        Self::from_string(value)
    }
}

impl<'a> TryFrom<&'a String> for UriRefBuf {
    type Error = <Self as TryFrom<&'a str>>::Error;

    fn try_from(value: &'a String) -> Result<Self, Self::Error> {
        <Self as TryFrom<&'a str>>::try_from(value.as_str())
    }
}

impl Default for UriRefBuf {
    fn default() -> Self {
        Self::new()
    }
}

impl Deref for UriRefBuf {
    type Target = UriRef;

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

impl AsRef<String> for UriRefBuf {
    fn as_ref(&self) -> &String {
        &self.0
    }
}

impl core::fmt::Display for UriRefBuf {
    fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result {
        self.write_to(f)
    }
}

/// # Constructors
impl UriRefBuf {
    /// Creates a new, empty [`UriRefBuf`].
    pub fn new() -> UriRefBuf {
        UriRefBuf(String::new())
    }

    /// Creates a new, empty [`UriRefBuf`] with a capacity of `capacity`.
    pub fn with_capacity(capacity: usize) -> UriRefBuf {
        UriRefBuf(String::with_capacity(capacity))
    }

    /// Attempts to create a new [`UriRefBuf`] from a string reference.
    pub fn from_str<S: AsRef<str> + Copy>(s: S) -> Result<Self, ParseError> {
        let str_ref = s.as_ref();
        UriRef::from_str(str_ref)?;
        Ok(UriRefBuf(str_ref.to_string()))
    }

    /// Attempts to create a new [`UriRefBuf`] from a [`String`].
    pub fn from_string(s: String) -> Result<Self, ParseError> {
        UriRef::from_str(s.as_str())?;
        Ok(UriRefBuf(s))
    }
}

/// # Conversions
impl UriRefBuf {
    /// Borrows a string slice containing this URI reference.
    #[inline(always)]
    pub fn as_str(&self) -> &str {
        self.0.as_str()
    }

    /// Borrows a [`UriRef`] slice containing this URI reference.
    #[inline(always)]
    pub fn as_uri_ref(&self) -> &UriRef {
        unsafe { UriRef::from_str_unchecked(self.as_str()) }
    }

    /// Borrows a mutable [`UriRef`] slice containing this URI reference.
    #[inline(always)]
    pub fn as_mut_uri_ref(&mut self) -> &mut UriRef {
        unsafe { UriRef::from_str_unchecked_mut(self.as_mut_str()) }
    }
}

/// # Manipulation
impl UriRefBuf {
    /// Removes fragment component, if present.
    pub fn truncate_fragment(&mut self) {
        if let Some(i) = self.fragment_start() {
            self.0.truncate(i)
        }
    }

    /// Truncates the authority, path, query, and fragment components of a `UriRefBuf`.
    pub fn truncate_heir_part(&mut self) {
        self.0.truncate(self.heir_part_start())
    }

    /// Removes query and fragment components, if present.
    /// E.g.:
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com/blah/bleh?query").to_uri_ref_buf();
    /// uri.truncate_query();
    /// assert_eq!("http://example.com/blah/bleh", uri.as_str());
    /// ```
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com/blah/bleh#foobar").to_uri_ref_buf();
    /// uri.truncate_query();
    /// assert_eq!("http://example.com/blah/bleh", uri.as_str());
    /// ```
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("another?foo#bar").to_uri_ref_buf();
    /// uri.truncate_query();
    /// assert_eq!("another", uri.as_str());
    /// ```
    pub fn truncate_query(&mut self) {
        if let Some(i) = self.query_start().or_else(|| self.fragment_start()) {
            self.0.truncate(i)
        }
    }

    /// Truncates the path, query, and fragments components of a `UriRefBuf`.
    pub fn truncate_path(&mut self) {
        self.0.truncate(self.path_start());
    }

    /// Removes the last path component (up to, but not including, the last slash), along with
    /// the query and fragment components, if present.
    /// E.g.:
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com/blah/bleh").to_uri_ref_buf();
    /// uri.truncate_resource();
    /// assert_eq!("http://example.com/blah/", uri.as_str());
    /// ```
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com/blah/").to_uri_ref_buf();
    /// uri.truncate_resource();
    /// assert_eq!("http://example.com/blah/", uri.as_str());
    /// ```
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("foo#bar").to_uri_ref_buf();
    /// uri.truncate_resource();
    /// assert_eq!("", uri.as_str());
    /// ```
    /// * `http://example.com/blah/bleh` becomes `http://example.com/blah/`
    /// * `http://example.com/blah/` becomes `http://example.com/blah/`
    /// * `foo#bar` becomes ``
    pub fn truncate_resource(&mut self) {
        self.truncate_query();
        let path_start = self.as_uri_ref().path_start();

        if let Some(i) = self.as_str().rfind('/') {
            if i + 1 > path_start {
                self.0.truncate(i + 1);
            }
        } else if path_start == 0 {
            self.clear();
        }
    }

    /// Removes the last path item, along with
    /// the query and fragment components, if present.
    ///
    /// This method will only result in an empty path if the path was empty to begin with.
    ///
    /// E.g.:
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com/blah/bleh").to_uri_ref_buf();
    /// uri.truncate_last_path_segment();
    /// assert_eq!("http://example.com/blah/", uri.as_str());
    /// uri.truncate_last_path_segment();
    /// assert_eq!("http://example.com/", uri.as_str());
    /// uri.truncate_last_path_segment();
    /// assert_eq!("http://example.com/", uri.as_str());
    /// ```
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("foo#bar").to_uri_ref_buf();
    /// uri.truncate_last_path_segment();
    /// assert_eq!("./", uri.as_str());
    /// ```
    /// * `http://example.com/blah/bleh` becomes `http://example.com/blah/`
    /// * `http://example.com/blah/` becomes `http://example.com/`
    /// * `foo#bar` becomes ``
    pub fn truncate_last_path_segment(&mut self) {
        let path_start = self.as_uri_ref().path_start();
        let path_end = self.as_uri_ref().path_end();

        if path_start == path_end {
            return;
        }

        self.truncate_query();

        let mut s = self.as_str();

        // Trim off any trailing slash (but only 1)
        if s.ends_with('/') {
            s = &s[..s.len() - 1];
        }

        if let Some(i) = s.rfind('/') {
            if i + 1 > path_start {
                self.0.truncate(i + 1);
            }
        } else if path_start == 0 {
            self.clear();
        }

        if self.raw_path().is_empty() {
            // The empty URI has special behaviors that we don't
            // want here, so make ourselves "./" here instead.
            self.0.push_str("./");
        }
    }

    /// Completely clears this `UriRefBuf`, leaving it empty.
    pub fn clear(&mut self) {
        self.0.clear()
    }

    /// Adds a trailing slash to the path if there isn't a trailing slash already present.
    pub fn add_trailing_slash(&mut self) -> bool {
        if self.is_empty() {
            self.0.push_str("./");
            true
        } else {
            let path_end = self.path_end();
            if path_end > 0 && &self[path_end - 1..path_end] == "/" {
                false
            } else {
                self.0.insert(path_end, '/');
                true
            }
        }
    }

    /// Adds a leading slash to the path if there isn't one already present.
    pub fn add_leading_slash(&mut self) -> bool {
        let path_begin = self.path_start();
        if self.len() > 0 && &self[path_begin..path_begin + 1] == "/" {
            false
        } else {
            self.0.insert(path_begin, '/');
            true
        }
    }

    /// Using this URI-reference as the base, performs "relative resolution" to the given instance
    /// implementing [`AnyUriRef`], updating the content of this `UriRefBuf` with the result.
    pub fn resolve<T: AnyUriRef + ?Sized>(&mut self, dest: &T) -> Result<(), ResolveError> {
        if !dest.is_empty() {
            *self = self.resolved(dest)?;
        }
        Ok(())
    }

    /// Percent-encodes and appends the given path segment to this URI-reference,
    /// truncating any existing query or fragment in the process.
    ///
    /// If this URI-reference isn't empty and doesn't end with a slash, one
    /// is first added. A trailing slash will be appended depending on the value
    /// of the `trailing_slash` argument.
    ///
    /// # Special Cases
    ///
    /// Calling `x.push_path_segment(".", false)` will do nothing.
    ///
    /// Calling `x.push_path_segment(".", true)` is the same as calling `x.add_trailing_slash()`.
    ///
    /// Calling `x.push_path_segment("..", _)` is the same as calling
    /// `x.truncate_last_path_segment()`. In this case the value of `trailing_slash` is ignored.
    ///
    /// # Example
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com").to_uri_ref_buf();
    ///
    /// uri.push_path_segment("foobar", false);
    /// assert_eq!(uri, uri_ref!("http://example.com/foobar"));
    ///
    /// uri.push_path_segment("a/b/c", true);
    /// assert_eq!(uri, uri_ref!("http://example.com/foobar/a%2Fb%2Fc/"));
    /// ```
    pub fn push_path_segment(&mut self, segment: &str, trailing_slash: bool) {
        if segment == "." {
            if trailing_slash {
                self.add_trailing_slash();
            } else if self.is_empty() {
                // this is the only case where we actually add the dot.
                self.0.push_str(".");
            }
            return;
        }

        if segment == ".." {
            self.truncate_last_path_segment();
            return;
        }

        self.truncate_query();

        if !self.is_empty() {
            self.add_trailing_slash();
        }

        self.0.extend(segment.escape_uri());

        if trailing_slash {
            self.add_trailing_slash();
        }
    }

    /// Percent-encodes and appends the given query item to this instance,
    /// truncating any existing fragment in the process.
    ///
    /// If no query is present, the query item is preceded with a '?' to
    /// indicate the start of the query component. Otherwise, this method
    /// uses `&` to separate query items.
    ///
    /// This method follows the common convention where spaces are encoded
    /// as `+` characters instead of `%20`.
    ///
    /// # Example
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com").to_uri_ref_buf();
    ///
    /// uri.push_query_item("foobar");
    /// assert_eq!(uri, uri_ref!("http://example.com?foobar"));
    ///
    /// uri.push_query_item("q=a vast query");
    /// assert_eq!(uri, uri_ref!("http://example.com?foobar&q=a+vast+query"));
    /// ```
    pub fn push_query_item(&mut self, item: &str) {
        self.truncate_fragment();

        if self.query_start().is_some() {
            self.0.push('&');
        } else {
            self.0.push('?');
        }

        self.0.extend(item.escape_uri().for_query());
    }

    /// Percent-encodes and appends the given query key/value pair to this URI-reference,
    /// truncating any existing fragment in the process.
    ///
    /// If no query is present, the query item is preceded with a '?' to
    /// indicate the start of the query component. Otherwise, this method
    /// uses `&` to separate query items.
    ///
    /// This method follows the common convention where spaces are encoded
    /// as `+` characters instead of `%20`.
    ///
    /// # Example
    ///
    /// ```
    /// # use lwuri::*;
    /// let mut uri = uri_ref!("http://example.com").to_uri_ref_buf();
    ///
    /// uri.push_query_key_value("foo","bar");
    /// assert_eq!(uri, uri_ref!("http://example.com?foo=bar"));
    ///
    /// uri.push_query_key_value("q","a vast query");
    /// assert_eq!(uri, uri_ref!("http://example.com?foo=bar&q=a+vast+query"));
    /// ```
    pub fn push_query_key_value(&mut self, key: &str, value: &str) {
        self.truncate_fragment();

        if self.query_start().is_some() {
            self.0.push('&');
        } else {
            self.0.push('?');
        }

        self.0.extend(key.escape_uri().for_query());
        self.0.push('=');
        self.0.extend(value.escape_uri().for_query());
    }

    /// Replaces the path, query, and fragment with that from `rel`.
    pub fn replace_path(&mut self, rel: &RelRef) {
        self.truncate_path();

        if !rel.starts_with(|c| c == '/' || c == '?' || c == '#') {
            self.add_trailing_slash();
        }

        self.0.push_str(rel.as_str());
    }
}

/// # Unsafe Methods
///
/// `UriRefBuf` needs some unsafe methods in order to function properly. This section is where
/// they are all located.
impl UriRefBuf {
    /// Unchecked version of [`UriRefBuf::from_string`].
    ///
    /// # Safety
    ///
    /// This method is marked as unsafe because it allows you to construct a `UriRefBuf` with
    /// a value that is not a well-formed URI reference.
    pub unsafe fn from_string_unchecked(s: String) -> UriRefBuf {
        UriRefBuf(s)
    }

    /// Borrows a mutable string slice containing this URI reference.
    ///
    /// This method is marked as unsafe because it allows you to change the
    /// content of the URI reference without any checks on syntax.
    #[inline(always)]
    pub unsafe fn as_mut_str(&mut self) -> &mut str {
        self.0.as_mut_str()
    }

    /// Borrows a mutable [`String`] reference containing this URI reference.
    ///
    /// This method is marked as unsafe because it allows you to change the
    /// content of the URI reference without any checks on syntax.
    pub unsafe fn as_mut_string_ref(&mut self) -> &mut String {
        &mut self.0
    }
}

#[doc(hidden)]
#[macro_export]
macro_rules! inherits_uri_ref_buf {
    ($BUF:ident) => {
        /// # Methods inherited from [`UriRefBuf`]
        impl $BUF {
            /// Returns a string slice for this instance.
            #[inline(always)]
            pub fn as_str(&self) -> &str {
                self.0.as_str()
            }

            /// Returns a mutable string slice (`&mut str`) for this instance.
            ///
            /// ## Safety
            ///
            /// This method is not safe because this type makes guarantees about
            /// the structure of the content it contains, which may be violated by
            /// using this method.
            #[inline(always)]
            pub unsafe fn as_mut_str(&mut self) -> &mut str {
                self.0.as_mut_str()
            }

            /// Borrows a reference to this mutable instance as a mutable URI-Reference
            /// (`&mut UriRef`).
            #[inline(always)]
            pub fn as_mut_uri_ref(&mut self) -> &mut UriRef {
                self.0.as_mut_uri_ref()
            }

            /// Removes the authority, path, query, and fragment components, if present.
            #[inline(always)]
            pub fn truncate_heir_part(&mut self) {
                self.0.truncate_heir_part()
            }

            /// Removes the path, query, and fragment components, if present.
            #[inline(always)]
            pub fn truncate_path(&mut self) {
                self.0.truncate_path()
            }

            /// Removes the query, and fragment components, if present.
            #[inline(always)]
            pub fn truncate_query(&mut self) {
                self.0.truncate_query()
            }

            /// Removes fragment component, if present.
            #[inline(always)]
            pub fn truncate_fragment(&mut self) {
                self.0.truncate_fragment()
            }

            /// Removes the last path component (up to, but not including, the last slash),
            /// along with the query and fragment components, if present.
            ///
            /// See [`UriRefBuf::truncate_resource`] for more information.
            #[inline(always)]
            pub fn truncate_resource(&mut self) {
                self.0.truncate_resource()
            }

            /// Removes the last path item, along with
            /// the query and fragment components, if present.
            ///
            /// See [`UriRefBuf::truncate_last_path_segment`] for more information.
            #[inline(always)]
            pub fn truncate_last_path_segment(&mut self) {
                self.0.truncate_last_path_segment()
            }

            /// Adds a trailing slash to the path if there isn't a trailing slash already present.
            #[inline(always)]
            pub fn add_trailing_slash(&mut self) -> bool {
                self.0.add_trailing_slash()
            }

            /// Adds a leading slash to the path if there isn't one already present.
            #[inline(always)]
            pub fn add_leading_slash(&mut self) -> bool {
                self.0.add_leading_slash()
            }

            /// Percent-encodes and appends the given path segment to this instance,
            /// truncating any existing query or fragment in the process.
            ///
            /// If this instance isn't empty and doesn't end with a slash, one
            /// is first added. A trailing slash will be appended depending on the value
            /// of the `trailing_slash` argument.
            ///
            #[inline(always)]
            pub fn push_path_segment(&mut self, segment: &str, trailing_slash: bool) {
                self.0.push_path_segment(segment, trailing_slash)
            }

            /// Percent-encodes and appends the given query item to this instance,
            /// truncating any existing fragment in the process.
            ///
            /// If no query is present, the query item is preceded with a '?' to
            /// indicate the start of the query component. Otherwise, this method
            /// uses `&` to separate query items.
            ///
            /// This method follows the common convention where spaces are encoded
            /// as `+` characters instead of `%20`.
            #[inline(always)]
            pub fn push_query_item(&mut self, item: &str) {
                self.0.push_query_item(item)
            }

            /// Percent-encodes and appends the given query key/value pair to this URI-reference,
            /// truncating any existing fragment in the process.
            ///
            /// If no query is present, the query item is preceded with a '?' to
            /// indicate the start of the query component. Otherwise, this method
            /// uses `&` to separate query items.
            ///
            /// This method follows the common convention where spaces are encoded
            /// as `+` characters instead of `%20`.
            #[inline(always)]
            pub fn push_query_key_value(&mut self, key: &str, value: &str) {
                self.0.push_query_key_value(key, value)
            }
        }
    };
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_try_from() {
        assert_eq!(
            UriRefBuf::try_from("https://www.google.com/"),
            UriRefBuf::from_str("https://www.google.com/")
        );

        assert_eq!(
            UriRefBuf::try_from("https://www.google.com/".to_string()),
            UriRefBuf::from_str("https://www.google.com/")
        );

        assert_eq!(
            UriRefBuf::try_from(&"https://www.google.com/".to_string()),
            UriRefBuf::try_from("https://www.google.com/"),
        );
    }

    #[test]
    fn test_from_str() {
        assert_eq!(
            <UriRefBuf as FromStr>::from_str("https://www.google.com/"),
            UriRefBuf::from_str("https://www.google.com/")
        );

        assert!(UriRefBuf::from_str("http://example.com/").is_ok());
    }

    #[test]
    fn push_path_segment() {
        let mut uri = iuri_ref!("").to_uri_ref_buf();

        uri.push_path_segment(".", false);
        assert_eq!(uri, iuri_ref!("."));

        let mut uri = iuri_ref!("").to_uri_ref_buf();

        uri.push_path_segment("foobar", false);
        assert_eq!(uri, iuri_ref!("foobar"));

        uri.push_path_segment("a/b/c", true);
        assert_eq!(uri, iuri_ref!("foobar/a%2Fb%2Fc/"));

        uri.push_path_segment(".", true);
        assert_eq!(uri, iuri_ref!("foobar/a%2Fb%2Fc/"));

        uri.push_path_segment("..", false);
        assert_eq!(uri, iuri_ref!("foobar/"));

        uri.push_path_segment("..", false);
        assert_eq!(uri, iuri_ref!("./"));
    }

    #[test]
    fn add_trailing_slash() {
        let mut uri = iuri_ref!("example/").to_uri_ref_buf();
        assert_eq!(false, uri.add_trailing_slash());

        let mut uri = iuri_ref!("example").to_uri_ref_buf();
        assert_eq!(true, uri.add_trailing_slash());
        assert_eq!(iuri_ref!("example/"), &uri);

        let mut uri = iuri_ref!("example?").to_uri_ref_buf();
        assert_eq!(true, uri.add_trailing_slash());
        assert_eq!(iuri_ref!("example/?"), &uri);

        let mut uri = iuri_ref!("example#").to_uri_ref_buf();
        assert_eq!(true, uri.add_trailing_slash());
        assert_eq!(iuri_ref!("example/#"), &uri);

        let mut uri = iuri_ref!("example?/#/").to_uri_ref_buf();
        assert_eq!(true, uri.add_trailing_slash());
        assert_eq!(iuri_ref!("example/?/#/"), &uri);

        let mut uri = iuri_ref!("/e/x/a/m/p/l/e?/#/").to_uri_ref_buf();
        assert_eq!(true, uri.add_trailing_slash());
        assert_eq!(iuri_ref!("/e/x/a/m/p/l/e/?/#/"), &uri);
    }

    #[test]
    fn add_leading_slash() {
        let mut uri = iuri_ref!("/example").to_uri_ref_buf();
        assert_eq!(false, uri.add_leading_slash());

        let mut uri = iuri_ref!("example").to_uri_ref_buf();
        assert_eq!(true, uri.add_leading_slash());
        assert_eq!(iuri_ref!("/example"), &uri);

        let mut uri = iuri_ref!("example?").to_uri_ref_buf();
        assert_eq!(true, uri.add_leading_slash());
        assert_eq!(iuri_ref!("/example?"), &uri);

        let mut uri = iuri_ref!("example#").to_uri_ref_buf();
        assert_eq!(true, uri.add_leading_slash());
        assert_eq!(iuri_ref!("/example#"), &uri);

        let mut uri = iuri_ref!("example?/#/").to_uri_ref_buf();
        assert_eq!(true, uri.add_leading_slash());
        assert_eq!(iuri_ref!("/example?/#/"), &uri);

        let mut uri = iuri_ref!("e/x/a/m/p/l/e/?/#/").to_uri_ref_buf();
        assert_eq!(true, uri.add_leading_slash());
        assert_eq!(iuri_ref!("/e/x/a/m/p/l/e/?/#/"), &uri);
    }
}