Skip to content

Commit 34e7f37

Browse files
committed
feat: Tree view of Predicate result (Case)
Finnaly, Fixes #7. Inspired by the work in #39. Created softprops/treeline#3 for trying to find ways to make this more efficient.
1 parent 7d2afb9 commit 34e7f37

File tree

4 files changed

+82
-1
lines changed

4 files changed

+82
-1
lines changed

Cargo.toml

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,9 @@ appveyor = { repository = "assert-rs/predicates-rs" }
2121
difference = { version = "2.0", optional = true }
2222
regex = { version="1.0", optional = true }
2323
float-cmp = { version="0.4", optional = true }
24+
treeline = { version = "0.1", optional = true }
2425

2526
[features]
26-
default = ["difference", "regex", "float-cmp"]
27+
default = ["difference", "regex", "float-cmp", "tree"]
2728
unstable = []
29+
tree = ["treeline",]

examples/case_tree.rs

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
extern crate predicates;
2+
3+
use predicates::prelude::*;
4+
use predicates::tree::CaseTreeExt;
5+
6+
fn main() {
7+
let pred = predicate::ne(5).not().and(predicate::ge(5));
8+
9+
let var = 5;
10+
let case = pred.find_case(true, &var);
11+
if let Some(case) = case {
12+
println!("var is {}", var);
13+
println!("{}", case.tree());
14+
}
15+
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,8 @@ extern crate difference;
9898
extern crate float_cmp;
9999
#[cfg(feature = "regex")]
100100
extern crate regex;
101+
#[cfg(feature = "treeline")]
102+
extern crate treeline;
101103

102104
pub mod prelude;
103105

@@ -121,3 +123,5 @@ pub mod boolean;
121123
pub mod float;
122124
pub mod path;
123125
pub mod str;
126+
#[cfg(feature = "tree")]
127+
pub mod tree;

src/tree.rs

Lines changed: 60 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,60 @@
1+
// Copyright (c) 2018 The predicates-rs Project Developers.
2+
//
3+
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
4+
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
5+
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
6+
// option. This file may not be copied, modified, or distributed
7+
// except according to those terms.
8+
9+
//! Render `Case` as a tree.
10+
11+
use std::fmt;
12+
13+
use treeline;
14+
15+
use reflection;
16+
17+
/// Render `Self` as a displayable tree.
18+
pub trait CaseTreeExt {
19+
/// Render `Self` as a displayable tree.
20+
fn tree(&self) -> CaseTree;
21+
}
22+
23+
impl<'a> CaseTreeExt for reflection::Case<'a> {
24+
fn tree(&self) -> CaseTree {
25+
CaseTree(convert(self))
26+
}
27+
}
28+
29+
type CaseTreeInner = treeline::Tree<Box<fmt::Display>>;
30+
31+
fn convert<'a>(case: &reflection::Case<'a>) -> CaseTreeInner {
32+
let mut leaves: Vec<CaseTreeInner> = vec![];
33+
34+
leaves.extend(case.predicate().iter().flat_map(|pred| {
35+
pred.parameters().map(|item| {
36+
let root: Box<fmt::Display> = Box::new(item.to_string());
37+
treeline::Tree::new(root, vec![])
38+
})
39+
}));
40+
41+
leaves.extend(case.products().map(|item| {
42+
let root: Box<fmt::Display> = Box::new(item.to_string());
43+
treeline::Tree::new(root, vec![])
44+
}));
45+
46+
leaves.extend(case.children().map(|item| convert(item)));
47+
48+
let root = Box::new(case.predicate().map(|p| p.to_string()).unwrap_or_default());
49+
CaseTreeInner::new(root, leaves)
50+
}
51+
52+
/// A `Case` rendered as a tree for display.
53+
#[allow(missing_debug_implementations)]
54+
pub struct CaseTree(CaseTreeInner);
55+
56+
impl fmt::Display for CaseTree {
57+
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
58+
self.0.fmt(f)
59+
}
60+
}

0 commit comments

Comments
 (0)