Skip to content

Commit

Permalink
fix(tree): Ensure alternate is passed down
Browse files Browse the repository at this point in the history
  • Loading branch information
epage committed Mar 14, 2023
1 parent 2cfb8f7 commit 24b04a4
Showing 1 changed file with 31 additions and 4 deletions.
35 changes: 31 additions & 4 deletions crates/tree/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,26 +24,29 @@ impl<'a> CaseTreeExt for reflection::Case<'a> {
}
}

type CaseTreeInner = termtree::Tree<Box<dyn fmt::Display + Send + Sync>>;
type CaseTreeInner = termtree::Tree<Displayable>;

fn convert(case: &reflection::Case<'_>) -> CaseTreeInner {
let mut leaves: Vec<CaseTreeInner> = vec![];

leaves.extend(case.predicate().iter().flat_map(|pred| {
pred.parameters().map(|item| {
let root: Box<dyn fmt::Display + Send + Sync> = Box::new(item.to_string());
let root = Displayable::new(&item);
termtree::Tree::new(root).with_multiline(true)
})
}));

leaves.extend(case.products().map(|item| {
let root: Box<dyn fmt::Display + Send + Sync> = Box::new(item.to_string());
let root = Displayable::new(item);
termtree::Tree::new(root).with_multiline(true)
}));

leaves.extend(case.children().map(convert));

let root = Box::new(case.predicate().map(|p| p.to_string()).unwrap_or_default());
let root = case
.predicate()
.map(|p| Displayable::new(&p))
.unwrap_or_default();
CaseTreeInner::new(root).with_leaves(leaves)
}

Expand All @@ -56,3 +59,27 @@ impl fmt::Display for CaseTree {
self.0.fmt(f)
}
}

#[derive(Default)]
struct Displayable {
primary: String,
alternate: String,
}

impl Displayable {
fn new(display: &dyn std::fmt::Display) -> Self {
let primary = format!("{}", display);
let alternate = format!("{:#}", display);
Self { primary, alternate }
}
}

impl fmt::Display for Displayable {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
if f.alternate() {
self.alternate.fmt(f)
} else {
self.primary.fmt(f)
}
}
}

0 comments on commit 24b04a4

Please sign in to comment.