Skip to content

Commit

Permalink
fix lint
Browse files Browse the repository at this point in the history
  • Loading branch information
xring committed Apr 6, 2024
1 parent 3ec3ee4 commit e1dc41b
Show file tree
Hide file tree
Showing 37 changed files with 191 additions and 281 deletions.
37 changes: 13 additions & 24 deletions src/base/column.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ impl Display for FunctionArguments {
}
}

impl<'a> From<Vec<FunctionArgument>> for FunctionArguments {
impl From<Vec<FunctionArgument>> for FunctionArguments {
fn from(args: Vec<FunctionArgument>) -> FunctionArguments {
FunctionArguments { arguments: args }
}
Expand All @@ -164,7 +164,7 @@ impl FunctionArgument {
map(CaseWhenExpression::parse, |cw| {
FunctionArgument::Conditional(cw)
}),
map(Column::without_alias, |c| FunctionArgument::Column(c)),
map(Column::without_alias, FunctionArgument::Column),
))(i)
}

Expand Down Expand Up @@ -229,10 +229,7 @@ impl Column {
map(table_parser, |tup| Column {
name: tup.1.to_string(),
alias: None,
table: match tup.0 {
None => None,
Some(t) => Some(t.to_string()),
},
table: tup.0.map(|t| t.to_string()),
function: None,
}),
))(i)
Expand All @@ -246,10 +243,7 @@ impl Column {
None => format!("{}", tup.0),
Some(a) => String::from(a),
},
alias: match tup.1 {
None => None,
Some(a) => Some(String::from(a)),
},
alias: tup.1.map(String::from),
table: None,
function: Some(Box::new(tup.0)),
}
Expand All @@ -262,14 +256,8 @@ impl Column {
)),
|tup| Column {
name: tup.1.to_string(),
alias: match tup.2 {
None => None,
Some(a) => Some(String::from(a)),
},
table: match tup.0 {
None => None,
Some(t) => Some(t.to_string()),
},
alias: tup.2.map(String::from),
table: tup.0.map(|t| t.to_string()),
function: None,
},
);
Expand Down Expand Up @@ -300,7 +288,7 @@ impl fmt::Display for Column {

impl From<String> for Column {
fn from(value: String) -> Self {
match value.find(".") {
match value.find('.') {
None => Column {
name: value,
alias: None,
Expand All @@ -319,7 +307,7 @@ impl From<String> for Column {

impl<'a> From<&'a str> for Column {
fn from(c: &str) -> Column {
match c.find(".") {
match c.find('.') {
None => Column {
name: String::from(c),
alias: None,
Expand Down Expand Up @@ -349,6 +337,7 @@ impl Ord for Column {
}
}

#[allow(clippy::non_canonical_partial_ord_impl)]
impl PartialOrd for Column {
fn partial_cmp(&self, other: &Column) -> Option<Ordering> {
if self.table.is_some() && other.table.is_some() {
Expand Down Expand Up @@ -473,9 +462,9 @@ impl ColumnConstraint {
})
}),
map(tuple((opt(tag("-")), digit1)), |d: (Option<&str>, &str)| {
let d_i64 = d.1.parse().unwrap();
let d_i64: i64 = d.1.parse().unwrap();
if d.0.is_some() {
Literal::Integer(-1 * d_i64)
Literal::Integer(-d_i64)
} else {
Literal::Integer(d_i64)
}
Expand Down Expand Up @@ -547,7 +536,7 @@ impl Display for ColumnPosition {
ColumnPosition::After(column) => {
let column_name = match &column.table {
Some(table) => format!("{}.{}", table, &column.name),
None => format!("{}", &column.name),
None => column.name.to_string(),
};
Ok(write!(f, "AFTER {column_name}")?)
}
Expand Down Expand Up @@ -594,7 +583,7 @@ impl ColumnSpecification {
ColumnSpecification {
column,
sql_type,
constraints: constraints.into_iter().filter_map(|m| m).collect(),
constraints: constraints.into_iter().flatten().collect(),
comment,
position,
},
Expand Down
24 changes: 12 additions & 12 deletions src/base/data_type.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,7 +110,7 @@ impl DataType {
map(preceded(tag_no_case("datetime"), opt(delim_digit)), |fsp| {
DataType::DateTime(match fsp {
Some(fsp) => Self::len_as_u16(fsp),
None => 0 as u16,
None => 0,
})
}),
map(tag_no_case("date"), |_| DataType::Date),
Expand All @@ -126,7 +126,7 @@ impl DataType {
),
multispace0,
),
|v| DataType::Enum(v),
DataType::Enum,
),
map(
tuple((
Expand Down Expand Up @@ -197,18 +197,18 @@ impl DataType {
if sign.eq_ignore_ascii_case("unsigned") {
Ok((
remaining_input,
DataType::UnsignedTinyint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::UnsignedTinyint(len.map(Self::len_as_u16).unwrap_or(1)),
))
} else {
Ok((
remaining_input,
DataType::Tinyint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::Tinyint(len.map(Self::len_as_u16).unwrap_or(1)),
))
}
}
None => Ok((
remaining_input,
DataType::Tinyint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::Tinyint(len.map(Self::len_as_u16).unwrap_or(1)),
)),
}
}
Expand All @@ -229,18 +229,18 @@ impl DataType {
if sign.eq_ignore_ascii_case("unsigned") {
Ok((
remaining_input,
DataType::UnsignedBigint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::UnsignedBigint(len.map(Self::len_as_u16).unwrap_or(1)),
))
} else {
Ok((
remaining_input,
DataType::Bigint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::Bigint(len.map(Self::len_as_u16).unwrap_or(1)),
))
}
}
None => Ok((
remaining_input,
DataType::Bigint(len.map(|l| Self::len_as_u16(l)).unwrap_or(1)),
DataType::Bigint(len.map(Self::len_as_u16).unwrap_or(1)),
)),
}
}
Expand All @@ -265,18 +265,18 @@ impl DataType {
if sign.eq_ignore_ascii_case("unsigned") {
Ok((
remaining_input,
DataType::UnsignedInt(len.map(|l| Self::len_as_u16(l)).unwrap_or(32)),
DataType::UnsignedInt(len.map(Self::len_as_u16).unwrap_or(32)),
))
} else {
Ok((
remaining_input,
DataType::Int(len.map(|l| Self::len_as_u16(l)).unwrap_or(32)),
DataType::Int(len.map(Self::len_as_u16).unwrap_or(32)),
))
}
}
None => Ok((
remaining_input,
DataType::Int(len.map(|l| Self::len_as_u16(l)).unwrap_or(32)),
DataType::Int(len.map(Self::len_as_u16).unwrap_or(32)),
)),
}
}
Expand Down Expand Up @@ -334,6 +334,6 @@ mod tests {
vec![DataType::Bool, DataType::Int(16), DataType::DateTime(16)]
);

assert!(res_not_ok.into_iter().all(|r| r == false));
assert!(res_not_ok.into_iter().all(|r| !r));
}
}
13 changes: 4 additions & 9 deletions src/base/field.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,9 @@ use common::arithmetic::ArithmeticExpression;
use common::keywords::escape_if_keyword;
use common::ws_sep_comma;

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
#[derive(Default, Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum FieldDefinitionExpression {
#[default]
All,
AllInTable(String),
Col(Column),
Expand All @@ -41,7 +42,7 @@ impl FieldDefinitionExpression {
map(LiteralExpression::parse, |lit| {
FieldDefinitionExpression::Value(FieldValueExpression::Literal(lit))
}),
map(Column::parse, |col| FieldDefinitionExpression::Col(col)),
map(Column::parse, FieldDefinitionExpression::Col),
)),
opt(ws_sep_comma),
))(i)
Expand All @@ -61,12 +62,6 @@ impl Display for FieldDefinitionExpression {
}
}

impl Default for FieldDefinitionExpression {
fn default() -> FieldDefinitionExpression {
FieldDefinitionExpression::All
}
}

#[derive(Clone, Debug, Eq, Hash, PartialEq, Serialize, Deserialize)]
pub enum FieldValueExpression {
Arithmetic(ArithmeticExpression),
Expand All @@ -78,7 +73,7 @@ impl FieldValueExpression {
alt((
map(Literal::parse, |l| {
FieldValueExpression::Literal(LiteralExpression {
value: l.into(),
value: l,
alias: None,
})
}),
Expand Down
24 changes: 12 additions & 12 deletions src/base/literal.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,11 +49,11 @@ impl Literal {
map(tuple((opt(tag("-")), digit1, tag("."), digit1)), |tup| {
Literal::FixedPoint(Real {
integral: if (tup.0).is_some() {
-1 * Self::unpack(tup.1)
-Self::unpack(tup.1)
} else {
Self::unpack(tup.1)
},
fractional: Self::unpack(tup.3) as i32,
fractional: Self::unpack(tup.3),
})
})(i)
}
Expand Down Expand Up @@ -90,7 +90,7 @@ impl Literal {
map(tag("\\Z"), |_| "\x1A"),
preceded(tag("\\"), take(1usize)),
)),
|| String::new(),
String::new,
|mut acc: String, bytes: &str| {
acc.push_str(bytes);
acc
Expand All @@ -114,7 +114,7 @@ impl Literal {
Self::raw_string_single_quoted,
Self::raw_string_double_quoted,
)),
|str| Literal::String(str),
Literal::String,
)(i)
}

Expand Down Expand Up @@ -201,13 +201,12 @@ impl ToString for Literal {
Literal::UnsignedInteger(ref i) => format!("{}", i),
Literal::FixedPoint(ref f) => format!("{}.{}", f.integral, f.fractional),
Literal::String(ref s) => format!("'{}'", s.replace('\'', "''")),
Literal::Blob(ref bv) => format!(
"{}",
bv.iter()
.map(|v| format!("{:x}", v))
.collect::<Vec<String>>()
.join(" ")
),
Literal::Blob(ref bv) => bv
.iter()
.map(|v| format!("{:x}", v))
.collect::<Vec<String>>()
.join(" ")
.to_string(),
Literal::CurrentTime => "CURRENT_TIME".to_string(),
Literal::CurrentDate => "CURRENT_DATE".to_string(),
Literal::CurrentTimestamp => "CURRENT_TIMESTAMP".to_string(),
Expand Down Expand Up @@ -266,9 +265,10 @@ mod tests {
use base::Literal;

#[test]
#[allow(clippy::redundant_slicing)]
fn literal_string_single_backslash_escape() {
let all_escaped = r#"\0\'\"\b\n\r\t\Z\\\%\_"#;
for quote in [&"'"[..], &"\""[..]].iter() {
for quote in ["'", "\""].iter() {
let quoted = &[quote, &all_escaped[..], quote].concat();
let res = Literal::string_literal(quoted);
let expected = Literal::String("\0\'\"\x7F\n\r\t\x1a\\%_".to_string());
Expand Down
20 changes: 4 additions & 16 deletions src/base/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,14 +36,8 @@ impl Table {
)),
|tup| Table {
name: String::from(tup.1),
alias: match tup.2 {
Some(a) => Some(String::from(a)),
None => None,
},
schema: match tup.0 {
Some((schema, _)) => Some(String::from(schema)),
None => None,
},
alias: tup.2.map(String::from),
schema: tup.0.map(|(schema, _)| String::from(schema)),
},
)(i)
}
Expand All @@ -52,10 +46,7 @@ impl Table {
pub fn table_reference(i: &str) -> IResult<&str, Table, ParseSQLError<&str>> {
map(pair(sql_identifier, opt(as_alias)), |tup| Table {
name: String::from(tup.0),
alias: match tup.1 {
Some(a) => Some(String::from(a)),
None => None,
},
alias: tup.1.map(String::from),
schema: None,
})(i)
}
Expand All @@ -67,10 +58,7 @@ impl Table {
|tup| Table {
name: String::from(tup.1),
alias: None,
schema: match tup.0 {
Some((schema, _)) => Some(String::from(schema)),
None => None,
},
schema: tup.0.map(|(schema, _)| String::from(schema)),
},
)(i)
}
Expand Down
5 changes: 1 addition & 4 deletions src/base/trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,7 @@ impl Trigger {
tuple((opt(pair(sql_identifier, tag("."))), sql_identifier)),
|tup| Trigger {
name: String::from(tup.1),
schema: match tup.0 {
Some((schema, _)) => Some(String::from(schema)),
None => None,
},
schema: tup.0.map(|(schema, _)| String::from(schema)),
},
)(i)
}
Expand Down
2 changes: 1 addition & 1 deletion src/common/case.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ impl CaseWhenExpression {
))(i)?;

let then_expr = ColumnOrLiteral::Column(column);
let else_expr = else_val.map(|v| ColumnOrLiteral::Literal(v));
let else_expr = else_val.map(ColumnOrLiteral::Literal);

Ok((
remaining_input,
Expand Down
Loading

0 comments on commit e1dc41b

Please sign in to comment.