1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
use std::fmt;
use std::fs;
use std::io::{self, Read};
use std::path;
use std::str;
use Predicate;
#[derive(Clone, Debug, PartialEq)]
struct FileContent(Vec<u8>);
impl FileContent {
pub fn new(path: &path::Path) -> io::Result<FileContent> {
let mut buffer = Vec::new();
fs::File::open(path)?.read_to_end(&mut buffer)?;
Ok(FileContent(buffer))
}
pub fn utf8(&self) -> Result<&str, str::Utf8Error> {
str::from_utf8(&self.0)
}
}
#[derive(Clone, Debug)]
pub struct BinaryFilePredicate {
path: path::PathBuf,
file_content: FileContent,
}
impl BinaryFilePredicate {
fn eval(&self, path: &path::Path) -> io::Result<bool> {
let content = FileContent::new(path)?;
Ok(self.file_content == content)
}
pub fn utf8(self) -> Option<StrFilePredicate> {
let path = self.path;
self.file_content.utf8().ok().map(|s| StrFilePredicate {
path: path,
content: s.to_string(),
})
}
}
impl Predicate<path::Path> for BinaryFilePredicate {
fn eval(&self, path: &path::Path) -> bool {
self.eval(path).unwrap_or(false)
}
}
impl fmt::Display for BinaryFilePredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var is {}", self.path.display())
}
}
pub fn eq_file(path: &path::Path) -> BinaryFilePredicate {
let file_content = FileContent::new(path).unwrap();
BinaryFilePredicate {
path: path.to_path_buf(),
file_content,
}
}
#[derive(Debug)]
pub struct StrFilePredicate {
path: path::PathBuf,
content: String,
}
impl StrFilePredicate {
fn eval(&self, path: &path::Path) -> Option<bool> {
let content = FileContent::new(path).ok()?;
let string = content.utf8().ok()?;
Some(self.content == string)
}
}
impl fmt::Display for StrFilePredicate {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
write!(f, "var is {}", self.path.display())
}
}
impl Predicate<path::Path> for StrFilePredicate {
fn eval(&self, path: &path::Path) -> bool {
self.eval(path).unwrap_or(false)
}
}