From ced479c46617eafedbce6211c39c9a3acab0caeb Mon Sep 17 00:00:00 2001 From: Emily Toop Date: Wed, 31 May 2017 15:18:17 +0100 Subject: [PATCH] Use EDN pretty printer to output QueryResults --- query-projector/src/lib.rs | 25 ++++++++++++++++++- tools/cli/src/mentat_cli/repl.rs | 43 +++----------------------------- 2 files changed, 27 insertions(+), 41 deletions(-) diff --git a/query-projector/src/lib.rs b/query-projector/src/lib.rs index 358667e5..84df5b06 100644 --- a/query-projector/src/lib.rs +++ b/query-projector/src/lib.rs @@ -116,6 +116,29 @@ impl QueryResults { &FindRel(_) => Box::new(|| QueryResults::Rel(vec![])), } } + + pub fn to_pretty(&self) -> Result { + use QueryResults::*; + match self { + &Scalar(Some(ref val)) => { + Ok(val.value_type().to_edn_value().to_pretty(120).unwrap_or(String::new()) ) + }, + &Tuple(Some(ref vals)) => { + Ok(vals.iter().map(|val| format!("{}\t", val.value_type().to_edn_value().to_pretty(120).unwrap_or(String::new()))).collect::>().into_iter().collect::()) + }, + &Coll(ref vals) => { + Ok(vals.iter().map(|val| format!("{}\n", val.value_type().to_edn_value().to_pretty(120).unwrap_or(String::new()))).collect::>().into_iter().collect::()) + }, + &Rel(ref valsvec) => { + let mut output = String::new(); + for vals in valsvec { + output.push_str(&format!("{}\n", &vals.iter().map(|val| format!("{}\t", val.value_type().to_edn_value().to_pretty(120).unwrap_or(String::new()))).collect::>().into_iter().collect::())); + } + Ok(output) + }, + _ => Ok("No results found.".to_string()) + } + } } type Index = i32; // See rusqlite::RowIndex. @@ -496,4 +519,4 @@ pub fn query_projection(query: &AlgebraicQuery) -> CombinedProjection { }, } } -} \ No newline at end of file +} diff --git a/tools/cli/src/mentat_cli/repl.rs b/tools/cli/src/mentat_cli/repl.rs index 6f712b56..6b56834c 100644 --- a/tools/cli/src/mentat_cli/repl.rs +++ b/tools/cli/src/mentat_cli/repl.rs @@ -156,35 +156,11 @@ impl Repl { }; if results.is_empty() { - println!("No results found.") + return println!("No results found.") } - - let mut output:String = String::new(); - match results { - QueryResults::Scalar(Some(val)) => { - output.push_str(&self.typed_value_as_string(val) ); - }, - QueryResults::Tuple(Some(vals)) => { - for val in vals { - output.push_str(&format!("{}\t", self.typed_value_as_string(val))); - } - }, - QueryResults::Coll(vv) => { - for val in vv { - output.push_str(&format!("{}\n", self.typed_value_as_string(val))); - } - }, - QueryResults::Rel(vvv) => { - for vv in vvv { - for v in vv { - output.push_str(&format!("{}\t", self.typed_value_as_string(v))); - } - output.push_str("\n"); - } - }, - _ => output.push_str(&format!("No results found.")) + if let Ok(output) = results.to_pretty() { + println!("\n{}", output); } - println!("\n{}", output); } fn execute_transact(&mut self, transaction: String) { @@ -194,19 +170,6 @@ impl Repl { } } - fn typed_value_as_string(&self, value: TypedValue) -> String { - match value { - TypedValue::Boolean(b) => if b { "true".to_string() } else { "false".to_string() }, - TypedValue::Double(d) => format!("{}", d), - TypedValue::Instant(i) => format!("{}", i), - TypedValue::Keyword(k) => format!("{}", k), - TypedValue::Long(l) => format!("{}", l), - TypedValue::Ref(r) => format!("{}", r), - TypedValue::String(s) => format!("{:?}", s.to_string()), - TypedValue::Uuid(u) => format!("{}", u), - } - } - fn read_files(&mut self, files: Vec) { for file in files { let res = self.read_file(file);