|
| 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::fmt; |
| 10 | +use std::io; |
| 11 | +use std::path; |
| 12 | + |
| 13 | +use path::fc::FileContent; |
| 14 | +use Predicate; |
| 15 | + |
| 16 | +/// Predicate that compares file matches |
| 17 | +#[derive(Clone, Debug)] |
| 18 | +pub struct BinaryFilePredicate { |
| 19 | + path: path::PathBuf, |
| 20 | + file_content: FileContent, |
| 21 | +} |
| 22 | + |
| 23 | +impl BinaryFilePredicate { |
| 24 | + fn eval(&self, path: &path::Path) -> io::Result<bool> { |
| 25 | + let content = FileContent::new(path)?; |
| 26 | + Ok(self.file_content == content) |
| 27 | + } |
| 28 | +} |
| 29 | + |
| 30 | +impl Predicate<path::Path> for BinaryFilePredicate { |
| 31 | + fn eval(&self, path: &path::Path) -> bool { |
| 32 | + self.eval(path).unwrap_or(false) |
| 33 | + } |
| 34 | +} |
| 35 | + |
| 36 | +impl fmt::Display for BinaryFilePredicate { |
| 37 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 38 | + write!(f, "var is {}", self.path.display()) |
| 39 | + } |
| 40 | +} |
| 41 | + |
| 42 | +/// Creates a new `Predicate` that ensures complete equality |
| 43 | +/// |
| 44 | +/// # Examples |
| 45 | +/// |
| 46 | +/// ``` |
| 47 | +/// use std::path::Path; |
| 48 | +/// use predicates::prelude::*; |
| 49 | +/// |
| 50 | +/// let predicate_file = predicate::path::eq_file(Path::new("Cargo.toml")); |
| 51 | +/// assert_eq!(true, predicate_file.eval(Path::new("Cargo.toml"))); |
| 52 | +/// assert_eq!(false, predicate_file.eval(Path::new("src"))); |
| 53 | +/// assert_eq!(false, predicate_file.eval(Path::new("Cargo.lock"))); |
| 54 | +/// ``` |
| 55 | +pub fn eq_file(path: &path::Path) -> BinaryFilePredicate { |
| 56 | + let file_content = FileContent::new(path).unwrap(); |
| 57 | + BinaryFilePredicate { |
| 58 | + path: path.to_path_buf(), |
| 59 | + file_content, |
| 60 | + } |
| 61 | +} |
| 62 | + |
| 63 | +impl BinaryFilePredicate { |
| 64 | + /// Creates a new `Predicate` that ensures complete equality |
| 65 | + /// |
| 66 | + /// # Examples |
| 67 | + /// |
| 68 | + /// ``` |
| 69 | + /// use std::path::Path; |
| 70 | + /// use predicates::prelude::*; |
| 71 | + /// |
| 72 | + /// let predicate_file = predicate::path::eq_file(Path::new("Cargo.toml")).utf8(); |
| 73 | + /// assert_eq!(true, predicate_file.eval(Path::new("Cargo.toml"))); |
| 74 | + /// assert_eq!(false, predicate_file.eval(Path::new("Cargo.lock"))); |
| 75 | + /// assert_eq!(false, predicate_file.eval(Path::new("src"))); |
| 76 | + /// ``` |
| 77 | + pub fn utf8(self) -> StrFilePredicate { |
| 78 | + StrFilePredicate { |
| 79 | + path: self.path, |
| 80 | + content: self.file_content.utf8().unwrap(), |
| 81 | + } |
| 82 | + } |
| 83 | +} |
| 84 | + |
| 85 | +/// Predicate that compares string content of files |
| 86 | +#[derive(Debug)] |
| 87 | +pub struct StrFilePredicate { |
| 88 | + path: path::PathBuf, |
| 89 | + content: String, |
| 90 | +} |
| 91 | + |
| 92 | +impl StrFilePredicate { |
| 93 | + fn eval(&self, path: &path::Path) -> Option<bool> { |
| 94 | + let content = FileContent::new(path).ok()?.utf8().ok()?; |
| 95 | + Some(self.content == content) |
| 96 | + } |
| 97 | +} |
| 98 | + |
| 99 | +impl fmt::Display for StrFilePredicate { |
| 100 | + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 101 | + write!(f, "var is {}", self.path.display()) |
| 102 | + } |
| 103 | +} |
| 104 | + |
| 105 | +impl Predicate<path::Path> for StrFilePredicate { |
| 106 | + fn eval(&self, path: &path::Path) -> bool { |
| 107 | + self.eval(path).unwrap_or(false) |
| 108 | + } |
| 109 | +} |
0 commit comments