aboutsummaryrefslogtreecommitdiff
path: root/src/util/web
diff options
context:
space:
mode:
authorMax Audron <audron@cocaine.farm>2021-10-22 19:08:59 +0200
committerMax Audron <audron@cocaine.farm>2021-10-22 19:09:39 +0200
commit309899168a086de88acf97fd6683387a7af7078c (patch)
tree846075c1e9af0d7139edae5597f1147b851ed2b2 /src/util/web
parentremove wolfram alpha url shortening (diff)
write tons of documentation and reorganize some modules
Diffstat (limited to 'src/util/web')
-rw-r--r--src/util/web/mod.rs42
-rw-r--r--src/util/web/url_shorteners.rs21
2 files changed, 63 insertions, 0 deletions
diff --git a/src/util/web/mod.rs b/src/util/web/mod.rs
new file mode 100644
index 0000000..4e886af
--- /dev/null
+++ b/src/util/web/mod.rs
@@ -0,0 +1,42 @@
+use anyhow::{Error, Result};
+use async_trait::async_trait;
+use urlparse::quote_plus as urlparse_quote_plus;
+
+pub mod url_shorteners;
+
+/// Shorten urls
+#[async_trait]
+pub trait UrlShortener {
+ /// Call this method with the url you want shortened.
+ /// Returns the shortened url.
+ async fn shorten(url: &str) -> Result<String, Error>;
+}
+
+/// quote strings to be URL save
+pub fn quote_plus(text: &str) -> Result<String, Error> {
+ Ok(urlparse_quote_plus(text, b"")?)
+}
+
+#[cfg(test)]
+mod tests {
+ use super::quote_plus;
+ use anyhow::{Error, Result};
+
+ #[test]
+ fn test_quote_plus_1() -> Result<(), Error> {
+ assert_eq!(quote_plus("5/10")?, "5%2F10");
+ Ok(())
+ }
+
+ #[test]
+ fn test_quote_plus_2() -> Result<(), Error> {
+ assert_eq!(quote_plus("1 * 2")?, "1+%2A+2");
+ Ok(())
+ }
+
+ #[test]
+ fn test_quote_plus_3() -> Result<(), Error> {
+ assert_eq!(quote_plus(&quote_plus("1 * 2")?)?, "1%2B%252A%2B2");
+ Ok(())
+ }
+}
diff --git a/src/util/web/url_shorteners.rs b/src/util/web/url_shorteners.rs
new file mode 100644
index 0000000..74d62ce
--- /dev/null
+++ b/src/util/web/url_shorteners.rs
@@ -0,0 +1,21 @@
+use anyhow::{Context, Error, Result};
+use async_trait::async_trait;
+use reqwest::{get, Url};
+
+pub struct Isgd;
+
+#[async_trait]
+impl super::UrlShortener for Isgd {
+ async fn shorten(url: &str) -> Result<String, Error> {
+ Ok(get(Url::parse(&format!(
+ "https://is.gd/create.php?format=simple&url={}",
+ url
+ ))
+ .context("Failed to parse url")?)
+ .await
+ .context("Failed to make request")?
+ .text()
+ .await
+ .context("failed to get request response text")?)
+ }
+}