From 1f05e98965cb4c09dcc3098fdda3a97b11d5b07e Mon Sep 17 00:00:00 2001 From: Patrick Meredith Date: Tue, 3 Sep 2019 22:21:37 -0400 Subject: [PATCH] Refactor to make save.rs readable --- src/commands/save.rs | 144 +++++++++++++++++++++++-------------------- 1 file changed, 78 insertions(+), 66 deletions(-) diff --git a/src/commands/save.rs b/src/commands/save.rs index cdf41df6e7..e998329c8f 100644 --- a/src/commands/save.rs +++ b/src/commands/save.rs @@ -6,6 +6,80 @@ use std::path::{Path, PathBuf}; pub struct Save; +macro_rules! process_string { + ($input:ident, $name_span:ident) => {{ + let mut result_string = String::new(); + for res in $input { + match res { + Tagged { + item: Value::Primitive(Primitive::String(s)), + .. + } => { + result_string.push_str(&s); + } + _ => { + yield core::task::Poll::Ready(Err(ShellError::labeled_error( + "Save could not successfully save", + "unexpected data during save", + $name_span, + ))); + } + } + } + Ok(result_string.into_bytes()) + }}; +} + +macro_rules! process_string_return_success { + ($result_vec:ident, $name_span:ident) => {{ + let mut result_string = String::new(); + for res in $result_vec { + match res { + Ok(ReturnSuccess::Value(Tagged { + item: Value::Primitive(Primitive::String(s)), + .. + })) => { + result_string.push_str(&s); + } + _ => { + yield core::task::Poll::Ready(Err(ShellError::labeled_error( + "Save could not successfully save", + "unexpected data during text save", + $name_span, + ))); + } + } + } + Ok(result_string.into_bytes()) + }}; +} + +macro_rules! process_binary_return_success { + ($result_vec:ident, $name_span:ident) => {{ + let mut result_binary: Vec = Vec::new(); + for res in $result_vec { + match res { + Ok(ReturnSuccess::Value(Tagged { + item: Value::Binary(b), + .. + })) => { + for u in b.into_iter() { + result_binary.push(u); + } + } + _ => { + yield core::task::Poll::Ready(Err(ShellError::labeled_error( + "Save could not successfully save", + "unexpected data during binary save", + $name_span, + ))); + } + } + } + Ok(result_binary) + }}; +} + #[derive(Deserialize)] pub struct SaveArgs { path: Option>, @@ -117,77 +191,15 @@ fn save( let mut result = converter.run(new_args.with_input(input), ®istry); let result_vec: Vec> = result.drain_vec().await; if converter.is_binary() { - let mut result_binary : Vec = Vec::new(); - for res in result_vec { - match res { - Ok(ReturnSuccess::Value(Tagged { item: Value::Binary(b), .. })) => { - for u in b.into_iter() { - result_binary.push(u); - } - } - _ => { - yield Err(ShellError::labeled_error( - "Save could not successfully save", - "unexpected data during binary save", - name_span, - )); - }, - } - } - Ok(result_binary) + process_binary_return_success!(result_vec, name_span) } else { - let mut result_string = String::new(); - for res in result_vec { - match res { - Ok(ReturnSuccess::Value(Tagged { item: Value::Primitive(Primitive::String(s)), .. })) => { - result_string.push_str(&s); - } - _ => { - yield Err(ShellError::labeled_error( - "Save could not successfully save", - "unexpected data during text save", - name_span, - )); - }, - } - } - Ok(result_string.into_bytes()) + process_string_return_success!(result_vec, name_span) } } else { - let mut result_string = String::new(); - for res in input { - match res { - Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { - result_string.push_str(&s); - } - _ => { - yield Err(ShellError::labeled_error( - "Save could not successfully save", - "unexpected data during save", - name_span, - )); - }, - } - } - Ok(result_string.into_bytes()) + process_string!(input, name_span) } } else { - let mut result_string = String::new(); - for res in input { - match res { - Tagged { item: Value::Primitive(Primitive::String(s)), .. } => { - result_string.push_str(&s); - } - _ => { - yield Err(ShellError::labeled_error( - "Save could not successfully save", - "unexpected data during save", - name_span, - )); - }, - } - } - Ok(result_string.into_bytes()) + process_string!(input, name_span) } } else { Ok(string_from(&input).into_bytes())