From 7e403c00af4956a3996e5570eb0aa578745c520d Mon Sep 17 00:00:00 2001 From: Vika Date: Tue, 20 Aug 2024 18:41:40 +0300 Subject: Send posts made in the post composer --- src/util.rs | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) create mode 100644 src/util.rs (limited to 'src/util.rs') 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) -> 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(()) + } +} -- cgit 1.4.1