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

feat: Re-organize API #26

Merged
merged 1 commit into from
Apr 16, 2018
Merged
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
File renamed without changes.
File renamed without changes.
8 changes: 4 additions & 4 deletions src/predicate/constant.rs → src/constant.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,9 +34,9 @@ impl<T> Predicate for BooleanPredicate<T> {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = always();
/// let predicate_fn = predicate::always();
/// assert_eq!(true, predicate_fn.eval(&5));
/// assert_eq!(true, predicate_fn.eval(&10));
/// assert_eq!(true, predicate_fn.eval(&15));
Expand All @@ -55,9 +55,9 @@ pub fn always<T>() -> BooleanPredicate<T> {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = never();
/// let predicate_fn = predicate::never();
/// assert_eq!(false, predicate_fn.eval(&5));
/// assert_eq!(false, predicate_fn.eval(&10));
/// assert_eq!(false, predicate_fn.eval(&15));
Expand Down
58 changes: 17 additions & 41 deletions src/predicate/mod.rs → src/core.rs
Original file line number Diff line number Diff line change
@@ -1,37 +1,13 @@
// Copyright (c) 2017, Nick Stevens <nick@bitcurry.com>
// Copyright (c) 2018 The predicates-rs Project Developers.
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/license/LICENSE-2.0> or the MIT license
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.

//! Predicate
//!
//! This module contains the `Predicate` trait and a number of combinators for
//! the trait. See the crate docs, and the docs for `Predicate`, for full detail.

// primitive `Predicate` types
mod constant;
mod function;
mod ord;
mod set;
pub use self::constant::{always, never, BooleanPredicate};
pub use self::function::{function, FnPredicate};
pub use self::ord::{eq, ge, gt, le, lt, ne, EqPredicate, OrdPredicate};
pub use self::set::{contains, contains_hashable, contains_ord, ContainsPredicate,
HashableContainsPredicate, OrdContainsPredicate};

// specialized primitive `Predicate` types
pub mod str;
pub mod path;
pub mod float;

// combinators
mod boolean;
mod boxed;
pub use self::boolean::{AndPredicate, NotPredicate, OrPredicate};
pub use self::boxed::BoxPredicate;
use boolean::{AndPredicate, NotPredicate, OrPredicate};
use boxed::BoxPredicate;

/// Trait for generically evaluating a type against a dynamically created
/// predicate function.
Expand All @@ -53,10 +29,10 @@ pub trait Predicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn1 = always().and(always());
/// let predicate_fn2 = always().and(never());
/// let predicate_fn1 = predicate::always().and(predicate::always());
/// let predicate_fn2 = predicate::always().and(predicate::never());
/// assert_eq!(true, predicate_fn1.eval(&4));
/// assert_eq!(false, predicate_fn2.eval(&4));
fn and<B>(self, other: B) -> AndPredicate<Self, B>
Expand All @@ -72,11 +48,11 @@ pub trait Predicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn1 = always().or(always());
/// let predicate_fn2 = always().or(never());
/// let predicate_fn3 = never().or(never());
/// let predicate_fn1 = predicate::always().or(predicate::always());
/// let predicate_fn2 = predicate::always().or(predicate::never());
/// let predicate_fn3 = predicate::never().or(predicate::never());
/// assert_eq!(true, predicate_fn1.eval(&4));
/// assert_eq!(true, predicate_fn2.eval(&4));
/// assert_eq!(false, predicate_fn3.eval(&4));
Expand All @@ -93,10 +69,10 @@ pub trait Predicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn1 = always().not();
/// let predicate_fn2 = never().not();
/// let predicate_fn1 = predicate::always().not();
/// let predicate_fn2 = predicate::never().not();
/// assert_eq!(false, predicate_fn1.eval(&4));
/// assert_eq!(true, predicate_fn2.eval(&4));
fn not(self) -> NotPredicate<Self>
Expand All @@ -121,11 +97,11 @@ pub trait Predicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicates = vec![
/// always().boxed(),
/// never().boxed(),
/// predicate::always().boxed(),
/// predicate::never().boxed(),
/// ];
/// assert_eq!(true, predicates[0].eval(&4));
/// assert_eq!(false, predicates[1].eval(&4));
Expand Down
16 changes: 8 additions & 8 deletions src/predicate/float/close.rs → src/float/close.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ impl IsClosePredicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let a = 0.15_f64 + 0.15_f64 + 0.15_f64;
/// let predicate_fn = float::is_close(a).distance(5);
/// let predicate_fn = predicate::float::is_close(a).distance(5);
/// ```
pub fn distance(mut self, distance: <f64 as Ulps>::U) -> Self {
self.epsilon = (distance as f64) * ::std::f64::EPSILON;
Expand All @@ -50,10 +50,10 @@ impl IsClosePredicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let a = 0.15_f64 + 0.15_f64 + 0.15_f64;
/// let predicate_fn = float::is_close(a).epsilon(5.0 * ::std::f64::EPSILON);
/// let predicate_fn = predicate::float::is_close(a).epsilon(5.0 * ::std::f64::EPSILON);
/// ```
pub fn epsilon(mut self, epsilon: f64) -> Self {
self.epsilon = epsilon;
Expand All @@ -67,10 +67,10 @@ impl IsClosePredicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let a = 0.15_f64 + 0.15_f64 + 0.15_f64;
/// let predicate_fn = float::is_close(a).ulps(5);
/// let predicate_fn = predicate::float::is_close(a).ulps(5);
/// ```
pub fn ulps(mut self, ulps: <f64 as Ulps>::U) -> Self {
self.ulps = ulps;
Expand All @@ -92,11 +92,11 @@ impl Predicate for IsClosePredicate {
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let a = 0.15_f64 + 0.15_f64 + 0.15_f64;
/// let b = 0.1_f64 + 0.1_f64 + 0.25_f64;
/// let predicate_fn = float::is_close(a);
/// let predicate_fn = predicate::float::is_close(a);
/// assert_eq!(true, predicate_fn.eval(&b));
/// assert_eq!(false, predicate_fn.distance(0).eval(&b));
/// ```
Expand Down
File renamed without changes.
2 changes: 1 addition & 1 deletion src/predicate/function.rs → src/function.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::{self, Predicate};
/// use predicates::prelude::*;
///
/// struct Example {
/// string: String,
Expand Down
25 changes: 21 additions & 4 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
//! A few different examples of how predicates might be used:
//!
//! ```
//! use predicates::{predicate, Predicate};
//! use predicates::prelude::*;
//!
//! // The simplest predicates are `always()` and `never()`, which always returns
//! // `true` and always returns `false`, respectively. The values are simply
Expand Down Expand Up @@ -92,6 +92,23 @@ extern crate float_cmp;
#[cfg(feature = "regex")]
extern crate regex;

// core `Predicate` trait
pub mod predicate;
pub use self::predicate::{BoxPredicate, Predicate};
pub mod prelude;

mod core;
pub use core::Predicate;
mod boxed;
pub use boxed::BoxPredicate;

// core predicates
pub mod constant;
pub mod function;
pub mod ord;
pub mod set;

// combinators
pub mod boolean;

// specialized primitive `Predicate` types
pub mod str;
pub mod path;
pub mod float;
24 changes: 12 additions & 12 deletions src/predicate/ord.rs → src/ord.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = eq(5);
/// let predicate_fn = predicate::eq(5);
/// assert_eq!(true, predicate_fn.eval(&5));
/// assert_eq!(false, predicate_fn.eval(&10));
/// ```
Expand All @@ -68,9 +68,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = ne(5);
/// let predicate_fn = predicate::ne(5);
/// assert_eq!(false, predicate_fn.eval(&5));
/// assert_eq!(true, predicate_fn.eval(&10));
/// ```
Expand Down Expand Up @@ -124,9 +124,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = lt(5);
/// let predicate_fn = predicate::lt(5);
/// assert_eq!(true, predicate_fn.eval(&4));
/// assert_eq!(false, predicate_fn.eval(&6));
/// ```
Expand All @@ -146,9 +146,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = le(5);
/// let predicate_fn = predicate::le(5);
/// assert_eq!(true, predicate_fn.eval(&4));
/// assert_eq!(true, predicate_fn.eval(&5));
/// assert_eq!(false, predicate_fn.eval(&6));
Expand All @@ -169,9 +169,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate = ge(5);
/// let predicate = predicate::ge(5);
/// assert_eq!(false, predicate.eval(&4));
/// assert_eq!(true, predicate.eval(&5));
/// assert_eq!(true, predicate.eval(&6));
Expand All @@ -192,9 +192,9 @@ where
/// # Examples
///
/// ```
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = gt(5);
/// let predicate_fn = predicate::gt(5);
/// assert_eq!(false, predicate_fn.eval(&4));
/// assert_eq!(false, predicate_fn.eval(&5));
/// assert_eq!(true, predicate_fn.eval(&6));
Expand Down
8 changes: 4 additions & 4 deletions src/predicate/path/existence.rs → src/path/existence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,9 @@ impl Predicate for ExistencePredicate {
///
/// ```
/// use std::path::Path;
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = path::exists();
/// let predicate_fn = predicate::path::exists();
/// assert_eq!(true, predicate_fn.eval(Path::new("Cargo.toml")));
/// ```
pub fn exists() -> ExistencePredicate {
Expand All @@ -47,9 +47,9 @@ pub fn exists() -> ExistencePredicate {
///
/// ```
/// use std::path::Path;
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = path::missing();
/// let predicate_fn = predicate::path::missing();
/// assert_eq!(true, predicate_fn.eval(Path::new("non-existent-file.foo")));
/// ```
pub fn missing() -> ExistencePredicate {
Expand Down
12 changes: 6 additions & 6 deletions src/predicate/path/ft.rs → src/path/ft.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,9 +70,9 @@ impl Predicate for FileTypePredicate {
///
/// ```
/// use std::path::Path;
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = path::is_file();
/// let predicate_fn = predicate::path::is_file();
/// assert_eq!(true, predicate_fn.eval(Path::new("Cargo.toml")));
/// assert_eq!(false, predicate_fn.eval(Path::new("src")));
/// assert_eq!(false, predicate_fn.eval(Path::new("non-existent-file.foo")));
Expand All @@ -90,9 +90,9 @@ pub fn is_file() -> FileTypePredicate {
///
/// ```
/// use std::path::Path;
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = path::is_dir();
/// let predicate_fn = predicate::path::is_dir();
/// assert_eq!(false, predicate_fn.eval(Path::new("Cargo.toml")));
/// assert_eq!(true, predicate_fn.eval(Path::new("src")));
/// assert_eq!(false, predicate_fn.eval(Path::new("non-existent-file.foo")));
Expand All @@ -110,9 +110,9 @@ pub fn is_dir() -> FileTypePredicate {
///
/// ```
/// use std::path::Path;
/// use predicates::predicate::*;
/// use predicates::prelude::*;
///
/// let predicate_fn = path::is_symlink();
/// let predicate_fn = predicate::path::is_symlink();
/// assert_eq!(false, predicate_fn.eval(Path::new("Cargo.toml")));
/// assert_eq!(false, predicate_fn.eval(Path::new("src")));
/// assert_eq!(false, predicate_fn.eval(Path::new("non-existent-file.foo")));
Expand Down
File renamed without changes.
Loading