| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | | use std::fs; |
| 14 | | use std::io::{self, Write}; |
| 15 | | use std::path::{Path, PathBuf}; |
| 16 | | use std::process::ExitCode; |
| 17 | |
|
| 18 | |
|
| 19 | | const MARKERS: [&str; 3] = [concat!("TO", "DO"), concat!("FIX", "ME"), concat!("X", "XX")]; |
| 20 | |
|
| 21 | |
|
| 22 | | const LISTED: usize = 20; |
| 23 | |
|
| 24 | | const USAGE: &str = "usage: flow-checks layout PATH... | todos DIR... | size DIR | summary"; |
| 25 | |
|
| 26 | 0× | fn main() -> ExitCode { |
| 27 | 0× | let args: Vec<String> = std::env::args().skip(1).collect(); |
| 28 | 0× | let root = Path::new("."); |
| 29 | 0× | let mut out = io::stdout().lock(); |
| 30 | 0× | let done = match args.split_first() { |
| 31 | 0× | Some((cmd, rest)) => match cmd.as_str() { |
| 32 | 0× | "layout" => layout(root, rest, &mut out), |
| 33 | 0× | "todos" => todos(root, rest, &mut out), |
| 34 | 0× | "size" => match rest { |
| 35 | 0× | [dir] => size(root, dir, &mut out), |
| 36 | 0× | _ => Err(USAGE.to_string()), |
| 37 | | }, |
| 38 | 0× | "summary" => summary(std::env::var("PROJECT").ok().as_deref(), &mut out), |
| 39 | 0× | _ => Err(USAGE.to_string()), |
| 40 | | }, |
| 41 | 0× | None => Err(USAGE.to_string()), |
| 42 | | }; |
| 43 | 0× | match done { |
| 44 | 0× | Ok(()) => ExitCode::SUCCESS, |
| 45 | 0× | Err(e) => { |
| 46 | 0× | eprintln!("{e}"); |
| 47 | 0× | ExitCode::FAILURE |
| 48 | | } |
| 49 | | } |
| 50 | 0× | } |
| 51 | |
|
| 52 | | type Done = Result<(), String>; |
| 53 | |
|
| 54 | 27× | fn emit(out: &mut impl Write, line: &str) -> Done { |
| 55 | 27× | writeln!(out, "{line}").map_err(|e| format!("write: {e}")) |
| 56 | 27× | } |
| 57 | |
|
| 58 | |
|
| 59 | 3× | fn layout(root: &Path, paths: &[String], out: &mut impl Write) -> Done { |
| 60 | 3× | if paths.is_empty() { |
| 61 | 0× | return Err(USAGE.to_string()); |
| 62 | 3× | } |
| 63 | 5× | for p in paths { |
| 64 | 5× | if !root.join(p).is_file() { |
| 65 | 2× | return Err(format!("missing: {p}")); |
| 66 | 3× | } |
| 67 | 3× | emit(out, &format!("found {p}"))?; |
| 68 | | } |
| 69 | 1× | Ok(()) |
| 70 | 3× | } |
| 71 | |
|
| 72 | |
|
| 73 | 1× | fn todos(root: &Path, dirs: &[String], out: &mut impl Write) -> Done { |
| 74 | 1× | if dirs.is_empty() { |
| 75 | 0× | return Err(USAGE.to_string()); |
| 76 | 1× | } |
| 77 | 1× | let mut noted = Vec::new(); |
| 78 | 1× | for d in dirs { |
| 79 | 3× | for file in files(&root.join(d))? { |
| 80 | | |
| 81 | 3× | let Ok(text) = fs::read_to_string(&file) else { continue }; |
| 82 | 2× | let name = shown(root, &file); |
| 83 | 27× | for (i, line) in text.lines().enumerate() { |
| 84 | 30× | if MARKERS.iter().any(|m| line.contains(m)) { |
| 85 | 26× | noted.push(format!("{name}:{}:{line}", i + 1)); |
| 86 | 26× | } |
| 87 | | } |
| 88 | | } |
| 89 | | } |
| 90 | 1× | emit(out, &format!("{} {} notes in {}", noted.len(), MARKERS.join("/"), shown_dirs(dirs)))?; |
| 91 | 20× | for line in noted.iter().take(LISTED) { |
| 92 | 20× | emit(out, line)?; |
| 93 | | } |
| 94 | 1× | Ok(()) |
| 95 | 1× | } |
| 96 | |
|
| 97 | |
|
| 98 | 1× | fn size(root: &Path, dir: &str, out: &mut impl Write) -> Done { |
| 99 | 3× | for krate in entries(&root.join(dir))?.into_iter().filter(|p| p.is_dir()) { |
| 100 | 2× | let mut lines = 0; |
| 101 | 4× | for file in files(&krate)?.into_iter().filter(|f| f.extension().is_some_and(|e| e == "rs")) { |
| 102 | 3× | let bytes = fs::read(&file).map_err(|e| format!("{}: {e}", file.display()))?; |
| 103 | 12× | lines += bytes.iter().filter(|&&b| b == b'\n').count(); |
| 104 | | } |
| 105 | 2× | emit(out, &format!("{:<24} {lines:>6} lines", format!("{}/", shown(root, &krate))))?; |
| 106 | | } |
| 107 | 1× | Ok(()) |
| 108 | 1× | } |
| 109 | |
|
| 110 | 1× | fn summary(project: Option<&str>, out: &mut impl Write) -> Done { |
| 111 | 1× | emit(out, &format!("{}: every check passed on this state.", project.unwrap_or("This project"))) |
| 112 | 1× | } |
| 113 | |
|
| 114 | |
|
| 115 | 11× | fn entries(dir: &Path) -> Result<Vec<PathBuf>, String> { |
| 116 | 11× | let read = fs::read_dir(dir).map_err(|e| format!("{}: {e}", dir.display()))?; |
| 117 | 17× | let mut all = read.map(|e| e.map(|e| e.path())).collect::<Result<Vec<_>, _>>().map_err(|e| format!("{}: {e}", dir.display()))?; |
| 118 | 11× | all.sort(); |
| 119 | 11× | Ok(all) |
| 120 | 11× | } |
| 121 | |
|
| 122 | |
|
| 123 | 10× | fn files(dir: &Path) -> Result<Vec<PathBuf>, String> { |
| 124 | 10× | let mut found = Vec::new(); |
| 125 | 14× | for p in entries(dir)? { |
| 126 | 14× | let kind = fs::symlink_metadata(&p).map_err(|e| format!("{}: {e}", p.display()))?.file_type(); |
| 127 | 14× | if kind.is_dir() { |
| 128 | 7× | found.extend(files(&p)?); |
| 129 | 7× | } else if kind.is_file() { |
| 130 | 7× | found.push(p); |
| 131 | 7× | } |
| 132 | | } |
| 133 | 10× | Ok(found) |
| 134 | 10× | } |
| 135 | |
|
| 136 | |
|
| 137 | 4× | fn shown(root: &Path, p: &Path) -> String { |
| 138 | 4× | let rel = p.strip_prefix(root).unwrap_or(p); |
| 139 | 12× | rel.components().map(|c| c.as_os_str().to_string_lossy()).collect::<Vec<_>>().join("/") |
| 140 | 4× | } |
| 141 | |
|
| 142 | 1× | fn shown_dirs(dirs: &[String]) -> String { |
| 143 | 1× | dirs.iter().map(|d| format!("{}/", d.trim_end_matches('/'))).collect::<Vec<_>>().join(" ") |
| 144 | 1× | } |
| 145 | |
|
| 146 | | #[cfg(test)] |
| 147 | | mod tests { |
| 148 | | use super::*; |
| 149 | |
|
| 150 | 3× | fn tree(files: &[(&str, &str)]) -> tempfile::TempDir { |
| 151 | 3× | let dir = tempfile::tempdir().unwrap(); |
| 152 | 10× | for (path, text) in files { |
| 153 | 10× | let p = dir.path().join(path); |
| 154 | 10× | fs::create_dir_all(p.parent().unwrap()).unwrap(); |
| 155 | 10× | fs::write(p, text).unwrap(); |
| 156 | 10× | } |
| 157 | 3× | dir |
| 158 | 3× | } |
| 159 | |
|
| 160 | 5× | fn run(f: impl FnOnce(&mut Vec<u8>) -> Done) -> (Done, String) { |
| 161 | 5× | let mut out = Vec::new(); |
| 162 | 5× | let done = f(&mut out); |
| 163 | 5× | (done, String::from_utf8(out).unwrap()) |
| 164 | 5× | } |
| 165 | |
|
| 166 | 4× | fn args(a: &[&str]) -> Vec<String> { |
| 167 | 7× | a.iter().map(|s| s.to_string()).collect() |
| 168 | 4× | } |
| 169 | |
|
| 170 | | #[test] |
| 171 | 1× | fn layout_lists_what_it_found_and_fails_on_the_first_missing() { |
| 172 | 1× | let t = tree(&[("Cargo.toml", ""), ("docs/m0.md", "")]); |
| 173 | 1× | let (done, out) = run(|o| layout(t.path(), &args(&["Cargo.toml", "docs/m0.md"]), o)); |
| 174 | 1× | assert_eq!(done, Ok(())); |
| 175 | 1× | assert_eq!(out, "found Cargo.toml\nfound docs/m0.md\n"); |
| 176 | 1× | let (done, out) = run(|o| layout(t.path(), &args(&["Cargo.toml", "nope.rs", "docs/m0.md"]), o)); |
| 177 | 1× | assert_eq!(done, Err("missing: nope.rs".to_string())); |
| 178 | 1× | assert_eq!(out, "found Cargo.toml\n"); |
| 179 | | |
| 180 | 1× | assert!(layout(t.path(), &args(&["docs"]), &mut Vec::new()).is_err()); |
| 181 | 1× | } |
| 182 | |
|
| 183 | | #[test] |
| 184 | 1× | fn todos_counts_every_marked_line_and_lists_the_first_twenty() { |
| 185 | 1× | let marker = MARKERS[0]; |
| 186 | 25× | let many: String = (0..25).map(|i| format!("// {marker} {i}\n")).collect(); |
| 187 | 1× | let t = tree(&[ |
| 188 | 1× | ("crates/a/src/lib.rs", &format!("fn a() {{}}\n// {} later\n", MARKERS[1])), |
| 189 | 1× | ("crates/b/src/many.rs", &many), |
| 190 | 1× | ("crates/b/blob.bin", "\u{0}"), |
| 191 | 1× | ]); |
| 192 | | |
| 193 | 1× | fs::write(t.path().join("crates/b/blob.bin"), [0xff, 0xfe, b'T']).unwrap(); |
| 194 | 1× | let (done, out) = run(|o| todos(t.path(), &args(&["crates"]), o)); |
| 195 | 1× | assert_eq!(done, Ok(())); |
| 196 | 1× | let lines: Vec<&str> = out.lines().collect(); |
| 197 | 1× | assert_eq!(lines[0], format!("26 {} notes in crates/", MARKERS.join("/"))); |
| 198 | 1× | assert_eq!(lines[1], format!("crates/a/src/lib.rs:2:// {} later", MARKERS[1])); |
| 199 | 1× | assert_eq!(lines[2], format!("crates/b/src/many.rs:1:// {marker} 0")); |
| 200 | 1× | assert_eq!(lines.len(), 1 + LISTED); |
| 201 | 1× | } |
| 202 | |
|
| 203 | | #[test] |
| 204 | 1× | fn size_counts_rust_lines_per_crate_in_name_order() { |
| 205 | 1× | let t = tree(&[ |
| 206 | 1× | ("crates/zeta/src/main.rs", "a\nb\nc\n"), |
| 207 | 1× | ("crates/alpha/src/lib.rs", "a\n"), |
| 208 | 1× | ("crates/alpha/src/deep/mod.rs", "a\nb\n"), |
| 209 | 1× | ("crates/alpha/README.md", "not rust\n"), |
| 210 | 1× | ("crates/loose.txt", "a file, not a crate\n"), |
| 211 | 1× | ]); |
| 212 | 1× | let (done, out) = run(|o| size(t.path(), "crates", o)); |
| 213 | 1× | assert_eq!(done, Ok(())); |
| 214 | 1× | assert_eq!(out, format!("{:<24} {:>6} lines\n{:<24} {:>6} lines\n", "crates/alpha/", 3, "crates/zeta/", 3)); |
| 215 | 1× | } |
| 216 | |
|
| 217 | | #[test] |
| 218 | 1× | fn summary_names_the_project() { |
| 219 | 1× | let (_, out) = run(|o| summary(Some("OxygenFlow"), o)); |
| 220 | 1× | assert_eq!(out, "OxygenFlow: every check passed on this state.\n"); |
| 221 | 1× | } |
| 222 | | } |