|
| 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 | +} |
0 commit comments