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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
const PASTE_RS_URL: &str = "https://paste.rs/";
use core::result::Result as coreResult;
#[derive(Debug)]
pub struct Paste {
pub id: String,
pub status_code: Option<reqwest::StatusCode>,
}
impl Paste {
pub fn from(val: &str) -> Result<Self> {
if is_url(val) && is_paste_rs_url(val) {
Ok(Paste {
id: extract_paste_id(&val.to_string())?,
status_code: None,
})
} else if !is_url(val) && is_paste_rs_url(val) {
let full_url = format!("https://{}", val);
Ok(Paste {
id: extract_paste_id(&full_url.to_string())?,
status_code: None,
})
} else if val.len() == 3 {
Ok(Paste {
id: val.to_string(),
status_code: None,
})
} else if is_url(val) && !is_paste_rs_url(val) {
Err(Error::InvalidUrl)
} else {
Err(Error::InvalidArguments)
}
}
pub async fn new(data: String) -> Result<Self> {
let client = reqwest::Client::new();
let res = client
.post(PASTE_RS_URL)
.body(data)
.header("Content-Type", "text/plain")
.send()
.await?;
match res.error_for_status() {
Ok(res) => {
let res_stat = res.status();
let res_text = res.text().await?;
Ok(Paste {
status_code: Some(res_stat),
id: extract_paste_id(&res_text)?,
})
}
Err(e) => Err(Error::ReqwestError(e)),
}
}
pub async fn get(&self) -> Result<String> {
let res = reqwest::get(self.get_url()).await?.text().await?;
Ok(res)
}
pub fn get_url(&self) -> String {
format!("{}/{}", PASTE_RS_URL, self.id)
}
}
fn is_url(url: &str) -> bool {
if url.contains("http://") || url.contains("https://") {
true
} else {
false
}
}
fn is_paste_rs_url(url: &str) -> bool {
if url.contains("paste.rs") {
true
} else {
false
}
}
fn extract_paste_id(url: &String) -> coreResult<String, Error> {
if url.contains(PASTE_RS_URL) {
Ok(url.replace(PASTE_RS_URL, ""))
} else {
Err(Error::InvalidUrl)
}
}
pub type Result<T> = coreResult<T, Error>;
#[derive(Debug)]
pub enum Error {
InvalidUrl,
InvalidArguments,
ReqwestError(reqwest::Error),
}
impl std::convert::From<reqwest::Error> for Error {
fn from(err: reqwest::Error) -> Self {
Error::ReqwestError(err)
}
}
impl std::error::Error for Error {
fn source(&self) -> Option<&(dyn std::error::Error + 'static)> {
match &self {
Error::ReqwestError(err) => Some(err),
_ => None,
}
}
}
impl std::fmt::Display for Error {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match &self {
Error::InvalidArguments => write!(f, "Invalid Arguments"),
Error::InvalidUrl => write!(f, "Invalid Url"),
Error::ReqwestError(req_err) => write!(f, "{}", req_err),
}
}
}