Skip to content

Commit 4284e9f

Browse files
committed
feat(path): Existence predicate
Part of assert-rs#8.
1 parent bc04dec commit 4284e9f

File tree

3 files changed

+72
-0
lines changed

3 files changed

+72
-0
lines changed

src/predicate/mod.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ pub use self::set::{contains, contains_hashable, contains_ord, ContainsPredicate
2424

2525
// specialized primitive `Predicate` types
2626
pub mod str;
27+
pub mod path;
2728

2829
// combinators
2930
mod boolean;

src/predicate/path/existence.rs

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
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/licenses/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+
use std::path;
10+
11+
use Predicate;
12+
13+
/// Predicate that checks if a file is present
14+
///
15+
/// This is created by the `predicate::path::exists` and `predicate::path::missing`.
16+
#[derive(Debug)]
17+
pub struct ExistencePredicate {
18+
exists: bool,
19+
}
20+
21+
impl Predicate for ExistencePredicate {
22+
type Item = path::Path;
23+
24+
fn eval(&self, path: &path::Path) -> bool {
25+
path.exists() == self.exists
26+
}
27+
}
28+
29+
/// Creates a new `Predicate` that ensures the path exists.
30+
///
31+
/// # Examples
32+
///
33+
/// ```
34+
/// use std::path::Path;
35+
/// use predicates::predicate::*;
36+
///
37+
/// let predicate_fn = path::exists();
38+
/// assert_eq!(true, predicate_fn.eval(Path::new("Cargo.toml")));
39+
/// ```
40+
pub fn exists() -> ExistencePredicate {
41+
ExistencePredicate { exists: true }
42+
}
43+
44+
/// Creates a new `Predicate` that ensures the path doesn't exist.
45+
///
46+
/// # Examples
47+
///
48+
/// ```
49+
/// use std::path::Path;
50+
/// use predicates::predicate::*;
51+
///
52+
/// let predicate_fn = path::missing();
53+
/// assert_eq!(true, predicate_fn.eval(Path::new("non-existent-file.foo")));
54+
/// ```
55+
pub fn missing() -> ExistencePredicate {
56+
ExistencePredicate { exists: false }
57+
}

src/predicate/path/mod.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
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/licenses/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+
//! Path Predicates
10+
//!
11+
//! This module contains predicates specifiuc to the file system.
12+
13+
mod existence;
14+
pub use self::existence::{exists, missing, ExistencePredicate};

0 commit comments

Comments
 (0)