1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
use std::fmt;
use predicates_core::reflection;
pub trait CaseTreeExt {
fn tree(&self) -> CaseTree;
}
impl<'a> CaseTreeExt for reflection::Case<'a> {
fn tree(&self) -> CaseTree {
CaseTree(convert(self))
}
}
type CaseTreeInner = termtree::Tree<Box<dyn fmt::Display + Send + Sync>>;
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());
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());
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());
CaseTreeInner::new(root).with_leaves(leaves)
}
#[allow(missing_debug_implementations)]
pub struct CaseTree(CaseTreeInner);
impl fmt::Display for CaseTree {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
}
}