Skip to content

Commit 8834057

Browse files
author
Leo Pourcelot
committed
Refactored try_read_map, try_read_list and try_read_vector
These three functions had the same similar pattern, which can be simplified by using an `if-let` expression instead of a match as well as the `?` operator, which simplifies error handling.
1 parent 03fb1e4 commit 8834057

File tree

1 file changed

+25
-55
lines changed

1 file changed

+25
-55
lines changed

src/reader.rs

Lines changed: 25 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -205,24 +205,20 @@ pub fn try_read_map(input: &str) -> IResult<&str, Value> {
205205
named!(lbracep<&str, &str>, preceded!(consume_clojure_whitespaces, tag!("{")));
206206
named!(rbracep<&str, &str>, preceded!(consume_clojure_whitespaces, tag!("}")));
207207
let (map_inner_input, _) = lbracep(input)?;
208-
let mut map_as_vec: Vec<MapEntry> = vec![];
208+
let mut map_as_vec: Vec<MapEntry> = Vec::new();
209209
let mut rest_input = map_inner_input;
210210
loop {
211211
let right_brace = rbracep(rest_input);
212-
match right_brace {
213-
Ok((after_map_input, _)) => {
214-
break Ok((after_map_input, map_as_vec.into_list_map().to_value()));
215-
}
216-
_ => {
217-
let (_rest_input, next_key) = try_read(rest_input)?;
218-
let (_rest_input, next_val) = try_read(_rest_input)?;
219-
map_as_vec.push(MapEntry {
220-
key: Rc::new(next_key),
221-
val: Rc::new(next_val),
222-
});
223-
rest_input = _rest_input;
224-
}
212+
if let Ok((after_map_input, _)) = right_brace {
213+
return Ok((after_map_input, map_as_vec.into_list_map().to_value()));
225214
}
215+
let (_rest_input, next_key) = try_read(rest_input)?;
216+
let (_rest_input, next_val) = try_read(_rest_input)?;
217+
map_as_vec.push(MapEntry {
218+
key: Rc::new(next_key),
219+
val: Rc::new(next_val),
220+
});
221+
rest_input = _rest_input;
226222
}
227223
}
228224

@@ -236,33 +232,20 @@ pub fn try_read_vector(input: &str) -> IResult<&str, Value> {
236232
named!(lbracketp<&str, &str>, preceded!(consume_clojure_whitespaces, tag!("[")));
237233
named!(rbracketp<&str, &str>, preceded!(consume_clojure_whitespaces, tag!("]")));
238234
let (vector_inner_input, _) = lbracketp(input)?;
239-
let mut vector_as_vec = vec![];
235+
let mut vector_as_vec = Vec::new();
240236
// What's left of our input as we read more of our PersistentVector
241237
let mut rest_input = vector_inner_input;
242238
loop {
243239
// Try parse end of vector
244-
let right_paren = rbracketp(rest_input);
245-
match right_paren {
246-
// If we succeeded, we can convert our vector of values into a PersistentVector and return our success
247-
Ok((after_vector_input, _)) => {
248-
break Ok((after_vector_input, vector_as_vec.into_vector().to_value()));
249-
}
250-
// Otherwise, we need to keep reading until we get that closing bracket letting us know we're finished
251-
_ => {
252-
let next_form_parse = try_read(rest_input);
253-
match next_form_parse {
254-
// Normal behavior; read our next element in the PersistentVector
255-
Ok((_rest_input, form)) => {
256-
vector_as_vec.push(form.to_rc_value());
257-
rest_input = _rest_input;
258-
}
259-
// This parse failed, return overall read failure
260-
_ => {
261-
break next_form_parse;
262-
}
263-
}
264-
}
240+
// If we succeeded, we can convert our vector of values into a PersistentVector and return our success
241+
if let Ok((after_vector_input, _)) = rbracketp(rest_input) {
242+
return Ok((after_vector_input, vector_as_vec.into_vector().to_value()));
265243
}
244+
245+
// Otherwise, we need to keep reading until we get that closing bracket letting us know we're finished
246+
let (_rest_input, form) = try_read(rest_input)?;
247+
vector_as_vec.push(form.to_rc_value());
248+
rest_input = _rest_input;
266249
}
267250
}
268251

@@ -271,28 +254,15 @@ pub fn try_read_list(input: &str) -> IResult<&str, Value> {
271254
named!(rparenp<&str, &str>, preceded!(consume_clojure_whitespaces, tag!(")")));
272255

273256
let (list_inner_input, _) = lparenp(input)?;
274-
let mut list_as_vec = vec![];
257+
let mut list_as_vec = Vec::new();
275258
let mut rest_input = list_inner_input;
276259
loop {
277-
let right_paren = rparenp(rest_input);
278-
match right_paren {
279-
Ok((after_list_input, _)) => {
280-
break Ok((after_list_input, list_as_vec.into_list().to_value()));
281-
}
282-
_ => {
283-
let next_form_parse = try_read(rest_input);
284-
match next_form_parse {
285-
Ok((_rest_input, form)) => {
286-
list_as_vec.push(form.to_rc_value());
287-
rest_input = _rest_input;
288-
}
289-
// This parse failed, forward failure
290-
_ => {
291-
break next_form_parse;
292-
}
293-
}
294-
}
260+
if let Ok((after_list_input, _)) = rparenp(rest_input) {
261+
return Ok((after_list_input, list_as_vec.into_list().to_value()));
295262
}
263+
let (_rest_input, form) = try_read(rest_input)?;
264+
list_as_vec.push(form.to_rc_value());
265+
rest_input = _rest_input;
296266
}
297267
}
298268

0 commit comments

Comments
 (0)