How to set up the project
$ curl --proto ' =https' --tlsv1.2 -sSf https://sh.rustup.rs | sh
$ rustup component add clippy
$ rustup component add rustfmt
# Run the project
$ cargo run
# Run tests
$ cargo test
# Run linters
$ cargo clippy
# Run rustfmt
$ cargo fmt
Create a new file in the src/checks
directory. The file name should contain the name of the check, for example: src/checks/example.rs
Add a new struct for this check, for example:
pub ( crate ) struct ExampleChecker {
template : String ,
}
Implement 2 methods for this struct: default
and run
, for example:
impl Default for ExampleChecker {
fn default ( ) -> Self {
Self {
template : String :: from ( "Example detected" ) ,
}
}
}
impl Check for ExampleChecker {
fn run ( & mut self , line : & LineEntry ) -> Option < Warning > {
// Write your check logic here...
if line. raw_string . starts_with ( "EXAMPLE" ) {
Some ( Warning :: new ( line. clone ( ) , self . template . clone ( ) ) )
} else {
None
}
}
}
Write tests for this check, for example:
#[ cfg( test) ]
mod tests {
use super :: * ;
use std:: path:: PathBuf ;
#[ test]
fn working_run ( ) {
let mut checker = ExampleChecker :: default ( ) ;
let line = & LineEntry {
number : 1 ,
file_path : PathBuf :: from ( ".env" ) ,
raw_string : String :: from ( "FOO=BAR" ) ,
} ;
assert_eq ! ( None , checker.run( line) ) ;
}
#[ test]
fn failing_run ( ) {
let mut checker = ExampleChecker :: default ( ) ;
let line = LineEntry {
number : 1 ,
file_path : PathBuf :: from ( ".env" ) ,
raw_string : String :: from ( "EXAMPLE=true" ) ,
} ;
let expected = Some ( Warning :: new ( line. clone ( ) , String :: from ( "Example detected" ) ) ) ;
assert_eq ! ( expected, checker.run( &line) ) ;
}
}
Add a new check to the file src/checks.rs
, for example:
mod example;
//...
fn checklist ( ) -> Vec < Box < dyn Check > > {
vec ! [
Box ::new( leading_space::LeadingSpaceChecker ::default ( ) ) ,
Box ::new( example::ExampleChecker ::default ( ) ) ,
]
}
That's all! You are awesome! 鉂わ笍