From 4b430532f7cd660bd813863871ede6f108e7be67 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Thu, 19 Jul 2018 19:53:33 -0600 Subject: [PATCH] feat(path): support file-based str predicates Fixes #56 --- src/path/fs.rs | 26 ++++++++++++++++++++------ 1 file changed, 20 insertions(+), 6 deletions(-) diff --git a/src/path/fs.rs b/src/path/fs.rs index 2edd403..8ae2160 100644 --- a/src/path/fs.rs +++ b/src/path/fs.rs @@ -44,6 +44,8 @@ impl BinaryFilePredicate { /// assert_eq!(true, predicate_file.eval(Path::new("Cargo.toml"))); /// assert_eq!(false, predicate_file.eval(Path::new("Cargo.lock"))); /// assert_eq!(false, predicate_file.eval(Path::new("src"))); + /// + /// assert_eq!(false, predicate_file.eval("Not a real Cargo.toml file content")); /// ``` pub fn utf8(self) -> Option { let path = self.path; @@ -58,6 +60,12 @@ impl Predicate for BinaryFilePredicate { } } +impl Predicate<[u8]> for BinaryFilePredicate { + fn eval(&self, actual: &[u8]) -> bool { + self.content == actual + } +} + impl fmt::Display for BinaryFilePredicate { fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { write!(f, "var is {}", self.path.display()) @@ -100,14 +108,20 @@ impl StrFilePredicate { } } -impl fmt::Display for StrFilePredicate { - fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { - write!(f, "var is {}", self.path.display()) - } -} - impl Predicate for StrFilePredicate { fn eval(&self, path: &path::Path) -> bool { self.eval(path).unwrap_or(false) } } + +impl Predicate for StrFilePredicate { + fn eval(&self, actual: &str) -> bool { + self.content == actual + } +} + +impl fmt::Display for StrFilePredicate { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + write!(f, "var is {}", self.path.display()) + } +}