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
use reqwest::Url;

pub trait UrlJoinExt {
    fn join_all<S: Into<String>>(&self, paths: Vec<S>) -> Self;
}

impl UrlJoinExt for Url {
    /// Join all the `paths` provided
    ///
    /// Example:
    /// ```ignore
    /// # use reqwest::Url;
    /// assert_eq!(
    ///     Url::parse("https://example.com/").unwrap().join_all(&["join_all", "example", "path"]),
    ///     Url::parse("https://example.com/join_all/example/path").unwrap()
    /// )
    /// ```
    fn join_all<S: Into<String>>(&self, mut paths: Vec<S>) -> Self {
        let mut url = self.clone();
        let last = paths.pop().unwrap();
        for segment in paths {
            let mut segment = segment.into();
            segment.push('/');
            url = url.join(&segment).unwrap();
        }
        url.join(&last.into()).unwrap()
    }
}