summary refs log tree commit diff
path: root/src/util.rs
blob: c3d5bd7ae60e18a3e339ad2aa221b1c2f8708930 (plain) (blame)
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
use std::borrow::Cow;

pub fn append_query(uri: &glib::Uri, q: impl IntoIterator<Item = (String, String)>) -> glib::Uri {
    let mut oq: Vec<(Cow<'static, str>, Cow<'static, str>)> = uri.query()
        .map(|q| serde_urlencoded::from_str(&q).unwrap())
        .unwrap_or_default();
    oq.extend(q.into_iter().map(|(k, v)| (k.into(), v.into())));
    let nq = "?".to_owned() + &serde_urlencoded::to_string(oq).unwrap();
    uri.parse_relative(&nq, glib::UriFlags::NONE).unwrap()
}

#[cfg(test)]
mod tests {
    #[test]
    fn test_append_query() -> Result<(), glib::Error> {
        let uri = glib::Uri::parse("https://fireburn.ru/.kittybox/micropub?test=a", glib::UriFlags::NONE)?;
        let q = [
            ("q".to_owned(), "config".to_owned()),
            ("awoo".to_owned(), "nya".to_owned()),
        ];
        assert_eq!(
            super::append_query(&uri, q).to_string().as_str(),
            "https://fireburn.ru/.kittybox/micropub?test=a&q=config&awoo=nya"
        );
        Ok(())
    }
}