Use EDN pretty printer to output QueryResults

This commit is contained in:
Emily Toop 2017-05-31 15:18:17 +01:00
parent 3a8e3c7e78
commit ced479c466
2 changed files with 27 additions and 41 deletions

View file

@ -116,6 +116,29 @@ impl QueryResults {
&FindRel(_) => Box::new(|| QueryResults::Rel(vec![])),
}
}
pub fn to_pretty(&self) -> Result<String> {
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::<Vec<String>>().into_iter().collect::<String>())
},
&Coll(ref vals) => {
Ok(vals.iter().map(|val| format!("{}\n", val.value_type().to_edn_value().to_pretty(120).unwrap_or(String::new()))).collect::<Vec<String>>().into_iter().collect::<String>())
},
&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::<Vec<String>>().into_iter().collect::<String>()));
}
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 {
},
}
}
}
}

View file

@ -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<String>) {
for file in files {
let res = self.read_file(file);