Skip to content

Commit ca56c4f

Browse files
committed
add clippy test to ci
1 parent 430a6e1 commit ca56c4f

File tree

10 files changed

+59
-82
lines changed

10 files changed

+59
-82
lines changed

.github/workflows/run-tests.yml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,11 @@ jobs:
1717
- uses: actions-rs/toolchain@v1
1818
with:
1919
toolchain: nightly
20+
- name: Run clippy
21+
uses: actions-rs/cargo@v1
22+
with:
23+
command: clippy
24+
args: -- -D warnings
2025
- name: Run unit tests
2126
uses: actions-rs/cargo@v1
2227
with:

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@ tera = "1.12.1"
1010
convert_case = "0.4.0"
1111
pluralize-rs = "0.1.0"
1212
include_dir = { version = "0.6.0", features = ["search"] }
13-
serde = {version="1.0.126", features = ["derive"]}
13+
serde = { version = "1.0.126", features = ["derive"] }
1414
serde_yaml = "0.8.17"
1515
serde_json = "1.0.64"
1616
toml = "0.5.8"

src/config/cli.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use structopt::StructOpt;
99
)]
1010
pub struct Config {
1111
/// Config file path.
12-
#[structopt(short="i", long, alias="input", parse(from_os_str), env = "CONFIG")]
12+
#[structopt(short = "i", long, alias = "input", parse(from_os_str), env = "CONFIG")]
1313
pub config: PathBuf,
1414
/// Output project path.
1515
#[structopt(short, long, parse(from_os_str), env = "OUTPUT")]
@@ -19,23 +19,23 @@ pub struct Config {
1919
pub force: bool,
2020

2121
/// Output project path.
22-
#[structopt(alias="dbms", long, env = "DATABASE")]
22+
#[structopt(alias = "dbms", long, env = "DATABASE")]
2323
pub database_engine: Option<String>,
2424
#[structopt(short, long, env = "API_NAME")]
2525
pub name: Option<String>,
2626

27-
#[structopt(alias="ddl", long, env = "DDL")]
27+
#[structopt(alias = "ddl", long, env = "DDL")]
2828
pub load_from_ddl: Option<String>,
29-
#[structopt(short="load-db", long, env = "LOAD_DB")]
29+
#[structopt(short = "load-db", long, env = "LOAD_DB")]
3030
pub load_from_db: Option<String>,
3131

32-
#[structopt(short="d", long, env = "DOCKER")]
33-
pub generate_docker: Option<bool>,
34-
#[structopt(alias="du", long, env = "DOCKER_USERNAME")]
32+
#[structopt(short = "d", long, env = "DOCKER")]
33+
pub generate_docker: Option<bool>,
34+
#[structopt(alias = "du", long, env = "DOCKER_USERNAME")]
3535
pub docker_username: Option<String>,
36-
#[structopt(alias="dt", long, env = "DOCKER_TAG")]
37-
pub docker_tag: Option<String>,
36+
#[structopt(alias = "dt", long, env = "DOCKER_TAG")]
37+
pub docker_tag: Option<String>,
3838

39-
#[structopt(short="k8s", long, env = "KUBERNETES")]
39+
#[structopt(short = "k8s", long, env = "KUBERNETES")]
4040
pub kubernetes: Option<bool>,
4141
}

src/config/file.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,3 @@
1-
use std::fs::OpenOptions;
2-
3-
use anyhow::{anyhow, Result};
41
use serde::{Deserialize, Serialize};
52

63
#[derive(Serialize, Deserialize, Debug, Clone)]
@@ -10,6 +7,7 @@ pub struct Field {
107
pub data_type: String,
118
}
129

10+
#[allow(clippy::upper_case_acronyms)]
1311
#[derive(Serialize, Deserialize, Debug, Clone)]
1412
pub struct API {
1513
pub name: String,
@@ -54,6 +52,7 @@ impl Default for Docker {
5452
}
5553
}
5654

55+
#[allow(clippy::upper_case_acronyms)]
5756
#[derive(Serialize, Deserialize, Debug, Clone)]
5857
pub struct CICD {
5958
#[serde(default)]
@@ -83,4 +82,3 @@ pub struct Config {
8382
#[serde(default)]
8483
pub cicd: CICD,
8584
}
86-

src/config/mod.rs

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
mod cli;
22
mod file;
33
use anyhow::anyhow;
4-
use std::{fs::File, io::Read, path::PathBuf};
4+
use std::{ffi::OsStr, fs::File, io::Read, path::PathBuf};
55

66
use structopt::StructOpt;
77

@@ -18,8 +18,8 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
1818
let mut file_config: file::Config = match cli_config
1919
.config
2020
.extension()
21-
.and_then(|it| it.to_str())
22-
.ok_or(anyhow!("Cannot open config file"))?
21+
.and_then(OsStr::to_str)
22+
.ok_or_else(|| anyhow!("Cannot open config file"))?
2323
{
2424
"toml" => {
2525
let mut content = String::new();
@@ -64,17 +64,15 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
6464
}
6565
let try_load_from_db = if let Some(addr) = cli_config.load_from_db {
6666
Some(addr)
67-
} else if let Some(addr) = file_config.implementation.database.url {
68-
Some(addr)
6967
} else {
70-
None
68+
file_config.implementation.database.url.clone()
7169
};
7270
if try_load_from_db.is_some() {
7371
if cli_config.name.is_none() {
7472
return Err(anyhow!("I need to know API name before load it from db!"));
7573
}
74+
todo!("load api config from database");
7675
}
77-
todo!("load api config from database");
7876
}
7977

8078
if let Some(generate_docker) = cli_config.generate_docker {
@@ -92,15 +90,19 @@ pub fn from_cli_config() -> anyhow::Result<Config> {
9290
file_config.cicd.kubernetes = match file_config.cicd.kubernetes {
9391
Some(false) => Some(false),
9492
Some(true) if file_config.implementation.database.url.is_none() => {
95-
return Err(anyhow!("generating kubernetes yaml file requires database connection string"));
93+
return Err(anyhow!(
94+
"generating kubernetes yaml file requires database connection string"
95+
));
9696
}
9797
Some(true) if file_config.cicd.docker.username.is_none() => {
9898
// todo: support other docker image registry than dockerhub
99-
return Err(anyhow!("generating kubernetes yaml file requires docker username"));
99+
return Err(anyhow!(
100+
"generating kubernetes yaml file requires docker username"
101+
));
100102
}
101103
Some(true) => Some(true),
102104
None if file_config.implementation.database.url.is_none() => Some(false),
103-
None => Some(true)
105+
None => Some(true),
104106
};
105107
Ok(Config {
106108
output: cli_config.output,
Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,6 @@
22
33
use serde::{Deserialize, Serialize};
44

5-
pub mod postgres;
6-
75
/// The database the user wants to use.
86
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
97
#[serde(rename_all = "lowercase")]

src/implementation/database/postgres.rs

Lines changed: 0 additions & 35 deletions
This file was deleted.

src/implementation/framework/golang.rs

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,9 +5,9 @@ use std::{collections::HashMap, path::Path};
55
use tera::{Result, Tera, Value};
66

77
use crate::{
8+
implementation::Implementation,
89
model::{data_type::DataType, Model},
910
render::render_simple,
10-
implementation::Implementation,
1111
};
1212

1313
/// "imports" for different golang files
@@ -29,17 +29,17 @@ pub struct Golang {
2929

3030
// TODO: It is highly possible that each language needs `data_type`, `register` and `render`, maybe we can add a trait.
3131
/// Get the string representing of a data_type in Golang.
32-
fn data_type(data_type: DataType) -> String {
32+
fn data_type(data_type: &DataType) -> String {
3333
match data_type {
34-
DataType::Int(x) if x <= 8 => "int8".to_string(),
35-
DataType::Int(x) if x <= 16 => "int16".to_string(),
36-
DataType::Int(x) if x <= 32 => "int32".to_string(),
34+
DataType::Int(x) if *x <= 8 => "int8".to_string(),
35+
DataType::Int(x) if *x <= 16 => "int16".to_string(),
36+
DataType::Int(x) if *x <= 32 => "int32".to_string(),
3737
DataType::Int(_) => "int64".to_string(),
38-
DataType::UInt(x) if x <= 8 => "uint8".to_string(),
39-
DataType::UInt(x) if x <= 16 => "uint16".to_string(),
40-
DataType::UInt(x) if x <= 32 => "uint32".to_string(),
38+
DataType::UInt(x) if *x <= 8 => "uint8".to_string(),
39+
DataType::UInt(x) if *x <= 16 => "uint16".to_string(),
40+
DataType::UInt(x) if *x <= 32 => "uint32".to_string(),
4141
DataType::UInt(_) => "uint64".to_string(),
42-
DataType::Float(x) if x <= 32 => "float32".to_string(),
42+
DataType::Float(x) if *x <= 32 => "float32".to_string(),
4343
DataType::Float(_) => "float64".to_string(),
4444
DataType::String(_) => "string".to_string(),
4545
DataType::DateTime => "time".to_string(),
@@ -48,7 +48,7 @@ fn data_type(data_type: DataType) -> String {
4848

4949
fn data_type_in_template(args: &HashMap<String, Value>) -> Result<Value> {
5050
let data_type_value: DataType = serde_json::from_value(args.get("data_type").unwrap().clone())?;
51-
Ok(Value::String(data_type(data_type_value)))
51+
Ok(Value::String(data_type(&data_type_value)))
5252
}
5353

5454
/// A function which can be used in the template for judging whether the datatype is a string

src/main.rs

Lines changed: 11 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use implementation::Implementation;
22

33
use crate::{
4-
model::{data_type::DataType, Field, Model},
54
implementation::framework::golang,
5+
model::{data_type::DataType, Field, Model},
66
};
77

8+
mod config;
9+
mod implementation;
810
mod model;
911
mod render;
10-
mod implementation;
11-
mod config;
1212

1313
fn main() {
1414
let mut tera = render::load_templates();
@@ -23,11 +23,17 @@ fn main() {
2323
name: "id".to_string(),
2424
data_type: DataType::UInt(64),
2525
},
26-
fields: config.generate_config.api.unwrap().fields.into_iter()
26+
fields: config
27+
.generate_config
28+
.api
29+
.unwrap()
30+
.fields
31+
.into_iter()
2732
.map(|it| Field {
2833
name: it.name,
2934
data_type: it.data_type.into(),
30-
}).collect(),
35+
})
36+
.collect(),
3137
};
3238
let mut context = tera::Context::new();
3339
context.insert("model", &model);

src/model/data_type.rs

Lines changed: 8 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
use convert_case::{Case, Casing};
22
use serde::{Deserialize, Serialize};
3-
use toml::value::Datetime;
43

54
/// Meta information about a string.
65
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, PartialOrd, Ord)]
@@ -27,11 +26,15 @@ impl From<String> for DataType {
2726
match s.as_str().to_case(Case::Snake).as_str() {
2827
"tiny_int" | "tinyint" | "tiny_integer" | "int8" => DataType::Int(8),
2928
"small_int" | "smallint" | "small_integer" | "int16" => DataType::Int(16),
30-
"int" | "integer" | "medium_int" | "mediumint" | "medium_integer" | "int32" => DataType::Int(32),
29+
"int" | "integer" | "medium_int" | "mediumint" | "medium_integer" | "int32" => {
30+
DataType::Int(32)
31+
}
3132
"big_int" | "bigint" | "big_integer" | "int64" => DataType::Int(64),
3233
"unsigned_tiny_int" | "unsigned_tiny_integer" | "uint8" => DataType::UInt(8),
3334
"unsigned_small_int" | "unsigned_small_integer" | "uint16" => DataType::UInt(16),
34-
"uint" | "unsigned" | "unsigned_medium_int" | "unsigned_medium_integer" | "uint32" => DataType::UInt(32),
35+
"uint" | "unsigned" | "unsigned_medium_int" | "unsigned_medium_integer" | "uint32" => {
36+
DataType::UInt(32)
37+
}
3538
"unsigned_big_int" | "unsigned_big_integer" | "uint64" => DataType::UInt(64),
3639
"float" | "float32" => DataType::Float(32),
3740
"double" | "float64" => DataType::Float(64),
@@ -40,7 +43,7 @@ impl From<String> for DataType {
4043
if s.starts_with("char(") {
4144
let length: usize = s
4245
.trim_start_matches("char(")
43-
.trim_end_matches(")")
46+
.trim_end_matches(')')
4447
.parse()
4548
.unwrap();
4649
DataType::String(Some(StringMeta {
@@ -50,7 +53,7 @@ impl From<String> for DataType {
5053
} else if s.starts_with("varchar(") {
5154
let length: usize = s
5255
.trim_start_matches("varchar(")
53-
.trim_end_matches(")")
56+
.trim_end_matches(')')
5457
.parse()
5558
.unwrap();
5659
DataType::String(Some(StringMeta {

0 commit comments

Comments
 (0)