-
Notifications
You must be signed in to change notification settings - Fork 29
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
Changes from 2 commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
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)); | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -8,15 +8,60 @@ | |
|
||
use std::fmt; | ||
|
||
struct TreeWriter<'a, Item: ?Sized + fmt::Debug + 'a> { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dead code? Speaking of, should we have a |
||
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>>) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 |
||
unimplemented!() | ||
} | ||
|
||
/// TODO | ||
#[cfg(feature = "term-table")] | ||
fn tree_eval(&self, variable: &Item) -> String { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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() | ||
} | ||
} |
There was a problem hiding this comment.
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.