improved checks

This commit is contained in:
mb 2022-10-06 18:10:07 +02:00
parent 2f71ed5e03
commit 17636fca11
5 changed files with 57 additions and 20 deletions

View file

@ -7,9 +7,7 @@ use crate::{
utils::{log_failure, log_process, log_success},
};
/// TODO: fill with appropriate rules
const FORMAT_CONFIG: &str = r#"{BasedOnStyle: llvm}"#;
mod formatting;
mod testables;
pub fn main(files: Vec<String>) {
@ -35,7 +33,7 @@ pub enum Diff {
fn check_formatting(file: String) {
let content = fs::read_to_string(&file).unwrap();
let formatted = FormatTask::new(file.clone(), FORMAT_CONFIG.into()).run();
let formatted = FormatTask::new(file.clone(), formatting::formatted_config()).run();
let mut line_number = 0usize;
let mut invalid = false;
let differences = diff::lines(&content, &formatted)
@ -94,7 +92,7 @@ fn check_formatting(file: String) {
pub fn format(files: Vec<String>) {
for file in files {
let mut formatted = FormatTask::new(file.clone(), FORMAT_CONFIG.into()).run();
let mut formatted = FormatTask::new(file.clone(), formatting::formatted_config()).run();
if !formatted.ends_with('\n') {
formatted += "\n";
}

17
src/check/formatting.rs Normal file
View file

@ -0,0 +1,17 @@
pub type StaticConf = [(&'static str, &'static str)];
/// TODO: fill with appropriate rules
pub const FORMAT_CONFIG: &StaticConf = &[
// (key, value)
("BasedOnStyle", "GNU"),
("IndentWidth", "4"),
];
pub fn formatted_config() -> String {
let middle = FORMAT_CONFIG
.into_iter()
.map(|(key, value)| format!("{key}: {value}"))
.collect::<Vec<_>>()
.join(", ");
format!("{{ {middle} }}")
}

View file

@ -1,18 +1,12 @@
fn ends_with_newline(source: String) -> Result<(), String> {
if !source.ends_with('\n') {
Err("source does not end with newline".into())
} else {
Ok(())
}
}
pub type RuleResult = Result<(), String>;
pub struct Rule {
name: String,
test: Box<dyn Fn(String) -> Result<(), String>>,
test: Box<dyn Fn(String) -> RuleResult>,
}
impl Rule {
pub fn new(name: impl ToString, test: impl 'static + Fn(String) -> Result<(), String>) -> Self {
pub fn new(name: impl ToString, test: impl 'static + Fn(String) -> RuleResult) -> Self {
let name = name.to_string();
let test = Box::new(test);
Self { name, test }
@ -22,15 +16,32 @@ impl Rule {
&self.name
}
pub fn test(&self, source: String) -> Result<(), String> {
pub fn test(&self, source: String) -> RuleResult {
(self.test)(source)
}
}
fn ends_with_newline(source: String) -> RuleResult {
if !source.ends_with('\n') {
Err("source does not end with newline".into())
} else {
Ok(())
}
}
fn function_under_50l(source: String) -> RuleResult {
for character in source.chars() {
let c: char = character;
}
Ok(())
}
/// TODO: fill with appropriate rules
pub fn tests() -> Vec<Rule> {
vec![
// rules
Rule::new("ends_with_newline", ends_with_newline),
Rule::new("function_under_50l", function_under_50l),
]
}

View file

@ -84,6 +84,7 @@ fn append_includes(list: &mut Vec<String>) {
fn compilation_args() -> Vec<String> {
let mut args = vec![
"-Wall".to_string(),
"-fsanitize=address".to_string(),
"-Wextra".to_string(),
"-std=c99".to_string(),
];

View file

@ -1,11 +1,11 @@
use std::{
fs,
path::PathBuf,
process::{Command, ExitStatus, Stdio},
process::{exit, Command, ExitStatus, Stdio},
};
use crate::utils::{
log_command_run, log_separator_bottom, log_separator_top, tmp_file_path, Apply,
log_command_run, log_failure, log_separator_bottom, log_separator_top, tmp_file_path, Apply,
};
pub struct CompileTask {
@ -145,10 +145,20 @@ impl FormatTask {
.arg(format!("-style={config}"))
.stdout(Stdio::piped())
.stderr(Stdio::piped());
command.status().unwrap();
let result = command.output().unwrap().stdout;
String::from_utf8(result).unwrap()
let status = command.status().unwrap();
let out = command.output().unwrap().stdout;
let out = String::from_utf8(out).unwrap();
let err = command.output().unwrap().stderr;
let err = String::from_utf8(err).unwrap();
if !status.success() {
log_failure("failed formatting");
println!("{out}");
eprintln!("{err}");
exit(1);
}
out
}
}