diff --git a/src/check.rs b/src/check.rs index 32990c2..1cd9d9d 100644 --- a/src/check.rs +++ b/src/check.rs @@ -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) { @@ -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) { 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"; } diff --git a/src/check/formatting.rs b/src/check/formatting.rs new file mode 100644 index 0000000..01aacda --- /dev/null +++ b/src/check/formatting.rs @@ -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::>() + .join(", "); + format!("{{ {middle} }}") +} diff --git a/src/check/testables.rs b/src/check/testables.rs index a696f19..9226aab 100644 --- a/src/check/testables.rs +++ b/src/check/testables.rs @@ -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 Result<(), String>>, + test: Box 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 { vec![ // rules Rule::new("ends_with_newline", ends_with_newline), + Rule::new("function_under_50l", function_under_50l), ] } diff --git a/src/main.rs b/src/main.rs index 138d37e..0b44c4b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -84,6 +84,7 @@ fn append_includes(list: &mut Vec) { fn compilation_args() -> Vec { let mut args = vec![ "-Wall".to_string(), + "-fsanitize=address".to_string(), "-Wextra".to_string(), "-std=c99".to_string(), ]; diff --git a/src/tasks.rs b/src/tasks.rs index 86be5a4..aa17132 100644 --- a/src/tasks.rs +++ b/src/tasks.rs @@ -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 } }