improved checks
This commit is contained in:
parent
2f71ed5e03
commit
17636fca11
5 changed files with 57 additions and 20 deletions
|
@ -7,9 +7,7 @@ use crate::{
|
||||||
utils::{log_failure, log_process, log_success},
|
utils::{log_failure, log_process, log_success},
|
||||||
};
|
};
|
||||||
|
|
||||||
/// TODO: fill with appropriate rules
|
mod formatting;
|
||||||
const FORMAT_CONFIG: &str = r#"{BasedOnStyle: llvm}"#;
|
|
||||||
|
|
||||||
mod testables;
|
mod testables;
|
||||||
|
|
||||||
pub fn main(files: Vec<String>) {
|
pub fn main(files: Vec<String>) {
|
||||||
|
@ -35,7 +33,7 @@ pub enum Diff {
|
||||||
|
|
||||||
fn check_formatting(file: String) {
|
fn check_formatting(file: String) {
|
||||||
let content = fs::read_to_string(&file).unwrap();
|
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 line_number = 0usize;
|
||||||
let mut invalid = false;
|
let mut invalid = false;
|
||||||
let differences = diff::lines(&content, &formatted)
|
let differences = diff::lines(&content, &formatted)
|
||||||
|
@ -94,7 +92,7 @@ fn check_formatting(file: String) {
|
||||||
|
|
||||||
pub fn format(files: Vec<String>) {
|
pub fn format(files: Vec<String>) {
|
||||||
for file in files {
|
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') {
|
if !formatted.ends_with('\n') {
|
||||||
formatted += "\n";
|
formatted += "\n";
|
||||||
}
|
}
|
||||||
|
|
17
src/check/formatting.rs
Normal file
17
src/check/formatting.rs
Normal 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} }}")
|
||||||
|
}
|
|
@ -1,18 +1,12 @@
|
||||||
fn ends_with_newline(source: String) -> Result<(), String> {
|
pub type RuleResult = Result<(), String>;
|
||||||
if !source.ends_with('\n') {
|
|
||||||
Err("source does not end with newline".into())
|
|
||||||
} else {
|
|
||||||
Ok(())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub struct Rule {
|
pub struct Rule {
|
||||||
name: String,
|
name: String,
|
||||||
test: Box<dyn Fn(String) -> Result<(), String>>,
|
test: Box<dyn Fn(String) -> RuleResult>,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Rule {
|
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 name = name.to_string();
|
||||||
let test = Box::new(test);
|
let test = Box::new(test);
|
||||||
Self { name, test }
|
Self { name, test }
|
||||||
|
@ -22,15 +16,32 @@ impl Rule {
|
||||||
&self.name
|
&self.name
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn test(&self, source: String) -> Result<(), String> {
|
pub fn test(&self, source: String) -> RuleResult {
|
||||||
(self.test)(source)
|
(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
|
/// TODO: fill with appropriate rules
|
||||||
pub fn tests() -> Vec<Rule> {
|
pub fn tests() -> Vec<Rule> {
|
||||||
vec![
|
vec![
|
||||||
// rules
|
// rules
|
||||||
Rule::new("ends_with_newline", ends_with_newline),
|
Rule::new("ends_with_newline", ends_with_newline),
|
||||||
|
Rule::new("function_under_50l", function_under_50l),
|
||||||
]
|
]
|
||||||
}
|
}
|
||||||
|
|
|
@ -84,6 +84,7 @@ fn append_includes(list: &mut Vec<String>) {
|
||||||
fn compilation_args() -> Vec<String> {
|
fn compilation_args() -> Vec<String> {
|
||||||
let mut args = vec![
|
let mut args = vec![
|
||||||
"-Wall".to_string(),
|
"-Wall".to_string(),
|
||||||
|
"-fsanitize=address".to_string(),
|
||||||
"-Wextra".to_string(),
|
"-Wextra".to_string(),
|
||||||
"-std=c99".to_string(),
|
"-std=c99".to_string(),
|
||||||
];
|
];
|
||||||
|
|
20
src/tasks.rs
20
src/tasks.rs
|
@ -1,11 +1,11 @@
|
||||||
use std::{
|
use std::{
|
||||||
fs,
|
fs,
|
||||||
path::PathBuf,
|
path::PathBuf,
|
||||||
process::{Command, ExitStatus, Stdio},
|
process::{exit, Command, ExitStatus, Stdio},
|
||||||
};
|
};
|
||||||
|
|
||||||
use crate::utils::{
|
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 {
|
pub struct CompileTask {
|
||||||
|
@ -145,10 +145,20 @@ impl FormatTask {
|
||||||
.arg(format!("-style={config}"))
|
.arg(format!("-style={config}"))
|
||||||
.stdout(Stdio::piped())
|
.stdout(Stdio::piped())
|
||||||
.stderr(Stdio::piped());
|
.stderr(Stdio::piped());
|
||||||
command.status().unwrap();
|
|
||||||
|
|
||||||
let result = command.output().unwrap().stdout;
|
let status = command.status().unwrap();
|
||||||
String::from_utf8(result).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
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue