|
| 1 | +use crate::check; |
| 2 | +#[allow(unused_imports)] |
| 3 | +use anyhow::{Context, Result}; |
| 4 | +use regex::Regex; |
| 5 | +use serde::{Deserialize, Serialize}; |
| 6 | +use std::path::{Path, PathBuf}; |
| 7 | + |
| 8 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 9 | +pub struct FileIssues { |
| 10 | + #[serde(skip_serializing_if = "Vec::is_empty")] |
| 11 | + pub trailing_whitespace: Vec<usize>, |
| 12 | + #[serde(skip_serializing_if = "std::ops::Not::not")] |
| 13 | + pub mixed_indentation: bool, |
| 14 | + #[serde(skip_serializing_if = "std::ops::Not::not")] |
| 15 | + pub no_final_newline: bool, |
| 16 | +} |
| 17 | + |
| 18 | +impl FileIssues { |
| 19 | + pub fn new() -> Self { |
| 20 | + FileIssues { |
| 21 | + trailing_whitespace: Vec::new(), |
| 22 | + mixed_indentation: false, |
| 23 | + no_final_newline: false, |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + pub fn has_issues(&self) -> bool { |
| 28 | + !self.trailing_whitespace.is_empty() || self.mixed_indentation || self.no_final_newline |
| 29 | + } |
| 30 | + |
| 31 | + pub fn total_issues(&self) -> usize { |
| 32 | + self.trailing_whitespace.len() |
| 33 | + + if self.mixed_indentation { 1 } else { 0 } |
| 34 | + + if self.no_final_newline { 1 } else { 0 } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +pub fn should_exclude(path: &Path) -> bool { |
| 39 | + let path_str = path.to_string_lossy(); |
| 40 | + let exclude_patterns = [ |
| 41 | + r"[\w\W/-]*\.go$", |
| 42 | + r"[\w\W/-]*\.patch$", |
| 43 | + r"^test/[\w/]*_corpus/[\w/]*", |
| 44 | + r"^tools/[\w/]*_corpus/[\w/]*", |
| 45 | + r"[\w/]*password_protected_password.txt$", |
| 46 | + ]; |
| 47 | + for pattern in &exclude_patterns { |
| 48 | + if let Ok(re) = Regex::new(pattern) { |
| 49 | + if re.is_match(&path_str) { |
| 50 | + return true; |
| 51 | + } |
| 52 | + } |
| 53 | + } |
| 54 | + false |
| 55 | +} |
| 56 | + |
| 57 | +pub fn process_file(path: &Path) -> Result<Option<(String, FileIssues)>> { |
| 58 | + if should_exclude(path) { |
| 59 | + return Ok(None); |
| 60 | + } |
| 61 | + let mut issues = FileIssues::new(); |
| 62 | + |
| 63 | + match check::trailing_whitespace(path) { |
| 64 | + Ok(lines) => issues.trailing_whitespace = lines, |
| 65 | + Err(e) => { |
| 66 | + return Err(e.context(format!( |
| 67 | + "Failed to check trailing whitespace for {}", |
| 68 | + path.display() |
| 69 | + ))); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + match check::mixed_indentation(path) { |
| 74 | + Ok(mixed) => issues.mixed_indentation = mixed, |
| 75 | + Err(e) => { |
| 76 | + return Err(e.context(format!( |
| 77 | + "Failed to check mixed indentation for {}", |
| 78 | + path.display() |
| 79 | + ))); |
| 80 | + } |
| 81 | + } |
| 82 | + |
| 83 | + match check::final_newline(path) { |
| 84 | + Ok(missing) => issues.no_final_newline = missing, |
| 85 | + Err(e) => { |
| 86 | + return Err(e.context(format!( |
| 87 | + "Failed to check final newline for {}", |
| 88 | + path.display() |
| 89 | + ))); |
| 90 | + } |
| 91 | + } |
| 92 | + |
| 93 | + if issues.has_issues() { |
| 94 | + Ok(Some((path.display().to_string(), issues))) |
| 95 | + } else { |
| 96 | + Ok(None) |
| 97 | + } |
| 98 | +} |
| 99 | + |
| 100 | +/// Recursively find all files in a directory |
| 101 | +pub fn find_files(path: &Path) -> Result<Vec<PathBuf>> { |
| 102 | + let mut files = Vec::new(); |
| 103 | + if path.is_file() { |
| 104 | + files.push(path.to_path_buf()); |
| 105 | + } else if path.is_dir() { |
| 106 | + for entry in std::fs::read_dir(path)? { |
| 107 | + let entry = entry?; |
| 108 | + let path = entry.path(); |
| 109 | + if path.is_file() { |
| 110 | + files.push(path); |
| 111 | + } else if path.is_dir() { |
| 112 | + files.extend(find_files(&path)?); |
| 113 | + } |
| 114 | + } |
| 115 | + } |
| 116 | + Ok(files) |
| 117 | +} |
0 commit comments