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
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
|
use std::str::FromStr;
use std::string::ToString;
use std::{error::Error, fmt};
#[derive(Debug)]
pub struct BarbParseError {}
impl Error for BarbParseError {}
impl fmt::Display for BarbParseError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "Error parsing barb file")
}
}
pub enum Method {
GET,
PUT,
POST,
PATCH,
DELETE,
}
impl FromStr for Method {
type Err = BarbParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"GET" => Ok(Self::GET),
"PUT" => Ok(Self::PUT),
"POST" => Ok(Self::POST),
"PATCH" => Ok(Self::PATCH),
"DELETE" => Ok(Self::DELETE),
_ => Err(BarbParseError {}),
}
}
}
impl ToString for Method {
fn to_string(&self) -> String {
match self {
Self::GET => String::from("GET"),
Self::PUT => String::from("PUT"),
Self::POST => String::from("POST"),
Self::PATCH => String::from("PATCH"),
Self::DELETE => String::from("DELETE"),
}
}
}
impl Method {
pub fn takes_body(&self) -> bool {
match self {
Method::GET => false,
Method::DELETE => false,
_ => true
}
}
}
#[derive(Debug)]
pub struct Header {
name: String,
value: String,
}
impl fmt::Display for Header {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{}: {}", self.name, self.value)
}
}
impl Header {
pub fn name(&self) -> &String {
&self.name
}
pub fn value(&self) -> &String {
&self.value
}
}
struct BarbHeader {
pub method: Method,
pub url: String,
pub headers: Vec<Header>,
pub filter: Option<String>,
}
impl BarbHeader {
fn new(method: Method, url: String, headers: Vec<Header>, filter: Option<String>) -> Self {
BarbHeader {
method: method,
url: url,
headers: headers,
filter: filter,
}
}
}
pub struct BarbFile {
header: BarbHeader,
body: Option<String>,
}
impl BarbFile {
pub fn headers(&self) -> &Vec<Header> {
&self.header.headers
}
pub fn method(&self) -> &Method {
&self.header.method
}
pub fn method_as_string(&self) -> String {
self.header.method.to_string()
}
pub fn url(&self) -> &String {
&self.header.url
}
pub fn body(&self) -> &Option<String> {
&self.body
}
}
fn decode_url_line(line: &str) -> Result<(Method, String), BarbParseError> {
let mut components = line[1..].split('^');
let meth = components.next().ok_or(BarbParseError {})?;
let url = components.next().ok_or(BarbParseError {})?;
return Ok((Method::from_str(meth)?, String::from(url)));
}
fn decode_header(line: &str) -> Result<Header, BarbParseError> {
let mut components = line[1..].split(':');
let header_name = components.next().ok_or(BarbParseError {})?;
let header_val = components.next().ok_or(BarbParseError {})?.trim();
Ok(Header {
name: String::from(header_name),
value: String::from(header_val),
})
}
impl FromStr for BarbFile {
type Err = BarbParseError;
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut lines = s.split('\n');
let (method, url) = decode_url_line(lines.next().ok_or(BarbParseError {})?)?;
let mut headers: Vec<Header> = vec![];
let mut filter = None;
for line in &mut lines {
if line == "" {
// End of header.
break;
}
if let Some(_) = line.find(':') {
headers.push(decode_header(line).unwrap());
}
if let None = filter {
filter = match &line[0..2] {
"#|" => Some(String::from(&line[2..])),
_ => None,
}
}
}
let body = lines.fold(String::from(""), |acc, x| acc + x);
Ok(BarbFile {
header: BarbHeader::new(method, url, headers, filter),
body: if body == "" {None} else {Some(body)},
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_method_from_str() {
assert!(matches!(Method::from_str("GET").unwrap(), Method::GET));
assert!(matches!(Method::from_str("GET").unwrap(), Method::GET));
assert!(matches!(Method::from_str("PUT").unwrap(), Method::PUT));
assert!(matches!(Method::from_str("POST").unwrap(), Method::POST));
assert!(matches!(Method::from_str("PATCH").unwrap(), Method::PATCH));
assert!(matches!(
Method::from_str("DELETE").unwrap(),
Method::DELETE
));
}
#[test]
fn test_decode_url_line() {
let (method, url) = decode_url_line("#GET^http://blahblah").unwrap();
assert!(matches!(method, Method::GET));
assert_eq!(url, "http://blahblah");
}
#[test]
fn test_decode_header() {
let hdr = decode_header("#Authorization: TOKEN 12345").unwrap();
assert_eq!(hdr.name, "Authorization");
assert_eq!(hdr.value, "TOKEN 12345");
}
#[test]
fn test_parse_barbfile_no_body() {
let barbfile =
BarbFile::from_str("#GET^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n")
.unwrap();
assert!(matches!(barbfile.header.method, Method::GET));
assert_eq!(barbfile.header.url, "https://blah.com/api/blah");
assert_eq!(barbfile.header.filter, Some(String::from("filtr")));
assert_eq!(barbfile.header.headers.len(), 1);
assert_eq!(barbfile.header.headers[0].name, "Authorization");
assert_eq!(barbfile.header.headers[0].value, "BLAH");
assert_eq!(barbfile.body, None);
}
#[test]
fn test_parse_barbfile_body() {
let barbfile =
BarbFile::from_str("#POST^https://blah.com/api/blah\n#Authorization: BLAH\n#|filtr\n\n{\"key\":\"value\"}\n")
.unwrap();
assert!(matches!(barbfile.header.method, Method::POST));
assert_eq!(barbfile.header.url, "https://blah.com/api/blah");
assert_eq!(barbfile.header.filter, Some(String::from("filtr")));
assert_eq!(barbfile.header.headers.len(), 1);
assert_eq!(barbfile.header.headers[0].name, "Authorization");
assert_eq!(barbfile.header.headers[0].value, "BLAH");
assert_eq!(barbfile.body, Some(String::from("{\"key\":\"value\"}")))
}
}
|