Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add pre-commit hooks for linting & tests #155

Merged
merged 5 commits into from
May 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
79 changes: 0 additions & 79 deletions .github/workflows/main.yml

This file was deleted.

5 changes: 5 additions & 0 deletions .rusty-hook.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
[hooks]
pre-commit = "cargo fmt && cargo clippy -- -Dwarnings && cargo test"

[logging]
verbose = true
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,9 @@ authors = ["Michael Flaherty (Headline#9999)"]
edition = "2018"
build = "src/build.rs"

[dev-dependencies]
rusty-hook = "0.11.2"

[dependencies]
tokio = { version = "1", features = ["full"] }
reqwest = { version = "0.11" }
Expand Down
2 changes: 1 addition & 1 deletion src/apis/mod.rs
Original file line number Diff line number Diff line change
@@ -1 +1 @@
pub mod dbl;
pub mod dbl;
26 changes: 10 additions & 16 deletions src/boilerplate/c.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@ use crate::boilerplate::generator::BoilerPlateGenerator;
use crate::utls::constants::C_LIKE_MAIN_REGEX;

pub struct CGenerator {
input : String
input: String,
}

impl BoilerPlateGenerator for CGenerator {
fn new(input: &str) -> Self {
let mut formated = input.to_string();
formated = formated.replace(";", ";\n"); // separate lines by ;
formated = formated.replace(';', ";\n"); // separate lines by ;

Self {
input : formated
}
Self { input: formated }
}

fn generate(&self) -> String {
let mut main_body = String::default();
let mut header = String::default();

let mut lines = self.input.split("\n");
while let Some(line) = lines.next() {
let lines = self.input.split('\n');
for line in lines {
let trimmed = line.trim();
if trimmed.starts_with("using") {
header.push_str(&format!("{}\n", trimmed));
}
else if trimmed.starts_with("#i") {
if trimmed.starts_with("using") || trimmed.starts_with("#i") {
header.push_str(&format!("{}\n", trimmed));
}
else {
} else {
main_body.push_str(&format!("{}\n", trimmed))
}
}
Expand All @@ -41,10 +35,10 @@ impl BoilerPlateGenerator for CGenerator {

fn needs_boilerplate(&self) -> bool {
for m in C_LIKE_MAIN_REGEX.captures_iter(&self.input) {
if let Some(_) = m.name("main") {
if m.name("main").is_some() {
return false;
}
}
return true;
true
}
}
}
26 changes: 10 additions & 16 deletions src/boilerplate/cpp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,27 @@ use crate::boilerplate::generator::BoilerPlateGenerator;
use crate::utls::constants::C_LIKE_MAIN_REGEX;

pub struct CppGenerator {
input : String
input: String,
}

impl BoilerPlateGenerator for CppGenerator {
fn new(input: &str) -> Self {
let mut formated = input.to_string();
formated = formated.replace(";", ";\n"); // separate lines by ;
formated = formated.replace(';', ";\n"); // separate lines by ;

Self {
input : formated
}
Self { input: formated }
}

fn generate(&self) -> String {
let mut main_body = String::default();
let mut header = String::default();

let mut lines = self.input.split("\n");
while let Some(line) = lines.next() {
let lines = self.input.split('\n');
for line in lines {
let trimmed = line.trim();
if trimmed.starts_with("using") {
header.push_str(&format!("{}\n", trimmed));
}
else if trimmed.starts_with("#i") {
if trimmed.starts_with("using") || trimmed.starts_with("#i") {
header.push_str(&format!("{}\n", trimmed));
}
else {
} else {
main_body.push_str(&format!("{}\n", trimmed))
}
}
Expand All @@ -42,10 +36,10 @@ impl BoilerPlateGenerator for CppGenerator {

fn needs_boilerplate(&self) -> bool {
for m in C_LIKE_MAIN_REGEX.captures_iter(&self.input) {
if let Some(_) = m.name("main") {
if m.name("main").is_some() {
return false;
}
}
return true;
true
}
}
}
26 changes: 13 additions & 13 deletions src/boilerplate/csharp.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,30 +2,27 @@ use crate::boilerplate::generator::BoilerPlateGenerator;
use crate::utls::constants::CSHARP_MAIN_REGEX;

pub struct CSharpGenerator {
input : String
input: String,
}

impl BoilerPlateGenerator for CSharpGenerator {
fn new(input: &str) -> Self {
let mut formated = input.to_string();
formated = formated.replace(";", ";\n"); // separate lines by ;
formated = formated.replace(';', ";\n"); // separate lines by ;

Self {
input : formated
}
Self { input: formated }
}

fn generate(&self) -> String {
let mut main_body = String::default();
let mut header = String::default();

let mut lines = self.input.split("\n");
while let Some(line) = lines.next() {
let lines = self.input.split('\n');
for line in lines {
let trimmed = line.trim();
if trimmed.starts_with("using") {
header.push_str(&format!("{}\n", trimmed));
}
else {
} else {
main_body.push_str(&format!("{}\n", trimmed))
}
}
Expand All @@ -34,15 +31,18 @@ impl BoilerPlateGenerator for CSharpGenerator {
if header.is_empty() {
header.push_str("using System;");
}
format!("{}\nnamespace Main{{\nclass Program {{\n static void Main(string[] args) {{\n{}}}}}}}", header, main_body)
format!(
"{}\nnamespace Main{{\nclass Program {{\n static void Main(string[] args) {{\n{}}}}}}}",
header, main_body
)
}

fn needs_boilerplate(&self) -> bool {
for m in CSHARP_MAIN_REGEX.captures_iter(&self.input) {
if let Some(_) = m.name("main") {
if m.name("main").is_some() {
return false;
}
}
return true;
true
}
}
}
49 changes: 22 additions & 27 deletions src/boilerplate/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,20 +4,26 @@ use crate::boilerplate::csharp::CSharpGenerator;
use crate::boilerplate::java::JavaGenerator;

pub trait BoilerPlateGenerator {
fn new(input : &str) -> Self where Self: Sized;
fn new(input: &str) -> Self
where
Self: Sized;
fn generate(&self) -> String;
fn needs_boilerplate(&self) -> bool;
}

pub struct BoilerPlate<T: ?Sized> where T: BoilerPlateGenerator {
generator : Box<T>
pub struct BoilerPlate<T: ?Sized>
where
T: BoilerPlateGenerator,
{
generator: Box<T>,
}

impl<T: ?Sized> BoilerPlate<T> where T: BoilerPlateGenerator{
pub fn new(generator : Box<T>) -> Self {
Self {
generator
}
impl<T: ?Sized> BoilerPlate<T>
where
T: BoilerPlateGenerator,
{
pub fn new(generator: Box<T>) -> Self {
Self { generator }
}

pub fn generate(&self) -> String {
Expand All @@ -44,25 +50,14 @@ impl BoilerPlateGenerator for Null {
}
}

pub fn boilerplate_factory(language : &str, code : &str)
-> BoilerPlate<dyn BoilerPlateGenerator> {
return match language {
"c++" => {
BoilerPlate::new(Box::new(CppGenerator::new(code)))
}
"c" => {
BoilerPlate::new(Box::new(CGenerator::new(code)))
}
"java" => {
BoilerPlate::new(Box::new(JavaGenerator::new(code)))
}
"c#" => {
BoilerPlate::new(Box::new(CSharpGenerator::new(code)))
}
pub fn boilerplate_factory(language: &str, code: &str) -> BoilerPlate<dyn BoilerPlateGenerator> {
match language {
"c++" => BoilerPlate::new(Box::new(CppGenerator::new(code))),
"c" => BoilerPlate::new(Box::new(CGenerator::new(code))),
"java" => BoilerPlate::new(Box::new(JavaGenerator::new(code))),
"c#" => BoilerPlate::new(Box::new(CSharpGenerator::new(code))),
// since all compilations go through this path, we have a Null generator whose
// needs_boilerplate() always returns false.
_ => {
BoilerPlate::new(Box::new(Null::new(code)))
}
_ => BoilerPlate::new(Box::new(Null::new(code))),
}
}
}
Loading