Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

WIP: Pretty error printer for predicates. #39

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ appveyor = { repository = "assert-rs/predicates-rs" }
difference = { version = "2.0", optional = true }
regex = { version="1.0", optional = true }
float-cmp = { version="0.4", optional = true }
term-table = { version = "0.1", optional = true }

[features]
default = ["difference", "regex", "float-cmp"]
Expand Down
8 changes: 8 additions & 0 deletions examples/tree_eval.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
extern crate predicates;

fn main() {
use predicates::prelude::*;
let predicate_fn = predicate::ne(5).and(predicate::ge(5));

println!("{}", predicate_fn.tree_eval(&7));
}
38 changes: 24 additions & 14 deletions src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ pub struct AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for making this a dedicated commit

If we keep this, please note in the commit message that you are breaking the API.

Personally, I recommend following conventional changelog, it makes it easier for me to notice everything when deciding on the new version / writing the changelog.

{
a: M1,
b: M2,
Expand All @@ -32,7 +32,7 @@ impl<M1, M2, Item> AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
/// Create a new `AndPredicate` over predicates `a` and `b`.
pub fn new(a: M1, b: M2) -> AndPredicate<M1, M2, Item> {
Expand All @@ -48,18 +48,28 @@ impl<M1, M2, Item> Predicate<Item> for AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn eval(&self, item: &Item) -> bool {
self.a.eval(item) && self.b.eval(item)
}

fn flatten<'a, 'b>(&'a self, vec: &'b mut Vec<&'a Predicate<Item>>) {
vec.push(self);
self.a.flatten(vec);
self.b.flatten(vec);
}

fn stringify(&self, variable: &Item) -> String {
format!("{} && {}", self.a.stringify(variable), self.b.stringify(variable))
}
}

impl<M1, M2, Item> fmt::Display for AndPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({} && {})", self.a, self.b)
Expand All @@ -74,7 +84,7 @@ pub struct OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
a: M1,
b: M2,
Expand All @@ -85,7 +95,7 @@ impl<M1, M2, Item> OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
/// Create a new `OrPredicate` over predicates `a` and `b`.
pub fn new(a: M1, b: M2) -> OrPredicate<M1, M2, Item> {
Expand All @@ -101,7 +111,7 @@ impl<M1, M2, Item> Predicate<Item> for OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn eval(&self, item: &Item) -> bool {
self.a.eval(item) || self.b.eval(item)
Expand All @@ -112,7 +122,7 @@ impl<M1, M2, Item> fmt::Display for OrPredicate<M1, M2, Item>
where
M1: Predicate<Item>,
M2: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "({} || {})", self.a, self.b)
Expand All @@ -126,7 +136,7 @@ where
pub struct NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
inner: M,
_phantom: PhantomData<Item>,
Expand All @@ -135,7 +145,7 @@ where
impl<M, Item> NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
/// Create a new `NotPredicate` over predicate `inner`.
pub fn new(inner: M) -> NotPredicate<M, Item> {
Expand All @@ -149,7 +159,7 @@ where
impl<M, Item> Predicate<Item> for NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn eval(&self, item: &Item) -> bool {
!self.inner.eval(item)
Expand All @@ -159,15 +169,15 @@ where
impl<M, Item> fmt::Display for NotPredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "(! {})", self.inner)
}
}

/// `Predicate` extension that adds boolean logic.
pub trait PredicateBooleanExt<Item: ?Sized>
pub trait PredicateBooleanExt<Item: ?Sized + fmt::Debug>
where
Self: Predicate<Item>,
{
Expand Down Expand Up @@ -233,6 +243,6 @@ where
impl<P, Item> PredicateBooleanExt<Item> for P
where
P: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
}
13 changes: 7 additions & 6 deletions src/boxed.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,11 +15,11 @@ use Predicate;

/// `Predicate` that wraps another `Predicate` as a trait object, allowing
/// sized storage of predicate types.
pub struct BoxPredicate<Item: ?Sized>(Box<Predicate<Item> + Send + Sync>);
pub struct BoxPredicate<Item: ?Sized + fmt::Debug>(Box<Predicate<Item> + Send + Sync>);

impl<Item> BoxPredicate<Item>
where
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
/// Creates a new `BoxPredicate`, a wrapper around a dynamically-dispatched
/// `Predicate` type with useful trait impls.
Expand All @@ -33,7 +33,7 @@ where

impl<Item> fmt::Debug for BoxPredicate<Item>
where
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("BoxPredicate").finish()
Expand All @@ -42,7 +42,7 @@ where

impl<Item> fmt::Display for BoxPredicate<Item>
where
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.0)
Expand All @@ -51,15 +51,15 @@ where

impl<Item> Predicate<Item> for BoxPredicate<Item>
where
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn eval(&self, variable: &Item) -> bool {
self.0.eval(variable)
}
}

