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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
|
use anyhow::{Context, Error, Result};
use async_trait::async_trait;
use reqwest::{get, Url};
use urlparse::quote_plus as urlparse_quote_plus;
#[async_trait]
pub(crate) trait UrlShortener {
fn new() -> Self;
async fn shorten(&self, url: &str) -> Result<String, Error>;
}
pub(crate) struct IsgdUrlShortener {}
#[async_trait]
impl UrlShortener for IsgdUrlShortener {
fn new() -> Self {
Self {}
}
async fn shorten(&self, 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")?)
}
}
pub(crate) 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("e_plus("1 * 2")?)?, "1%2B%252A%2B2");
Ok(())
}
}
|