Skip to content

Commit 188a847

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 22433f4 commit 188a847

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
@@ -22,7 +22,9 @@ difference = { version = "2.0", optional = true }
2222
normalize-line-endings = { version = "0.2.2", optional = true }
2323
regex = { version="1.0", optional = true }
2424
float-cmp = { version="0.4", optional = true }
25+
treeline = { version = "0.1", optional = true }
2526

2627
[features]
27-
default = ["difference", "regex", "float-cmp", "normalize-line-endings"]
28+
default = ["difference", "regex", "float-cmp", "normalize-line-endings", "tree"]
2829
unstable = []
30+
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
@@ -100,6 +100,8 @@ extern crate float_cmp;
100100
extern crate normalize_line_endings;
101101
#[cfg(feature = "regex")]
102102
extern crate regex;
103+
#[cfg(feature = "treeline")]
104+
extern crate treeline;
103105

104106
pub mod prelude;
105107

@@ -123,3 +125,5 @@ pub mod boolean;
123125
pub mod float;
124126
pub mod path;
125127
pub mod str;
128+
#[cfg(feature = "tree")]
129+
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)