1use std::fmt;
6use std::io;
7use std::path::Path;
8
9#[derive(Debug)]
11pub enum Refusal {
12 Link,
14 NotAFile,
16 OutsidePackage,
18 HandWritten,
20 Exists,
22 Unreadable(io::Error),
24}
25
26impl fmt::Display for Refusal {
27 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
28 match self {
29 Refusal::Link => f.write_str("it is a link, which cuno does not write through"),
30 Refusal::NotAFile => f.write_str("it is not a file"),
31 Refusal::OutsidePackage => f.write_str("it is not the package's"),
32 Refusal::HandWritten => f.write_str("cuno did not write it"),
33 Refusal::Exists => f.write_str("it is already there"),
34 Refusal::Unreadable(e) => write!(f, "{e}"),
35 }
36 }
37}
38
39impl Refusal {
40 pub fn which(&self) -> String {
43 match self {
44 Refusal::Link => "which is a link cuno does not write through".to_owned(),
45 Refusal::NotAFile => "which is not a file".to_owned(),
46 Refusal::OutsidePackage => "which is not the package's".to_owned(),
47 Refusal::HandWritten => "which cuno did not write".to_owned(),
48 Refusal::Exists => "which is already there".to_owned(),
49 Refusal::Unreadable(e) => format!("which cannot be read: {e}"),
50 }
51 }
52}
53
54fn kind(path: &Path) -> Result<Option<std::fs::FileType>, Refusal> {
56 match std::fs::symlink_metadata(path) {
57 Ok(meta) => Ok(Some(meta.file_type())),
58 Err(e) if e.kind() == io::ErrorKind::NotFound => Ok(None),
59 Err(e) => Err(Refusal::Unreadable(e)),
60 }
61}
62
63pub fn code(path: &Path, ours: &dyn Fn(&Path) -> bool) -> Result<(), Refusal> {
67 match kind(path)? {
68 Some(kind) if kind.is_symlink() => Err(Refusal::Link),
69 Some(kind) if !kind.is_file() => Err(Refusal::NotAFile),
70 _ if !ours(path) => Err(Refusal::OutsidePackage),
71 _ => Ok(()),
72 }
73}
74
75pub fn generated(path: &Path, marker: &str) -> Result<(), Refusal> {
80 match kind(path)? {
81 None => Ok(()),
82 Some(kind) if kind.is_symlink() => Err(Refusal::Link),
83 Some(kind) if !kind.is_file() => Err(Refusal::NotAFile),
84 Some(_) => {
85 let held = std::fs::read(path).map_err(Refusal::Unreadable)?;
86 let first = held.split(|b| *b == b'\n').next().unwrap_or_default();
87 if first.strip_suffix(b"\r").unwrap_or(first) == marker.as_bytes() {
88 Ok(())
89 } else {
90 Err(Refusal::HandWritten)
91 }
92 }
93 }
94}
95
96pub fn absent(path: &Path) -> Result<(), Refusal> {
101 match kind(path)? {
102 None => Ok(()),
103 Some(_) => Err(Refusal::Exists),
104 }
105}
106
107pub fn appendable(path: &Path) -> Result<(), Refusal> {
110 match kind(path)? {
111 Some(kind) if kind.is_symlink() => Err(Refusal::Link),
112 Some(kind) if !kind.is_file() => Err(Refusal::NotAFile),
113 _ => Ok(()),
114 }
115}
116
117#[cfg(test)]
118mod tests {
119 use super::*;
120
121 const MARKER: &str = "# Generated by cuno";
122
123 #[test]
129 fn a_new_file_only_where_nothing_is() {
130 let dir = tempfile::tempdir().unwrap();
131 let file = dir.path().join("a.txt");
132 assert!(absent(&file).is_ok());
133 std::fs::write(&file, "").unwrap();
134 assert!(matches!(absent(&file), Err(Refusal::Exists)));
135 assert!(matches!(absent(dir.path()), Err(Refusal::Exists)));
136 assert!(appendable(&file).is_ok());
137 assert!(matches!(appendable(dir.path()), Err(Refusal::NotAFile)));
138 }
139
140 #[test]
145 fn code_is_a_file_of_the_package() {
146 let dir = tempfile::tempdir().unwrap();
147 let file = dir.path().join("a.cpp");
148 std::fs::write(&file, "").unwrap();
149 assert!(code(&file, &|_| true).is_ok());
150 assert!(matches!(
151 code(&file, &|_| false),
152 Err(Refusal::OutsidePackage)
153 ));
154 assert!(matches!(
155 code(dir.path(), &|_| true),
156 Err(Refusal::NotAFile)
157 ));
158 }
159
160 #[test]
166 fn generated_is_absent_or_marked() {
167 let dir = tempfile::tempdir().unwrap();
168 let path = dir.path().join(".clang-format");
169 assert!(generated(&path, MARKER).is_ok());
170 std::fs::write(&path, format!("{MARKER}\r\nBasedOnStyle: LLVM\n")).unwrap();
171 assert!(generated(&path, MARKER).is_ok());
172 std::fs::write(&path, "BasedOnStyle: LLVM\n").unwrap();
173 assert!(matches!(
174 generated(&path, MARKER),
175 Err(Refusal::HandWritten)
176 ));
177 assert!(matches!(
178 generated(dir.path(), MARKER),
179 Err(Refusal::NotAFile)
180 ));
181 }
182
183 #[cfg(windows)]
189 #[test]
190 fn a_link_is_never_written() {
191 let dir = tempfile::tempdir().unwrap();
192 let real = dir.path().join("real");
193 std::fs::create_dir(&real).unwrap();
194 let link = dir.path().join("link");
195 let made = std::process::Command::new("cmd")
196 .args(["/C", "mklink", "/J"])
197 .arg(&link)
198 .arg(&real)
199 .output()
200 .unwrap();
201 assert!(made.status.success(), "{made:?}");
202 assert!(matches!(code(&link, &|_| true), Err(Refusal::Link)));
203 assert!(matches!(generated(&link, MARKER), Err(Refusal::Link)));
204 std::fs::remove_dir(&link).unwrap();
205 }
206}