summary refs log tree commit diff
path: root/src/util.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.rs')
-rw-r--r--src/util.rs27
1 files changed, 27 insertions, 0 deletions
diff --git a/src/util.rs b/src/util.rs
new file mode 100644
index 0000000..c3d5bd7
--- /dev/null
+++ b/src/util.rs
@@ -0,0 +1,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(())
+    }
+}