Skip to content

Commit

Permalink
code refactor && add github action
Browse files Browse the repository at this point in the history
  • Loading branch information
xring committed Apr 5, 2024
1 parent ea3a047 commit 7d229ec
Show file tree
Hide file tree
Showing 16 changed files with 670 additions and 453 deletions.
112 changes: 112 additions & 0 deletions .github/workflows/rust.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
name: Rust

on: [push, pull_request]

jobs:

codestyle:
runs-on: ubuntu-latest
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v1
with:
components: rustfmt
# Note that `nightly` is required for `license_template_path`, as
# it's an unstable feature.
rust-version: nightly
- uses: actions/checkout@v2
- run: cargo +nightly fmt -- --check --config-path <(echo 'license_template_path = "HEADER"')

lint:
runs-on: ubuntu-latest
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v1
with:
components: clippy
- uses: actions/checkout@v2
- run: cargo clippy --all-targets --all-features -- -D warnings

compile:
runs-on: ubuntu-latest
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v1
- uses: actions/checkout@master
- run: cargo check --all-targets --all-features

docs:
runs-on: ubuntu-latest
env:
RUSTDOCFLAGS: "-Dwarnings"
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v1
- uses: actions/checkout@master
- run: cargo doc --document-private-items --no-deps --workspace --all-features

compile-no-std:
runs-on: ubuntu-latest
steps:
- name: Set up Rust
uses: hecrj/setup-rust-action@v1
with:
targets: 'thumbv6m-none-eabi'
- uses: actions/checkout@master
- run: cargo check --no-default-features --target thumbv6m-none-eabi

test:
strategy:
matrix:
rust: [stable, beta, nightly]
runs-on: ubuntu-latest
steps:
- name: Setup Rust
uses: hecrj/setup-rust-action@v1
with:
rust-version: ${{ matrix.rust }}
- name: Install Tarpaulin
uses: actions-rs/[email protected]
with:
crate: cargo-tarpaulin
version: 0.14.2
use-tool-cache: true
- name: Checkout
uses: actions/checkout@v2
- name: Test
run: cargo test --all-features

# test-coverage:
# runs-on: ubuntu-latest
# steps:
# - name: Setup Rust
# uses: hecrj/setup-rust-action@v1
# with:
# rust-version: stable
# - name: Install Tarpaulin
# uses: actions-rs/[email protected]
# with:
# crate: cargo-tarpaulin
# version: 0.14.2
# use-tool-cache: true
# - name: Checkout
# uses: actions/checkout@v2
# - name: Coverage
# run: cargo tarpaulin -o Lcov --output-dir ./coverage
# - name: Coveralls
# uses: coverallsapp/github-action@master
# with:
# github-token: ${{ secrets.GITHUB_TOKEN }}
#
# publish-crate:
# if: startsWith(github.ref, 'refs/tags/v0')
# runs-on: ubuntu-latest
# needs: [test]
# steps:
# - name: Set up Rust
# uses: hecrj/setup-rust-action@v1
# - uses: actions/checkout@v2
# - name: Publish
# shell: bash
# run: |
# cargo publish --token ${{ secrets.CRATES_TOKEN }}
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ license = "Apache-2.0"
serde = "1"
serde_derive = "1"
nom = "7"
log = "0.4"

