summary refs log tree commit diff
path: root/src/util.rs
diff options
context:
space:
mode:
authorVika <vika@fireburn.ru>2024-08-20 18:41:40 +0300
committerVika <vika@fireburn.ru>2024-08-20 18:41:40 +0300
commit7e403c00af4956a3996e5570eb0aa578745c520d (patch)
tree8e0b26774c119e47a9adc20f0619dbc450c6ef2f /src/util.rs
parente6e4de9e15833e4042be5cd71944e9b8929346d4 (diff)
downloadbowl-7e403c00af4956a3996e5570eb0aa578745c520d.tar.zst
Send posts made in the post composer
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(())
+    }
+}