/// `Predicate` extension for boxing a `Predicate`.
pub trait PredicateBoxExt<Item: ?Sized>
pub trait PredicateBoxExt<Item: ?Sized + fmt::Debug>
where
Self: Predicate<Item>,
{
Expand Down Expand Up @@ -98,5 +98,6 @@ where
impl<P, Item> PredicateBoxExt<Item> for P
where
P: Predicate<Item>,
Item: ?Sized + fmt::Debug
{
}
2 changes: 1 addition & 1 deletion src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ pub struct BooleanPredicate<Item> {
_phantom: PhantomData<Item>,
}

impl<Item> Predicate<Item> for BooleanPredicate<Item> {
impl<Item: fmt::Debug> Predicate<Item> for BooleanPredicate<Item> {
fn eval(&self, _variable: &Item) -> bool {
self.retval
}
Expand Down
47 changes: 46 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,60 @@

use std::fmt;

struct TreeWriter<'a, Item: ?Sized + fmt::Debug + 'a> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code?

Speaking of, should we have a ExpressionWriter trait that flatten accepts as a reference to decouple the allocation policy from the Predicate trait?

items: Vec<&'a Predicate<Item>>,
}

/// Trait for generically evaluating a type against a dynamically created
/// predicate function.
///
/// The exact meaning of `eval` depends on the situation, but will usually
/// mean that the evaluated item is in some sort of pre-defined set. This is
/// different from `Ord` and `Eq` in that an `item` will almost never be the
/// same type as the implementing `Predicate` type.
pub trait Predicate<Item: ?Sized>: fmt::Display {
pub trait Predicate<Item: ?Sized + fmt::Debug>: fmt::Display {
/// Execute this `Predicate` against `variable`, returning the resulting
/// boolean.
fn eval(&self, variable: &Item) -> bool;

/// TODO
fn flatten<'a, 'b>(&'a self, _vec: &'b mut Vec<&'a Predicate<Item>>) {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what name should we use for this?

unimplemented!()
}

/// TODO
fn stringify(&self, _variable: &Item) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

So, what name should we use for this?

The difference between this and Display is that this includes and evaluation of var / actual.

unimplemented!()
}

/// TODO
#[cfg(feature = "term-table")]
fn tree_eval(&self, variable: &Item) -> String {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We need a way to programmatically know the result of the evaluation, so a caller like assert_cmd can decide whether to panic or not.

use term_table::{
Table,
cell::Cell,
row::Row,
};

let mut table = Table::new();
table.max_column_width = 80;
let mut vec = Vec::new();
self.flatten(&mut vec);

table.add_row(Row::new(vec![
Cell::new("PREDICATE", 1),
Cell::new("RESULT", 1),
]));

for item in vec {
let result = if item.eval(variable) {"PASSED"} else {"FAILED"};

table.add_row(Row::new(vec![
Cell::new(item.stringify(variable), 1),
Cell::new(result, 1),
]));
}

table.as_string()
}
}
7 changes: 4 additions & 3 deletions src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,11 +51,12 @@ where
}
}

impl<F, T> Predicate<T> for FnPredicate<F, T>
impl<F, Item> Predicate<Item> for FnPredicate<F, Item>
where
F: Fn(&T) -> bool,
F: Fn(&Item) -> bool,
Item: fmt::Debug
{
fn eval(&self, variable: &T) -> bool {
fn eval(&self, variable: &Item) -> bool {
(self.function)(variable)
}
}
Expand Down
2 changes: 2 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,8 @@ extern crate difference;
extern crate float_cmp;
#[cfg(feature = "regex")]
extern crate regex;
#[cfg(feature = "term-table")]
extern crate term_table;

pub mod prelude;

Expand Down
10 changes: 5 additions & 5 deletions src/name.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ use Predicate;
pub struct NamePredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
inner: M,
name: &'static str,
Expand All @@ -30,7 +30,7 @@ where
impl<M, Item> Predicate<Item> for NamePredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn eval(&self, item: &Item) -> bool {
self.inner.eval(item)
Expand All @@ -40,15 +40,15 @@ where
impl<M, Item> fmt::Display for NamePredicate<M, Item>
where
M: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "{}", self.name)
}
}

/// `Predicate` extension that adds naming predicate expressions.
pub trait PredicateNameExt<Item: ?Sized>
pub trait PredicateNameExt<Item: ?Sized + fmt::Debug>
where
Self: Predicate<Item>,
{
Expand Down Expand Up @@ -77,6 +77,6 @@ where
impl<P, Item> PredicateNameExt<Item> for P
where
P: Predicate<Item>,
Item: ?Sized,
Item: ?Sized + fmt::Debug,
{
}
Loading