1use std::fmt;
10use std::fs;
11use std::io::{self, Read};
12use std::path;
13
14use crate::reflection;
15use crate::utils;
16use crate::Predicate;
17
18fn read_file(path: &path::Path) -> io::Result<Vec<u8>> {
19 let mut buffer = Vec::new();
20 fs::File::open(path)?.read_to_end(&mut buffer)?;
21 Ok(buffer)
22}
23
24#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct BinaryFilePredicate {
27 path: path::PathBuf,
28 content: utils::DebugAdapter<Vec<u8>>,
29}
30
31impl BinaryFilePredicate {
32 fn eval(&self, path: &path::Path) -> io::Result<bool> {
33 let content = read_file(path)?;
34 Ok(self.content.debug == content)
35 }
36
37 pub fn utf8(self) -> Option<StrFilePredicate> {
53 let path = self.path;
54 let content = String::from_utf8(self.content.debug).ok()?;
55 Some(StrFilePredicate { path, content })
56 }
57}
58
59impl Predicate<path::Path> for BinaryFilePredicate {
60 fn eval(&self, path: &path::Path) -> bool {
61 self.eval(path).unwrap_or(false)
62 }
63
64 fn find_case<'a>(
65 &'a self,
66 expected: bool,
67 variable: &path::Path,
68 ) -> Option<reflection::Case<'a>> {
69 utils::default_find_case(self, expected, variable)
70 }
71}
72
73impl Predicate<[u8]> for BinaryFilePredicate {
74 fn eval(&self, actual: &[u8]) -> bool {
75 self.content.debug == actual
76 }
77
78 fn find_case<'a>(&'a self, expected: bool, variable: &[u8]) -> Option<reflection::Case<'a>> {
79 utils::default_find_case(self, expected, variable)
80 }
81}
82
83impl reflection::PredicateReflection for BinaryFilePredicate {
84 fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Parameter<'a>> + 'a> {
85 let params = vec![reflection::Parameter::new("content", &self.content)];
86 Box::new(params.into_iter())
87 }
88}
89
90impl fmt::Display for BinaryFilePredicate {
91 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
92 let palette = crate::Palette::new(f.alternate());
93 write!(
94 f,
95 "{} {} {}",
96 palette.var("var"),
97 palette.description("is"),
98 palette.expected(self.path.display())
99 )
100 }
101}
102
103pub fn eq_file<P: Into<path::PathBuf>>(path: P) -> BinaryFilePredicate {
116 let path = path.into();
117 let content = utils::DebugAdapter::new(read_file(&path).unwrap());
118 BinaryFilePredicate { path, content }
119}
120
121#[derive(Debug, Clone, PartialEq, Eq)]
123pub struct StrFilePredicate {
124 path: path::PathBuf,
125 content: String,
126}
127
128impl StrFilePredicate {
129 fn eval(&self, path: &path::Path) -> Option<bool> {
130 let content = read_file(path).ok()?;
131 let content = String::from_utf8(content).ok()?;
132 Some(self.content == content)
133 }
134}
135
136impl Predicate<path::Path> for StrFilePredicate {
137 fn eval(&self, path: &path::Path) -> bool {
138 self.eval(path).unwrap_or(false)
139 }
140
141 fn find_case<'a>(
142 &'a self,
143 expected: bool,
144 variable: &path::Path,
145 ) -> Option<reflection::Case<'a>> {
146 utils::default_find_case(self, expected, variable)
147 }
148}
149
150impl Predicate<str> for StrFilePredicate {
151 fn eval(&self, actual: &str) -> bool {
152 self.content == actual
153 }
154
155 fn find_case<'a>(&'a self, expected: bool, variable: &str) -> Option<reflection::Case<'a>> {
156 utils::default_find_case(self, expected, variable)
157 }
158}
159
160impl reflection::PredicateReflection for StrFilePredicate {
161 fn parameters<'a>(&'a self) -> Box<dyn Iterator<Item = reflection::Parameter<'a>> + 'a> {
162 let params = vec![reflection::Parameter::new("content", &self.content)];
163 Box::new(params.into_iter())
164 }
165}
166
167impl fmt::Display for StrFilePredicate {
168 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
169 let palette = crate::Palette::new(f.alternate());
170 write!(
171 f,
172 "{} {} {}",
173 palette.var("var"),
174 palette.description("is"),
175 palette.expected(self.path.display())
176 )
177 }
178}