in src/results/table.rs [42:106]
fn build_table(elems: &[&OwnedElement]) -> Result<Table> {
let mut headers_set = HashSet::new();
let mut headers = vec![];
let mut single_value = false;
for elem in elems {
match elem.ion_type() {
IonType::Struct => {
let strukt = elem.as_struct().unwrap();
for (field, _) in strukt.iter() {
let heading = field.text().unwrap().to_string();
if headers_set.insert(heading.clone()) {
headers.push(heading);
}
}
}
_ => single_value = true,
};
}
// not really needed, but makes it clear that this set has done it's job.
drop(headers_set);
let mut final_headers = vec![];
// If we found a single value, push in a special header.
//
// Note that top-level QLDB documents can only be structs, so at the
// top-level, the only way to get values back is with `select value $field
// from $table`.
//
// However, this code theoretically can handle mixed values. There is one
// weird case where 1 result is a value while another has a field called
// VALUE. In that case, the column VALUE will appear twice.
if single_value {
final_headers.push("VALUE".to_string());
}
final_headers.extend(headers);
let mut table = Table::new();
table.load_preset(comfy_table::presets::ASCII_BORDERS_ONLY_CONDENSED);
table.set_header(final_headers.clone());
for elem in elems {
let row = match elem.ion_type() {
IonType::Struct => {
let strukt = elem.as_struct().unwrap();
let mut row = vec![];
for field in &final_headers {
row.push(format_element_for_cell(strukt.get(field))?);
}
row
}
_ => {
let mut row = vec!["".to_string(); final_headers.len()];
row[0] = format_element_for_cell(Some(elem))?;
row
}
};
table.add_row(row);
}
Ok(table)
}