[dev-dependencies]
pretty_assertions = "0.5.1"
2 changes: 2 additions & 0 deletions src/base/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -482,6 +482,8 @@ impl ColumnConstraint {
}),
map(tag("''"), |_| Literal::String(String::from(""))),
map(tag_no_case("NULL"), |_| Literal::Null),
map(tag_no_case("FALSE"), |_| Literal::Bool(false)),
map(tag_no_case("TRUE"), |_| Literal::Bool(true)),
map(
tuple((tag_no_case("CURRENT_TIMESTAMP"), opt(delim_digit))),
|_| Literal::CurrentTimestamp,
Expand Down
7 changes: 6 additions & 1 deletion src/base/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ pub enum DataType {
Longtext,
Text,
Json,
Uuid,
Date,
DateTime(u16),
Timestamp,
Expand Down Expand Up @@ -68,6 +69,7 @@ impl fmt::Display for DataType {
DataType::Longtext => write!(f, "LONGTEXT"),
DataType::Text => write!(f, "TEXT"),
DataType::Json => write!(f, "JSON"),
DataType::Uuid => write!(f, "UUID"),
DataType::Date => write!(f, "DATE"),
DataType::DateTime(len) => write!(f, "DATETIME({})", len),
DataType::Timestamp => write!(f, "TIMESTAMP"),
Expand All @@ -93,7 +95,9 @@ impl DataType {
Self::tiny_int,
Self::big_int,
Self::sql_int_type,
map(tag_no_case("bool"), |_| DataType::Bool),
map(alt((tag_no_case("boolean"), tag_no_case("bool"))), |_| {
DataType::Bool
}),
map(
tuple((
tag_no_case("char"),
Expand Down Expand Up @@ -139,6 +143,7 @@ impl DataType {
),
map(tag_no_case("text"), |_| DataType::Text),
map(tag_no_case("json"), |_| DataType::Json),
map(tag_no_case("uuid"), |_| DataType::Uuid),
map(
tuple((tag_no_case("timestamp"), opt(delim_digit), multispace0)),
|_| DataType::Timestamp,
Expand Down
10 changes: 9 additions & 1 deletion src/base/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ use common::{as_alias, opt_delimited, ws_sep_comma};

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum Literal {
Bool(bool),
Null,
Integer(i64),
UnsignedInteger(u64),
Expand Down Expand Up @@ -81,7 +82,7 @@ impl Literal {
}
}),
map(tag("\\\\"), |_| "\\"),
map(tag("\\b"), |_| "\x08"), // 注意:\x7f 是 DEL,\x08 是退格
map(tag("\\b"), |_| "\x7F"), // 注意:\x7f 是 DEL,\x08 是退格
map(tag("\\r"), |_| "\r"),
map(tag("\\n"), |_| "\n"),
map(tag("\\t"), |_| "\t"),
Expand Down Expand Up @@ -189,6 +190,13 @@ impl ToString for Literal {
fn to_string(&self) -> String {
match *self {
Literal::Null => "NULL".to_string(),
Literal::Bool(ref value) => {
if *value {
"TRUE".to_string()
} else {
"FALSE".to_string()
}
}
Literal::Integer(ref i) => format!("{}", i),
Literal::UnsignedInteger(ref i) => format!("{}", i),
Literal::FixedPoint(ref f) => format!("{}.{}", f.integral, f.fractional),
Expand Down
3 changes: 2 additions & 1 deletion src/common/arithmetic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,7 +394,7 @@ mod tests {
let exprs = [
"CAST(`t`.`foo` AS signed int) + CAST(`t`.`bar` AS signed int) ",
"CAST(5 AS bigint) - foo ",
"CAST(5 AS bigint) - foo AS 5_minus_foo",
"CAST(5 AS bigint) - foo AS `5_minus_foo`",
];

// XXX(malte): currently discards the cast and type information!
Expand All @@ -417,6 +417,7 @@ mod tests {
for (i, e) in exprs.iter().enumerate() {
let res = ArithmeticExpression::parse(e);
assert!(res.is_ok(), "{} failed to parse", e);
println!("{:?}", res);
assert_eq!(res.unwrap().1, expected[i]);
}
}
Expand Down
18 changes: 14 additions & 4 deletions src/common/key_part.rs
Original file line number Diff line number Diff line change
Expand Up @@ -56,8 +56,13 @@ impl KeyPart {
/// {col_name [(length)] | (expr)}
#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum KeyPartType {
ColumnNameWithLength(String, Option<usize>),
Expr(String),
ColumnNameWithLength {
col_name: String,
length: Option<usize>,
},
Expr {
expr: String,
},
}

impl KeyPartType {
Expand All @@ -84,9 +89,14 @@ impl KeyPartType {

alt((
map(col_name_with_length, |(_, col_name, _, length)| {
KeyPartType::ColumnNameWithLength(String::from(col_name), length)
KeyPartType::ColumnNameWithLength {
col_name: String::from(col_name),
length,
}
}),
map(expr, |expr| KeyPartType::Expr {
expr: String::from(expr),
}),
map(expr, |expr| KeyPartType::Expr(String::from(expr))),
))(i)
}
}
3 changes: 2 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -499,7 +499,8 @@ pub fn as_alias(i: &str) -> IResult<&str, &str, ParseSQLError<&str>> {
map(
tuple((
multispace1,
opt(pair(tag_no_case("as"), multispace1)),
opt(pair(tag_no_case("AS"), multispace1)),
// FIXME as can starts with number
sql_identifier,
)),
|a| a.2,
Expand Down
41 changes: 29 additions & 12 deletions src/common/table_option.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use nom::branch::alt;
use nom::bytes::complete::{tag, tag_no_case, take_until};
use nom::character::complete::{digit1, multispace0, multispace1};
use nom::combinator::{map, opt};
use nom::combinator::{map, opt, value};
use nom::sequence::{delimited, tuple};
use nom::{IResult, Parser};

Expand Down Expand Up @@ -47,6 +47,7 @@ pub enum TableOption {
AutoIncrement(u64),
AvgRowLength(u64),
DefaultCharacterSet(String),
DefaultCharset(String),
Checksum(u8),
DefaultCollate(String),
Comment(String),
Expand Down Expand Up @@ -114,6 +115,7 @@ impl TableOption {
Self::auto_increment,
Self::avg_row_length,
Self::default_character_set,
Self::default_charset,
Self::checksum,
Self::default_collate,
Self::comment,
Expand Down Expand Up @@ -167,14 +169,14 @@ impl TableOption {
fn auto_increment(i: &str) -> IResult<&str, TableOption, ParseSQLError<&str>> {
map(
tuple((
tag_no_case("AUTO_INCREMENT "),
tag_no_case("AUTO_INCREMENT"),
multispace0,
opt(tag("=")),
multispace0,
digit1,
multispace0,
)),
|(_, _, _, value, _, _): (&str, &str, Option<&str>, &str, &str, &str)| {
|(_, _, _, _, value, _): (&str, &str, Option<&str>, &str, &str, &str)| {
TableOption::AutoIncrement(value.parse::<u64>().unwrap())
},
)(i)
Expand Down Expand Up @@ -204,7 +206,6 @@ impl TableOption {
opt(tag_no_case("DEFAULT ")),
multispace0,
tuple((
multispace1,
tag_no_case("CHARACTER"),
multispace1,
tag_no_case("SET"),
Expand All @@ -219,6 +220,25 @@ impl TableOption {
)(i)
}

/// [DEFAULT] CHARSET [=] charset_name

Check failure on line 223 in src/common/table_option.rs

View workflow job for this annotation

GitHub Actions / docs

unresolved link to `DEFAULT`
fn default_charset(i: &str) -> IResult<&str, TableOption, ParseSQLError<&str>> {
map(
tuple((
opt(tag_no_case("DEFAULT ")),
multispace0,
tuple((
tag_no_case("CHARSET"),
multispace0,
opt(tag("=")),
multispace0,
)),
map(sql_identifier, |x| String::from(x)),
multispace0,
)),
|(_, _, _, charset_name, _)| TableOption::DefaultCharset(charset_name),
)(i)
}

/// CHECKSUM [=] {0 | 1}
fn checksum(i: &str) -> IResult<&str, TableOption, ParseSQLError<&str>> {
map(
Expand All @@ -240,31 +260,28 @@ impl TableOption {
tuple((
opt(tag_no_case("DEFAULT ")),
multispace0,
tag_no_case("CHARACTER"),
multispace1,
map(
tuple((
multispace1,
tag_no_case("COLLATE"),
multispace1,
opt(tag("=")),
multispace0,
sql_identifier,
multispace0,
)),
|(_, _, _, _, _, collation_name, _)| String::from(collation_name),
|(_, _, _, _, collation_name, _)| String::from(collation_name),
),
multispace0,
)),
|(_, _, _, _, collation_name, _)| TableOption::DefaultCollate(collation_name),
|(_, _, collation_name, _)| TableOption::DefaultCollate(collation_name),
)(i)
}

/// COMMENT [=] 'string'
fn comment(i: &str) -> IResult<&str, TableOption, ParseSQLError<&str>> {
map(
tuple((
tag_no_case("COMMENT "),
tag_no_case("COMMENT"),
multispace0,
opt(tag("=")),
multispace0,
Expand Down Expand Up @@ -382,7 +399,7 @@ impl TableOption {
map(
tuple((
tag_no_case("ENGINE"),
multispace1,
multispace0,
opt(tag("=")),
multispace0,
sql_identifier,
Expand Down Expand Up @@ -479,7 +496,7 @@ impl TableOption {
fn pack_keys(i: &str) -> IResult<&str, TableOption, ParseSQLError<&str>> {
map(
tuple((
tag_no_case("INSERT_METHOD "),
tag_no_case("PACK_KEYS"),
multispace0,
opt(tag("=")),
multispace0,
Expand Down
Loading

0 comments on commit 7d229ec

Please sign in to comment.