Skip to content

WIP: Pretty error printer for predicates. #39

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

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all 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 }
treeline = { version = "0.1", optional = true }

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

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

let (result, output) = predicate_fn.tree_eval(&5);
println!("{}", output);
}
66 changes: 52 additions & 14 deletions src/boolean.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@
use std::marker::PhantomData;
use std::fmt;

#[cfg(feature = "treeline")]
use treeline::Tree;

use Predicate;

/// Predicate that combines two `Predicate`s, returning the AND of the results.
Expand All @@ -21,7 +24,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 +35,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 +51,37 @@ 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)
}

#[cfg(feature = "treeline")]
fn make_tree(&self, item: &Item) -> Tree<String> {
Tree::new(
format!(
"{} {}",
self.stringify(item),
::core::pass_fail(self.eval(item))
),
vec![
self.a.make_tree(item),
self.b.make_tree(item),
]
)
}

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

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 +96,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 +107,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 +123,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 +134,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 +148,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 +157,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,25 +171,41 @@ 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)
}

#[cfg(feature = "treeline")]
fn make_tree(&self, item: &Item) -> Tree<String> {
Tree::new(
format!(
"{} {}",
self.stringify(item),
::core::pass_fail(self.eval(item))
),
vec![self.inner.make_tree(item)]
)
}

fn stringify(&self, item: &Item) -> String {
format!("!({})", self.inner.stringify(item))
}
}

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 +271,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
30 changes: 29 additions & 1 deletion src/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,43 @@

use std::fmt;

#[cfg(feature = "treeline")]
use treeline::Tree;

pub(crate) fn pass_fail(b: bool) -> &'static str {
if b {
"PASSED"
} else {
"FAILED"
}
}

/// 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 stringify(&self, _item: &Item) -> String {
unimplemented!()
}

/// TODO
#[cfg(feature = "treeline")]
fn make_tree(&self, _item: &Item) -> Tree<String> {
unimplemented!()
}

/// TODO
#[cfg(feature = "treeline")]
fn tree_eval(&self, item: &Item) -> (bool, Tree<String>) {
(self.eval(item), self.make_tree(item))
}
}
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 = "treeline")]
extern crate treeline;

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