diff --git a/benches/keyword.rs b/benches/keyword.rs index 9f106d5..4b9d06b 100644 --- a/benches/keyword.rs +++ b/benches/keyword.rs @@ -146,7 +146,7 @@ static VALUES: [&[u8]; 136] = [ #[bench] fn bench_keyword_token(b: &mut Bencher) { b.iter(|| { - for value in VALUES.iter() { + for value in &VALUES { assert!(keyword_token(value).is_some()) } }); diff --git a/examples/sql_tokens.rs b/examples/sql_tokens.rs index 4d49389..f4bec2d 100644 --- a/examples/sql_tokens.rs +++ b/examples/sql_tokens.rs @@ -32,7 +32,7 @@ fn main() { //TK_ID => debug_assert!(), //TK_VARIABLE => debug_assert!(), TK_BLOB => debug_assert!( - token.len() % 2 == 0 && token.iter().all(|b| b.is_ascii_hexdigit()) + token.len() % 2 == 0 && token.iter().all(u8::is_ascii_hexdigit) ), TK_INTEGER => { if token.len() > 2 @@ -50,7 +50,7 @@ fn main() { if res.is_err() { eprintln!("Err: {} in {}", res.unwrap_err(), arg); }*/ - debug_assert!(token.iter().all(|b| b.is_ascii_digit())) + debug_assert!(token.iter().all(u8::is_ascii_digit)) } } TK_FLOAT => { diff --git a/src/dialect/mod.rs b/src/dialect/mod.rs index 09b8f79..35dca88 100644 --- a/src/dialect/mod.rs +++ b/src/dialect/mod.rs @@ -48,7 +48,7 @@ pub(crate) const MAX_KEYWORD_LEN: usize = 17; pub fn keyword_token(word: &[u8]) -> Option { KEYWORDS .get(UncasedStr::new(unsafe { str::from_utf8_unchecked(word) })) - .cloned() + .copied() } pub(crate) fn is_identifier(name: &str) -> bool { diff --git a/src/lexer/scan.rs b/src/lexer/scan.rs index 5315236..d38c6f8 100644 --- a/src/lexer/scan.rs +++ b/src/lexer/scan.rs @@ -60,8 +60,8 @@ pub struct Scanner { impl Scanner { /// Constructor - pub fn new(splitter: S) -> Scanner { - Scanner { + pub fn new(splitter: S) -> Self { + Self { offset: 0, mark: (0, 0, 0), splitter, diff --git a/src/lexer/sql/error.rs b/src/lexer/sql/error.rs index 6b59c52..2ccc1a3 100644 --- a/src/lexer/sql/error.rs +++ b/src/lexer/sql/error.rs @@ -36,28 +36,28 @@ pub enum Error { impl fmt::Display for Error { fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { match *self { - Error::Io(ref err) => err.fmt(f), - Error::UnrecognizedToken(pos) => write!(f, "unrecognized token at {:?}", pos.unwrap()), - Error::UnterminatedLiteral(pos) => { + Self::Io(ref err) => err.fmt(f), + Self::UnrecognizedToken(pos) => write!(f, "unrecognized token at {:?}", pos.unwrap()), + Self::UnterminatedLiteral(pos) => { write!(f, "non-terminated literal at {:?}", pos.unwrap()) } - Error::UnterminatedBracket(pos) => { + Self::UnterminatedBracket(pos) => { write!(f, "non-terminated bracket at {:?}", pos.unwrap()) } - Error::UnterminatedBlockComment(pos) => { + Self::UnterminatedBlockComment(pos) => { write!(f, "non-terminated block comment at {:?}", pos.unwrap()) } - Error::BadVariableName(pos) => write!(f, "bad variable name at {:?}", pos.unwrap()), - Error::BadNumber(pos) => write!(f, "bad number at {:?}", pos.unwrap()), - Error::ExpectedEqualsSign(pos) => write!(f, "expected = sign at {:?}", pos.unwrap()), - Error::MalformedBlobLiteral(pos) => { + Self::BadVariableName(pos) => write!(f, "bad variable name at {:?}", pos.unwrap()), + Self::BadNumber(pos) => write!(f, "bad number at {:?}", pos.unwrap()), + Self::ExpectedEqualsSign(pos) => write!(f, "expected = sign at {:?}", pos.unwrap()), + Self::MalformedBlobLiteral(pos) => { write!(f, "malformed blob literal at {:?}", pos.unwrap()) } - Error::MalformedHexInteger(pos) => { + Self::MalformedHexInteger(pos) => { write!(f, "malformed hex integer at {:?}", pos.unwrap()) } - Error::ParserError(ref msg, Some(pos)) => write!(f, "{} at {:?}", msg, pos), - Error::ParserError(ref msg, _) => write!(f, "{}", msg), + Self::ParserError(ref msg, Some(pos)) => write!(f, "{msg} at {pos:?}"), + Self::ParserError(ref msg, _) => write!(f, "{msg}"), } } } @@ -65,31 +65,31 @@ impl fmt::Display for Error { impl error::Error for Error {} impl From for Error { - fn from(err: io::Error) -> Error { - Error::Io(err) + fn from(err: io::Error) -> Self { + Self::Io(err) } } impl From for Error { - fn from(err: ParserError) -> Error { - Error::ParserError(err, None) + fn from(err: ParserError) -> Self { + Self::ParserError(err, None) } } impl ScanError for Error { fn position(&mut self, line: u64, column: usize) { match *self { - Error::Io(_) => {} - Error::UnrecognizedToken(ref mut pos) => *pos = Some((line, column)), - Error::UnterminatedLiteral(ref mut pos) => *pos = Some((line, column)), - Error::UnterminatedBracket(ref mut pos) => *pos = Some((line, column)), - Error::UnterminatedBlockComment(ref mut pos) => *pos = Some((line, column)), - Error::BadVariableName(ref mut pos) => *pos = Some((line, column)), - Error::BadNumber(ref mut pos) => *pos = Some((line, column)), - Error::ExpectedEqualsSign(ref mut pos) => *pos = Some((line, column)), - Error::MalformedBlobLiteral(ref mut pos) => *pos = Some((line, column)), - Error::MalformedHexInteger(ref mut pos) => *pos = Some((line, column)), - Error::ParserError(_, ref mut pos) => *pos = Some((line, column)), + Self::Io(_) => {} + Self::UnrecognizedToken(ref mut pos) => *pos = Some((line, column)), + Self::UnterminatedLiteral(ref mut pos) => *pos = Some((line, column)), + Self::UnterminatedBracket(ref mut pos) => *pos = Some((line, column)), + Self::UnterminatedBlockComment(ref mut pos) => *pos = Some((line, column)), + Self::BadVariableName(ref mut pos) => *pos = Some((line, column)), + Self::BadNumber(ref mut pos) => *pos = Some((line, column)), + Self::ExpectedEqualsSign(ref mut pos) => *pos = Some((line, column)), + Self::MalformedBlobLiteral(ref mut pos) => *pos = Some((line, column)), + Self::MalformedHexInteger(ref mut pos) => *pos = Some((line, column)), + Self::ParserError(_, ref mut pos) => *pos = Some((line, column)), } } } diff --git a/src/lexer/sql/mod.rs b/src/lexer/sql/mod.rs index 7db9bcd..0eb1543 100644 --- a/src/lexer/sql/mod.rs +++ b/src/lexer/sql/mod.rs @@ -33,7 +33,7 @@ pub struct Parser<'input> { impl<'input> Parser<'input> { /// Constructor - pub fn new(input: &'input [u8]) -> Parser<'input> { + pub fn new(input: &'input [u8]) -> Self { let lexer = Tokenizer::new(); let scanner = Scanner::new(lexer); let ctx = Context::new(input); @@ -253,8 +253,8 @@ pub struct Tokenizer {} impl Tokenizer { /// Constructor - pub fn new() -> Tokenizer { - Tokenizer {} + pub fn new() -> Self { + Self {} } } @@ -401,7 +401,7 @@ impl Splitter for Tokenizer { b',' => Ok((Some((&data[..1], TK_COMMA)), 1)), b'&' => Ok((Some((&data[..1], TK_BITAND)), 1)), b'~' => Ok((Some((&data[..1], TK_BITNOT)), 1)), - quote @ b'`' | quote @ b'\'' | quote @ b'"' => literal(data, quote), + quote @ (b'`' | b'\'' | b'"') => literal(data, quote), b'.' => { if let Some(b) = data.get(1) { if b.is_ascii_digit() { @@ -417,7 +417,7 @@ impl Splitter for Tokenizer { b'[' => { if let Some(i) = memchr(b']', data) { // Keep original quotes / '[' ... ’]' - Ok((Some((&data[0..i + 1], TK_ID)), i + 1)) + Ok((Some((&data[0..=i], TK_ID)), i + 1)) } else { Err(Error::UnterminatedBracket(None)) } diff --git a/src/lexer/sql/test.rs b/src/lexer/sql/test.rs index f40b4d0..02f6e2d 100644 --- a/src/lexer/sql/test.rs +++ b/src/lexer/sql/test.rs @@ -96,7 +96,7 @@ fn vtab_args() -> Result<(), Error> { #[test] fn only_semicolons_no_statements() { let sqls = ["", ";", ";;;"]; - for sql in sqls.iter() { + for sql in &sqls { let r = parse(sql.as_bytes()); assert_eq!(r.unwrap(), None); } @@ -110,7 +110,7 @@ fn extra_semicolons_between_statements() { "; SELECT 1; SELECT 2", ";; SELECT 1;; SELECT 2;;", ]; - for sql in sqls.iter() { + for sql in &sqls { let mut parser = Parser::new(sql.as_bytes()); assert!(matches!( parser.next().unwrap(), @@ -132,7 +132,7 @@ fn extra_comments_between_statements() { "/* abc */; SELECT 1 /* def */; SELECT 2 /* ghj */", "/* abc */;; SELECT 1;/* def */; SELECT 2; /* ghj */; /* klm */", ]; - for sql in sqls.iter() { + for sql in &sqls { let mut parser = Parser::new(sql.as_bytes()); assert!(matches!( parser.next().unwrap(), diff --git a/src/parser/ast/check.rs b/src/parser/ast/check.rs index 954bb27..11a3cb0 100644 --- a/src/parser/ast/check.rs +++ b/src/parser/ast/check.rs @@ -7,22 +7,22 @@ impl Cmd { /// Statement accessor pub fn stmt(&self) -> &Stmt { match self { - Cmd::Explain(stmt) => stmt, - Cmd::ExplainQueryPlan(stmt) => stmt, - Cmd::Stmt(stmt) => stmt, + Self::Explain(stmt) => stmt, + Self::ExplainQueryPlan(stmt) => stmt, + Self::Stmt(stmt) => stmt, } } /// Like `sqlite3_column_count` but more limited pub fn column_count(&self) -> ColumnCount { match self { - Cmd::Explain(_) => ColumnCount::Fixed(8), - Cmd::ExplainQueryPlan(_) => ColumnCount::Fixed(4), - Cmd::Stmt(stmt) => stmt.column_count(), + Self::Explain(_) => ColumnCount::Fixed(8), + Self::ExplainQueryPlan(_) => ColumnCount::Fixed(4), + Self::Stmt(stmt) => stmt.column_count(), } } /// Like `sqlite3_stmt_isexplain` pub fn is_explain(&self) -> bool { - matches!(self, Cmd::Explain(_) | Cmd::ExplainQueryPlan(_)) + matches!(self, Self::Explain(_) | Self::ExplainQueryPlan(_)) } /// Like `sqlite3_stmt_readonly` pub fn readonly(&self) -> bool { @@ -46,7 +46,7 @@ pub enum ColumnCount { impl ColumnCount { fn incr(&mut self) { - if let ColumnCount::Fixed(n) = self { + if let Self::Fixed(n) = self { *n += 1; } } @@ -56,17 +56,17 @@ impl Stmt { /// Like `sqlite3_column_count` but more limited pub fn column_count(&self) -> ColumnCount { match self { - Stmt::Delete { + Self::Delete { returning: Some(returning), .. } => column_count(returning), - Stmt::Insert { + Self::Insert { returning: Some(returning), .. } => column_count(returning), - Stmt::Pragma(..) => ColumnCount::Dynamic, - Stmt::Select(s) => s.column_count(), - Stmt::Update { + Self::Pragma(..) => ColumnCount::Dynamic, + Self::Select(s) => s.column_count(), + Self::Update { returning: Some(returning), .. } => column_count(returning), @@ -77,16 +77,16 @@ impl Stmt { /// Like `sqlite3_stmt_readonly` pub fn readonly(&self) -> bool { match self { - Stmt::Attach { .. } => true, - Stmt::Begin(..) => true, - Stmt::Commit(..) => true, - Stmt::Detach(..) => true, - Stmt::Pragma(..) => true, // TODO check all - Stmt::Reindex { .. } => true, - Stmt::Release(..) => true, - Stmt::Rollback { .. } => true, - Stmt::Savepoint(..) => true, - Stmt::Select(..) => true, + Self::Attach { .. } => true, + Self::Begin(..) => true, + Self::Commit(..) => true, + Self::Detach(..) => true, + Self::Pragma(..) => true, // TODO check all + Self::Reindex { .. } => true, + Self::Release(..) => true, + Self::Rollback { .. } => true, + Self::Savepoint(..) => true, + Self::Select(..) => true, _ => false, } } @@ -94,7 +94,7 @@ impl Stmt { /// check for extra rules pub fn check(&self) -> Result<(), ParserError> { match self { - Stmt::AlterTable(old_name, AlterTableBody::RenameTo(new_name)) => { + Self::AlterTable(old_name, AlterTableBody::RenameTo(new_name)) => { if *new_name == old_name.name { return Err(custom_err!( "there is already another table or index with this name: {}", @@ -103,7 +103,7 @@ impl Stmt { } Ok(()) } - Stmt::AlterTable(.., AlterTableBody::AddColumn(cd)) => { + Self::AlterTable(.., AlterTableBody::AddColumn(cd)) => { for c in cd { if let ColumnConstraint::PrimaryKey { .. } = c { return Err(custom_err!("Cannot add a PRIMARY KEY column")); @@ -113,7 +113,7 @@ impl Stmt { } Ok(()) } - Stmt::CreateTable { + Self::CreateTable { temporary, tbl_name, body, @@ -128,7 +128,7 @@ impl Stmt { } body.check(tbl_name) } - Stmt::CreateView { + Self::CreateView { view_name, columns: Some(columns), select, @@ -153,12 +153,12 @@ impl Stmt { _ => Ok(()), } } - Stmt::Delete { + Self::Delete { order_by: Some(_), limit: None, .. } => Err(custom_err!("ORDER BY without LIMIT on DELETE")), - Stmt::Insert { + Self::Insert { columns: Some(columns), body: InsertBody::Select(select, ..), .. @@ -168,12 +168,12 @@ impl Stmt { } _ => Ok(()), }, - Stmt::Insert { + Self::Insert { columns: Some(columns), body: InsertBody::DefaultValues, .. } => Err(custom_err!("0 values for {} columns", columns.len())), - Stmt::Update { + Self::Update { order_by: Some(_), limit: None, .. @@ -186,7 +186,7 @@ impl Stmt { impl CreateTableBody { /// check for extra rules pub fn check(&self, tbl_name: &QualifiedName) -> Result<(), ParserError> { - if let CreateTableBody::ColumnsAndConstraints { + if let Self::ColumnsAndConstraints { columns, constraints: _, options, @@ -194,7 +194,7 @@ impl CreateTableBody { { let mut generated_count = 0; for c in columns.values() { - for cs in c.constraints.iter() { + for cs in &c.constraints { if let ColumnConstraint::Generated { .. } = cs.constraint { generated_count += 1; } @@ -244,7 +244,7 @@ impl CreateTableBody { /// explicit primary key constraint ? pub fn has_primary_key(&self) -> bool { - if let CreateTableBody::ColumnsAndConstraints { + if let Self::ColumnsAndConstraints { columns, constraints, .. @@ -292,8 +292,8 @@ impl OneSelect { /// Like `sqlite3_column_count` but more limited pub fn column_count(&self) -> ColumnCount { match self { - OneSelect::Select { columns, .. } => column_count(columns), - OneSelect::Values(values) => { + Self::Select { columns, .. } => column_count(columns), + Self::Values(values) => { assert!(!values.is_empty()); // TODO Validate ColumnCount::Fixed(values[0].len()) } @@ -318,7 +318,7 @@ impl Display for QualifiedName { impl ResultColumn { fn column_count(&self) -> ColumnCount { match self { - ResultColumn::Expr(..) => ColumnCount::Fixed(1), + Self::Expr(..) => ColumnCount::Fixed(1), _ => ColumnCount::Dynamic, } } diff --git a/src/parser/ast/fmt.rs b/src/parser/ast/fmt.rs index 350e739..614ad7d 100644 --- a/src/parser/ast/fmt.rs +++ b/src/parser/ast/fmt.rs @@ -92,17 +92,17 @@ impl Display for dyn ToTokens { impl ToTokens for Cmd { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Cmd::Explain(stmt) => { + Self::Explain(stmt) => { s.append(TK_EXPLAIN, None)?; stmt.to_tokens(s)?; } - Cmd::ExplainQueryPlan(stmt) => { + Self::ExplainQueryPlan(stmt) => { s.append(TK_EXPLAIN, None)?; s.append(TK_QUERY, None)?; s.append(TK_PLAN, None)?; stmt.to_tokens(s)?; } - Cmd::Stmt(stmt) => { + Self::Stmt(stmt) => { stmt.to_tokens(s)?; } } @@ -119,20 +119,20 @@ impl Display for Cmd { impl ToTokens for Stmt { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Stmt::AlterTable(tbl_name, body) => { + Self::AlterTable(tbl_name, body) => { s.append(TK_ALTER, None)?; s.append(TK_TABLE, None)?; tbl_name.to_tokens(s)?; body.to_tokens(s) } - Stmt::Analyze(obj_name) => { + Self::Analyze(obj_name) => { s.append(TK_ANALYZE, None)?; if let Some(obj_name) = obj_name { obj_name.to_tokens(s)?; } Ok(()) } - Stmt::Attach { expr, db_name, key } => { + Self::Attach { expr, db_name, key } => { s.append(TK_ATTACH, None)?; expr.to_tokens(s)?; s.append(TK_AS, None)?; @@ -143,7 +143,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Begin(tx_type, tx_name) => { + Self::Begin(tx_type, tx_name) => { s.append(TK_BEGIN, None)?; if let Some(tx_type) = tx_type { tx_type.to_tokens(s)?; @@ -154,7 +154,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Commit(tx_name) => { + Self::Commit(tx_name) => { s.append(TK_COMMIT, None)?; if let Some(tx_name) = tx_name { s.append(TK_TRANSACTION, None)?; @@ -162,7 +162,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::CreateIndex { + Self::CreateIndex { unique, if_not_exists, idx_name, @@ -192,7 +192,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::CreateTable { + Self::CreateTable { temporary, if_not_exists, tbl_name, @@ -211,7 +211,7 @@ impl ToTokens for Stmt { tbl_name.to_tokens(s)?; body.to_tokens(s) } - Stmt::CreateTrigger { + Self::CreateTrigger { temporary, if_not_exists, trigger_name, @@ -255,7 +255,7 @@ impl ToTokens for Stmt { } s.append(TK_END, None) } - Stmt::CreateView { + Self::CreateView { temporary, if_not_exists, view_name, @@ -281,7 +281,7 @@ impl ToTokens for Stmt { s.append(TK_AS, None)?; select.to_tokens(s) } - Stmt::CreateVirtualTable { + Self::CreateVirtualTable { if_not_exists, tbl_name, module_name, @@ -304,7 +304,7 @@ impl ToTokens for Stmt { } s.append(TK_RP, None) } - Stmt::Delete { + Self::Delete { with, tbl_name, indexed, @@ -340,11 +340,11 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Detach(expr) => { + Self::Detach(expr) => { s.append(TK_DETACH, None)?; expr.to_tokens(s) } - Stmt::DropIndex { + Self::DropIndex { if_exists, idx_name, } => { @@ -356,7 +356,7 @@ impl ToTokens for Stmt { } idx_name.to_tokens(s) } - Stmt::DropTable { + Self::DropTable { if_exists, tbl_name, } => { @@ -368,7 +368,7 @@ impl ToTokens for Stmt { } tbl_name.to_tokens(s) } - Stmt::DropTrigger { + Self::DropTrigger { if_exists, trigger_name, } => { @@ -380,7 +380,7 @@ impl ToTokens for Stmt { } trigger_name.to_tokens(s) } - Stmt::DropView { + Self::DropView { if_exists, view_name, } => { @@ -392,7 +392,7 @@ impl ToTokens for Stmt { } view_name.to_tokens(s) } - Stmt::Insert { + Self::Insert { with, or_conflict, tbl_name, @@ -426,7 +426,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Pragma(name, value) => { + Self::Pragma(name, value) => { s.append(TK_PRAGMA, None)?; name.to_tokens(s)?; if let Some(value) = value { @@ -434,18 +434,18 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Reindex { obj_name } => { + Self::Reindex { obj_name } => { s.append(TK_REINDEX, None)?; if let Some(obj_name) = obj_name { obj_name.to_tokens(s)?; } Ok(()) } - Stmt::Release(name) => { + Self::Release(name) => { s.append(TK_RELEASE, None)?; name.to_tokens(s) } - Stmt::Rollback { + Self::Rollback { tx_name, savepoint_name, } => { @@ -460,12 +460,12 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Savepoint(name) => { + Self::Savepoint(name) => { s.append(TK_SAVEPOINT, None)?; name.to_tokens(s) } - Stmt::Select(select) => select.to_tokens(s), - Stmt::Update { + Self::Select(select) => select.to_tokens(s), + Self::Update { with, or_conflict, tbl_name, @@ -513,7 +513,7 @@ impl ToTokens for Stmt { } Ok(()) } - Stmt::Vacuum(name, expr) => { + Self::Vacuum(name, expr) => { s.append(TK_VACUUM, None)?; if let Some(ref name) = name { name.to_tokens(s)?; @@ -531,7 +531,7 @@ impl ToTokens for Stmt { impl ToTokens for Expr { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Expr::Between { + Self::Between { lhs, not, start, @@ -546,12 +546,12 @@ impl ToTokens for Expr { s.append(TK_AND, None)?; end.to_tokens(s) } - Expr::Binary(lhs, op, rhs) => { + Self::Binary(lhs, op, rhs) => { lhs.to_tokens(s)?; op.to_tokens(s)?; rhs.to_tokens(s) } - Expr::Case { + Self::Case { base, when_then_pairs, else_expr, @@ -572,7 +572,7 @@ impl ToTokens for Expr { } s.append(TK_END, None) } - Expr::Cast { expr, type_name } => { + Self::Cast { expr, type_name } => { s.append(TK_CAST, None)?; s.append(TK_LP, None)?; expr.to_tokens(s)?; @@ -582,25 +582,25 @@ impl ToTokens for Expr { } s.append(TK_RP, None) } - Expr::Collate(expr, collation) => { + Self::Collate(expr, collation) => { expr.to_tokens(s)?; s.append(TK_COLLATE, None)?; double_quote(collation, s) } - Expr::DoublyQualified(db_name, tbl_name, col_name) => { + Self::DoublyQualified(db_name, tbl_name, col_name) => { db_name.to_tokens(s)?; s.append(TK_DOT, None)?; tbl_name.to_tokens(s)?; s.append(TK_DOT, None)?; col_name.to_tokens(s) } - Expr::Exists(subquery) => { + Self::Exists(subquery) => { s.append(TK_EXISTS, None)?; s.append(TK_LP, None)?; subquery.to_tokens(s)?; s.append(TK_RP, None) } - Expr::FunctionCall { + Self::FunctionCall { name, distinctness, args, @@ -626,7 +626,7 @@ impl ToTokens for Expr { } Ok(()) } - Expr::FunctionCallStar { name, filter_over } => { + Self::FunctionCallStar { name, filter_over } => { name.to_tokens(s)?; s.append(TK_LP, None)?; s.append(TK_STAR, None)?; @@ -636,8 +636,8 @@ impl ToTokens for Expr { } Ok(()) } - Expr::Id(id) => id.to_tokens(s), - Expr::InList { lhs, not, rhs } => { + Self::Id(id) => id.to_tokens(s), + Self::InList { lhs, not, rhs } => { lhs.to_tokens(s)?; if *not { s.append(TK_NOT, None)?; @@ -649,7 +649,7 @@ impl ToTokens for Expr { } s.append(TK_RP, None) } - Expr::InSelect { lhs, not, rhs } => { + Self::InSelect { lhs, not, rhs } => { lhs.to_tokens(s)?; if *not { s.append(TK_NOT, None)?; @@ -659,7 +659,7 @@ impl ToTokens for Expr { rhs.to_tokens(s)?; s.append(TK_RP, None) } - Expr::InTable { + Self::InTable { lhs, not, rhs, @@ -678,11 +678,11 @@ impl ToTokens for Expr { } Ok(()) } - Expr::IsNull(sub_expr) => { + Self::IsNull(sub_expr) => { sub_expr.to_tokens(s)?; s.append(TK_ISNULL, None) } - Expr::Like { + Self::Like { lhs, not, op, @@ -701,23 +701,23 @@ impl ToTokens for Expr { } Ok(()) } - Expr::Literal(lit) => lit.to_tokens(s), - Expr::Name(name) => name.to_tokens(s), - Expr::NotNull(sub_expr) => { + Self::Literal(lit) => lit.to_tokens(s), + Self::Name(name) => name.to_tokens(s), + Self::NotNull(sub_expr) => { sub_expr.to_tokens(s)?; s.append(TK_NOTNULL, None) } - Expr::Parenthesized(exprs) => { + Self::Parenthesized(exprs) => { s.append(TK_LP, None)?; comma(exprs, s)?; s.append(TK_RP, None) } - Expr::Qualified(qualifier, qualified) => { + Self::Qualified(qualifier, qualified) => { qualifier.to_tokens(s)?; s.append(TK_DOT, None)?; qualified.to_tokens(s) } - Expr::Raise(rt, err) => { + Self::Raise(rt, err) => { s.append(TK_RAISE, None)?; s.append(TK_LP, None)?; rt.to_tokens(s)?; @@ -727,16 +727,16 @@ impl ToTokens for Expr { } s.append(TK_RP, None) } - Expr::Subquery(query) => { + Self::Subquery(query) => { s.append(TK_LP, None)?; query.to_tokens(s)?; s.append(TK_RP, None) } - Expr::Unary(op, sub_expr) => { + Self::Unary(op, sub_expr) => { op.to_tokens(s)?; sub_expr.to_tokens(s) } - Expr::Variable(var) => match var.chars().next() { + Self::Variable(var) => match var.chars().next() { Some(c) if c == '$' || c == '@' || c == '#' || c == ':' => { s.append(TK_VARIABLE, Some(var)) } @@ -756,14 +756,14 @@ impl Display for Expr { impl ToTokens for Literal { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Literal::Numeric(ref num) => s.append(TK_FLOAT, Some(num)), // TODO Validate TK_FLOAT - Literal::String(ref str) => s.append(TK_STRING, Some(str)), - Literal::Blob(ref blob) => s.append(TK_BLOB, Some(blob)), - Literal::Keyword(ref str) => s.append(TK_ID, Some(str)), // TODO Validate TK_ID - Literal::Null => s.append(TK_NULL, None), - Literal::CurrentDate => s.append(TK_CTIME_KW, Some("CURRENT_DATE")), - Literal::CurrentTime => s.append(TK_CTIME_KW, Some("CURRENT_TIME")), - Literal::CurrentTimestamp => s.append(TK_CTIME_KW, Some("CURRENT_TIMESTAMP")), + Self::Numeric(ref num) => s.append(TK_FLOAT, Some(num)), // TODO Validate TK_FLOAT + Self::String(ref str) => s.append(TK_STRING, Some(str)), + Self::Blob(ref blob) => s.append(TK_BLOB, Some(blob)), + Self::Keyword(ref str) => s.append(TK_ID, Some(str)), // TODO Validate TK_ID + Self::Null => s.append(TK_NULL, None), + Self::CurrentDate => s.append(TK_CTIME_KW, Some("CURRENT_DATE")), + Self::CurrentTime => s.append(TK_CTIME_KW, Some("CURRENT_TIME")), + Self::CurrentTimestamp => s.append(TK_CTIME_KW, Some("CURRENT_TIMESTAMP")), } } } @@ -773,10 +773,10 @@ impl ToTokens for LikeOperator { s.append( TK_LIKE_KW, Some(match self { - LikeOperator::Glob => "GLOB", - LikeOperator::Like => "LIKE", - LikeOperator::Match => "MATCH", - LikeOperator::Regexp => "REGEXP", + Self::Glob => "GLOB", + Self::Like => "LIKE", + Self::Match => "MATCH", + Self::Regexp => "REGEXP", }), ) } @@ -785,31 +785,31 @@ impl ToTokens for LikeOperator { impl ToTokens for Operator { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Operator::Add => s.append(TK_PLUS, None), - Operator::And => s.append(TK_AND, None), - Operator::ArrowRight => s.append(TK_PTR, Some("->")), - Operator::ArrowRightShift => s.append(TK_PTR, Some("->>")), - Operator::BitwiseAnd => s.append(TK_BITAND, None), - Operator::BitwiseOr => s.append(TK_BITOR, None), - Operator::Concat => s.append(TK_CONCAT, None), - Operator::Equals => s.append(TK_EQ, None), - Operator::Divide => s.append(TK_SLASH, None), - Operator::Greater => s.append(TK_GT, None), - Operator::GreaterEquals => s.append(TK_GE, None), - Operator::Is => s.append(TK_IS, None), - Operator::IsNot => { + Self::Add => s.append(TK_PLUS, None), + Self::And => s.append(TK_AND, None), + Self::ArrowRight => s.append(TK_PTR, Some("->")), + Self::ArrowRightShift => s.append(TK_PTR, Some("->>")), + Self::BitwiseAnd => s.append(TK_BITAND, None), + Self::BitwiseOr => s.append(TK_BITOR, None), + Self::Concat => s.append(TK_CONCAT, None), + Self::Equals => s.append(TK_EQ, None), + Self::Divide => s.append(TK_SLASH, None), + Self::Greater => s.append(TK_GT, None), + Self::GreaterEquals => s.append(TK_GE, None), + Self::Is => s.append(TK_IS, None), + Self::IsNot => { s.append(TK_IS, None)?; s.append(TK_NOT, None) } - Operator::LeftShift => s.append(TK_LSHIFT, None), - Operator::Less => s.append(TK_LT, None), - Operator::LessEquals => s.append(TK_LE, None), - Operator::Modulus => s.append(TK_REM, None), - Operator::Multiply => s.append(TK_STAR, None), - Operator::NotEquals => s.append(TK_NE, None), - Operator::Or => s.append(TK_OR, None), - Operator::RightShift => s.append(TK_RSHIFT, None), - Operator::Substract => s.append(TK_MINUS, None), + Self::LeftShift => s.append(TK_LSHIFT, None), + Self::Less => s.append(TK_LT, None), + Self::LessEquals => s.append(TK_LE, None), + Self::Modulus => s.append(TK_REM, None), + Self::Multiply => s.append(TK_STAR, None), + Self::NotEquals => s.append(TK_NE, None), + Self::Or => s.append(TK_OR, None), + Self::RightShift => s.append(TK_RSHIFT, None), + Self::Substract => s.append(TK_MINUS, None), } } } @@ -818,10 +818,10 @@ impl ToTokens for UnaryOperator { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - UnaryOperator::BitwiseNot => TK_BITNOT, - UnaryOperator::Negative => TK_MINUS, - UnaryOperator::Not => TK_NOT, - UnaryOperator::Positive => TK_PLUS, + Self::BitwiseNot => TK_BITNOT, + Self::Negative => TK_MINUS, + Self::Not => TK_NOT, + Self::Positive => TK_PLUS, }, None, ) @@ -868,13 +868,13 @@ impl ToTokens for CompoundSelect { impl ToTokens for CompoundOperator { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - CompoundOperator::Union => s.append(TK_UNION, None), - CompoundOperator::UnionAll => { + Self::Union => s.append(TK_UNION, None), + Self::UnionAll => { s.append(TK_UNION, None)?; s.append(TK_ALL, None) } - CompoundOperator::Except => s.append(TK_EXCEPT, None), - CompoundOperator::Intersect => s.append(TK_INTERSECT, None), + Self::Except => s.append(TK_EXCEPT, None), + Self::Intersect => s.append(TK_INTERSECT, None), } } } @@ -888,7 +888,7 @@ impl Display for CompoundOperator { impl ToTokens for OneSelect { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - OneSelect::Select { + Self::Select { distinctness, columns, from, @@ -918,7 +918,7 @@ impl ToTokens for OneSelect { } Ok(()) } - OneSelect::Values(values) => { + Self::Values(values) => { for (i, vals) in values.iter().enumerate() { if i == 0 { s.append(TK_VALUES, None)?; @@ -951,8 +951,8 @@ impl ToTokens for Distinctness { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - Distinctness::Distinct => TK_DISTINCT, - Distinctness::All => TK_ALL, + Self::Distinct => TK_DISTINCT, + Self::All => TK_ALL, }, None, ) @@ -962,15 +962,15 @@ impl ToTokens for Distinctness { impl ToTokens for ResultColumn { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - ResultColumn::Expr(expr, alias) => { + Self::Expr(expr, alias) => { expr.to_tokens(s)?; if let Some(alias) = alias { alias.to_tokens(s)?; } Ok(()) } - ResultColumn::Star => s.append(TK_STAR, None), - ResultColumn::TableStar(tbl_name) => { + Self::Star => s.append(TK_STAR, None), + Self::TableStar(tbl_name) => { tbl_name.to_tokens(s)?; s.append(TK_DOT, None)?; s.append(TK_STAR, None) @@ -982,11 +982,11 @@ impl ToTokens for ResultColumn { impl ToTokens for As { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - As::As(ref name) => { + Self::As(ref name) => { s.append(TK_AS, None)?; name.to_tokens(s) } - As::Elided(ref name) => name.to_tokens(s), + Self::Elided(ref name) => name.to_tokens(s), } } } @@ -1005,7 +1005,7 @@ impl ToTokens for JoinedSelectTable { impl ToTokens for SelectTable { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - SelectTable::Table(name, alias, indexed) => { + Self::Table(name, alias, indexed) => { name.to_tokens(s)?; if let Some(alias) = alias { alias.to_tokens(s)?; @@ -1015,7 +1015,7 @@ impl ToTokens for SelectTable { } Ok(()) } - SelectTable::TableCall(name, exprs, alias) => { + Self::TableCall(name, exprs, alias) => { name.to_tokens(s)?; s.append(TK_LP, None)?; if let Some(exprs) = exprs { @@ -1027,7 +1027,7 @@ impl ToTokens for SelectTable { } Ok(()) } - SelectTable::Select(select, alias) => { + Self::Select(select, alias) => { s.append(TK_LP, None)?; select.to_tokens(s)?; s.append(TK_RP, None)?; @@ -1036,7 +1036,7 @@ impl ToTokens for SelectTable { } Ok(()) } - SelectTable::Sub(from, alias) => { + Self::Sub(from, alias) => { s.append(TK_LP, None)?; from.to_tokens(s)?; s.append(TK_RP, None)?; @@ -1052,8 +1052,8 @@ impl ToTokens for SelectTable { impl ToTokens for JoinOperator { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - JoinOperator::Comma => s.append(TK_COMMA, None), - JoinOperator::TypedJoin(join_type) => { + Self::Comma => s.append(TK_COMMA, None), + Self::TypedJoin(join_type) => { if let Some(ref join_type) = join_type { join_type.to_tokens(s)?; } @@ -1065,25 +1065,25 @@ impl ToTokens for JoinOperator { impl ToTokens for JoinType { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { - if self.contains(JoinType::NATURAL) { + if self.contains(Self::NATURAL) { s.append(TK_JOIN_KW, Some("NATURAL"))?; } - if self.contains(JoinType::INNER) { - if self.contains(JoinType::CROSS) { + if self.contains(Self::INNER) { + if self.contains(Self::CROSS) { s.append(TK_JOIN_KW, Some("CROSS"))?; } s.append(TK_JOIN_KW, Some("INNER"))?; } else { - if self.contains(JoinType::LEFT) { - if self.contains(JoinType::RIGHT) { + if self.contains(Self::LEFT) { + if self.contains(Self::RIGHT) { s.append(TK_JOIN_KW, Some("FULL"))?; } else { s.append(TK_JOIN_KW, Some("LEFT"))?; } - } else if self.contains(JoinType::RIGHT) { + } else if self.contains(Self::RIGHT) { s.append(TK_JOIN_KW, Some("RIGHT"))?; } - if self.contains(JoinType::OUTER) { + if self.contains(Self::OUTER) { s.append(TK_JOIN_KW, Some("OUTER"))?; } } @@ -1094,11 +1094,11 @@ impl ToTokens for JoinType { impl ToTokens for JoinConstraint { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - JoinConstraint::On(expr) => { + Self::On(expr) => { s.append(TK_ON, None)?; expr.to_tokens(s) } - JoinConstraint::Using(col_names) => { + Self::Using(col_names) => { s.append(TK_USING, None)?; s.append(TK_LP, None)?; comma(col_names.deref(), s)?; @@ -1157,23 +1157,23 @@ impl ToTokens for QualifiedName { impl ToTokens for AlterTableBody { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - AlterTableBody::RenameTo(name) => { + Self::RenameTo(name) => { s.append(TK_RENAME, None)?; s.append(TK_TO, None)?; name.to_tokens(s) } - AlterTableBody::AddColumn(def) => { + Self::AddColumn(def) => { s.append(TK_ADD, None)?; s.append(TK_COLUMNKW, None)?; def.to_tokens(s) } - AlterTableBody::RenameColumn { old, new } => { + Self::RenameColumn { old, new } => { s.append(TK_RENAME, None)?; old.to_tokens(s)?; s.append(TK_TO, None)?; new.to_tokens(s) } - AlterTableBody::DropColumn(name) => { + Self::DropColumn(name) => { s.append(TK_DROP, None)?; s.append(TK_COLUMNKW, None)?; name.to_tokens(s) @@ -1185,7 +1185,7 @@ impl ToTokens for AlterTableBody { impl ToTokens for CreateTableBody { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - CreateTableBody::ColumnsAndConstraints { + Self::ColumnsAndConstraints { columns, constraints, options, @@ -1206,7 +1206,7 @@ impl ToTokens for CreateTableBody { } Ok(()) } - CreateTableBody::AsSelect(select) => { + Self::AsSelect(select) => { s.append(TK_AS, None)?; select.to_tokens(s) } @@ -1220,7 +1220,7 @@ impl ToTokens for ColumnDefinition { if let Some(ref col_type) = self.col_type { col_type.to_tokens(s)?; } - for constraint in self.constraints.iter() { + for constraint in &self.constraints { constraint.to_tokens(s)?; } Ok(()) @@ -1240,7 +1240,7 @@ impl ToTokens for NamedColumnConstraint { impl ToTokens for ColumnConstraint { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - ColumnConstraint::PrimaryKey { + Self::PrimaryKey { order, conflict_clause, auto_increment, @@ -1260,7 +1260,7 @@ impl ToTokens for ColumnConstraint { } Ok(()) } - ColumnConstraint::NotNull { + Self::NotNull { nullable, conflict_clause, } => { @@ -1275,7 +1275,7 @@ impl ToTokens for ColumnConstraint { } Ok(()) } - ColumnConstraint::Unique(conflict_clause) => { + Self::Unique(conflict_clause) => { s.append(TK_UNIQUE, None)?; if let Some(conflict_clause) = conflict_clause { s.append(TK_ON, None)?; @@ -1284,22 +1284,22 @@ impl ToTokens for ColumnConstraint { } Ok(()) } - ColumnConstraint::Check(expr) => { + Self::Check(expr) => { s.append(TK_CHECK, None)?; s.append(TK_LP, None)?; expr.to_tokens(s)?; s.append(TK_RP, None) } - ColumnConstraint::Default(expr) => { + Self::Default(expr) => { s.append(TK_DEFAULT, None)?; expr.to_tokens(s) } - ColumnConstraint::Defer(deref_clause) => deref_clause.to_tokens(s), - ColumnConstraint::Collate { collation_name } => { + Self::Defer(deref_clause) => deref_clause.to_tokens(s), + Self::Collate { collation_name } => { s.append(TK_COLLATE, None)?; collation_name.to_tokens(s) } - ColumnConstraint::ForeignKey { + Self::ForeignKey { clause, deref_clause, } => { @@ -1310,7 +1310,7 @@ impl ToTokens for ColumnConstraint { } Ok(()) } - ColumnConstraint::Generated { expr, typ } => { + Self::Generated { expr, typ } => { s.append(TK_AS, None)?; s.append(TK_LP, None)?; expr.to_tokens(s)?; @@ -1337,7 +1337,7 @@ impl ToTokens for NamedTableConstraint { impl ToTokens for TableConstraint { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - TableConstraint::PrimaryKey { + Self::PrimaryKey { columns, auto_increment, conflict_clause, @@ -1357,7 +1357,7 @@ impl ToTokens for TableConstraint { } Ok(()) } - TableConstraint::Unique { + Self::Unique { columns, conflict_clause, } => { @@ -1372,13 +1372,13 @@ impl ToTokens for TableConstraint { } Ok(()) } - TableConstraint::Check(expr) => { + Self::Check(expr) => { s.append(TK_CHECK, None)?; s.append(TK_LP, None)?; expr.to_tokens(s)?; s.append(TK_RP, None) } - TableConstraint::ForeignKey { + Self::ForeignKey { columns, clause, deref_clause, @@ -1403,8 +1403,8 @@ impl ToTokens for SortOrder { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - SortOrder::Asc => TK_ASC, - SortOrder::Desc => TK_DESC, + Self::Asc => TK_ASC, + Self::Desc => TK_DESC, }, None, ) @@ -1416,8 +1416,8 @@ impl ToTokens for NullsOrder { s.append(TK_NULLS, None)?; s.append( match self { - NullsOrder::First => TK_FIRST, - NullsOrder::Last => TK_LAST, + Self::First => TK_FIRST, + Self::Last => TK_LAST, }, None, ) @@ -1432,7 +1432,7 @@ impl ToTokens for ForeignKeyClause { comma(columns, s)?; s.append(TK_RP, None)?; } - for arg in self.args.iter() { + for arg in &self.args { arg.to_tokens(s)?; } Ok(()) @@ -1442,22 +1442,22 @@ impl ToTokens for ForeignKeyClause { impl ToTokens for RefArg { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - RefArg::OnDelete(ref action) => { + Self::OnDelete(ref action) => { s.append(TK_ON, None)?; s.append(TK_DELETE, None)?; action.to_tokens(s) } - RefArg::OnInsert(ref action) => { + Self::OnInsert(ref action) => { s.append(TK_ON, None)?; s.append(TK_INSERT, None)?; action.to_tokens(s) } - RefArg::OnUpdate(ref action) => { + Self::OnUpdate(ref action) => { s.append(TK_ON, None)?; s.append(TK_UPDATE, None)?; action.to_tokens(s) } - RefArg::Match(ref name) => { + Self::Match(ref name) => { s.append(TK_MATCH, None)?; name.to_tokens(s) } @@ -1468,17 +1468,17 @@ impl ToTokens for RefArg { impl ToTokens for RefAct { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - RefAct::SetNull => { + Self::SetNull => { s.append(TK_SET, None)?; s.append(TK_NULL, None) } - RefAct::SetDefault => { + Self::SetDefault => { s.append(TK_SET, None)?; s.append(TK_DEFAULT, None) } - RefAct::Cascade => s.append(TK_CASCADE, None), - RefAct::Restrict => s.append(TK_RESTRICT, None), - RefAct::NoAction => { + Self::Cascade => s.append(TK_CASCADE, None), + Self::Restrict => s.append(TK_RESTRICT, None), + Self::NoAction => { s.append(TK_NO, None)?; s.append(TK_ACTION, None) } @@ -1504,8 +1504,8 @@ impl ToTokens for InitDeferredPred { s.append(TK_INITIALLY, None)?; s.append( match self { - InitDeferredPred::InitiallyDeferred => TK_DEFERRED, - InitDeferredPred::InitiallyImmediate => TK_IMMEDIATE, + Self::InitiallyDeferred => TK_DEFERRED, + Self::InitiallyImmediate => TK_IMMEDIATE, }, None, ) @@ -1529,12 +1529,12 @@ impl ToTokens for IndexedColumn { impl ToTokens for Indexed { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Indexed::IndexedBy(ref name) => { + Self::IndexedBy(ref name) => { s.append(TK_INDEXED, None)?; s.append(TK_BY, None)?; name.to_tokens(s) } - Indexed::NotIndexed => { + Self::NotIndexed => { s.append(TK_NOT, None)?; s.append(TK_INDEXED, None) } @@ -1570,14 +1570,14 @@ impl ToTokens for Limit { impl ToTokens for InsertBody { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - InsertBody::Select(select, upsert) => { + Self::Select(select, upsert) => { select.to_tokens(s)?; if let Some(upsert) = upsert { upsert.to_tokens(s)?; } Ok(()) } - InsertBody::DefaultValues => { + Self::DefaultValues => { s.append(TK_DEFAULT, None)?; s.append(TK_VALUES, None) } @@ -1602,11 +1602,11 @@ impl ToTokens for Set { impl ToTokens for PragmaBody { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - PragmaBody::Equals(value) => { + Self::Equals(value) => { s.append(TK_EQ, None)?; value.to_tokens(s) } - PragmaBody::Call(value) => { + Self::Call(value) => { s.append(TK_LP, None)?; value.to_tokens(s)?; s.append(TK_RP, None) @@ -1618,9 +1618,9 @@ impl ToTokens for PragmaBody { impl ToTokens for TriggerTime { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - TriggerTime::Before => s.append(TK_BEFORE, None), - TriggerTime::After => s.append(TK_AFTER, None), - TriggerTime::InsteadOf => { + Self::Before => s.append(TK_BEFORE, None), + Self::After => s.append(TK_AFTER, None), + Self::InsteadOf => { s.append(TK_INSTEAD, None)?; s.append(TK_OF, None) } @@ -1631,10 +1631,10 @@ impl ToTokens for TriggerTime { impl ToTokens for TriggerEvent { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - TriggerEvent::Delete => s.append(TK_DELETE, None), - TriggerEvent::Insert => s.append(TK_INSERT, None), - TriggerEvent::Update => s.append(TK_UPDATE, None), - TriggerEvent::UpdateOf(ref col_names) => { + Self::Delete => s.append(TK_DELETE, None), + Self::Insert => s.append(TK_INSERT, None), + Self::Update => s.append(TK_UPDATE, None), + Self::UpdateOf(ref col_names) => { s.append(TK_UPDATE, None)?; s.append(TK_OF, None)?; comma(col_names.deref(), s) @@ -1646,7 +1646,7 @@ impl ToTokens for TriggerEvent { impl ToTokens for TriggerCmd { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - TriggerCmd::Update { + Self::Update { or_conflict, tbl_name, sets, @@ -1671,7 +1671,7 @@ impl ToTokens for TriggerCmd { } Ok(()) } - TriggerCmd::Insert { + Self::Insert { or_conflict, tbl_name, col_names, @@ -1705,7 +1705,7 @@ impl ToTokens for TriggerCmd { } Ok(()) } - TriggerCmd::Delete { + Self::Delete { tbl_name, where_clause, } => { @@ -1718,7 +1718,7 @@ impl ToTokens for TriggerCmd { } Ok(()) } - TriggerCmd::Select(select) => select.to_tokens(s), + Self::Select(select) => select.to_tokens(s), } } } @@ -1727,11 +1727,11 @@ impl ToTokens for ResolveType { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - ResolveType::Rollback => TK_ROLLBACK, - ResolveType::Abort => TK_ABORT, - ResolveType::Fail => TK_FAIL, - ResolveType::Ignore => TK_IGNORE, - ResolveType::Replace => TK_REPLACE, + Self::Rollback => TK_ROLLBACK, + Self::Abort => TK_ABORT, + Self::Fail => TK_FAIL, + Self::Ignore => TK_IGNORE, + Self::Replace => TK_REPLACE, }, None, ) @@ -1790,8 +1790,8 @@ impl ToTokens for Type { impl ToTokens for TypeSize { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - TypeSize::MaxSize(size) => size.to_tokens(s), - TypeSize::TypeSize(size1, size2) => { + Self::MaxSize(size) => size.to_tokens(s), + Self::TypeSize(size1, size2) => { size1.to_tokens(s)?; s.append(TK_COMMA, None)?; size2.to_tokens(s) @@ -1804,9 +1804,9 @@ impl ToTokens for TransactionType { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - TransactionType::Deferred => TK_DEFERRED, - TransactionType::Immediate => TK_IMMEDIATE, - TransactionType::Exclusive => TK_EXCLUSIVE, + Self::Deferred => TK_DEFERRED, + Self::Immediate => TK_IMMEDIATE, + Self::Exclusive => TK_EXCLUSIVE, }, None, ) @@ -1844,7 +1844,7 @@ impl ToTokens for UpsertIndex { impl ToTokens for UpsertDo { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - UpsertDo::Set { sets, where_clause } => { + Self::Set { sets, where_clause } => { s.append(TK_DO, None)?; s.append(TK_UPDATE, None)?; s.append(TK_SET, None)?; @@ -1855,7 +1855,7 @@ impl ToTokens for UpsertDo { } Ok(()) } - UpsertDo::Nothing => { + Self::Nothing => { s.append(TK_DO, None)?; s.append(TK_NOTHING, None) } @@ -1883,8 +1883,8 @@ impl ToTokens for FunctionTail { impl ToTokens for Over { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - Over::Window(ref window) => window.to_tokens(s), - Over::Name(ref name) => name.to_tokens(s), + Self::Window(ref window) => window.to_tokens(s), + Self::Name(ref name) => name.to_tokens(s), } } } @@ -1943,9 +1943,9 @@ impl ToTokens for FrameMode { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { s.append( match self { - FrameMode::Groups => TK_GROUPS, - FrameMode::Range => TK_RANGE, - FrameMode::Rows => TK_ROWS, + Self::Groups => TK_GROUPS, + Self::Range => TK_RANGE, + Self::Rows => TK_ROWS, }, None, ) @@ -1955,23 +1955,23 @@ impl ToTokens for FrameMode { impl ToTokens for FrameBound { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - FrameBound::CurrentRow => { + Self::CurrentRow => { s.append(TK_CURRENT, None)?; s.append(TK_ROW, None) } - FrameBound::Following(value) => { + Self::Following(value) => { value.to_tokens(s)?; s.append(TK_FOLLOWING, None) } - FrameBound::Preceding(value) => { + Self::Preceding(value) => { value.to_tokens(s)?; s.append(TK_PRECEDING, None) } - FrameBound::UnboundedFollowing => { + Self::UnboundedFollowing => { s.append(TK_UNBOUNDED, None)?; s.append(TK_FOLLOWING, None) } - FrameBound::UnboundedPreceding => { + Self::UnboundedPreceding => { s.append(TK_UNBOUNDED, None)?; s.append(TK_PRECEDING, None) } @@ -1982,16 +1982,16 @@ impl ToTokens for FrameBound { impl ToTokens for FrameExclude { fn to_tokens(&self, s: &mut S) -> Result<(), S::Error> { match self { - FrameExclude::NoOthers => { + Self::NoOthers => { s.append(TK_NO, None)?; s.append(TK_OTHERS, None) } - FrameExclude::CurrentRow => { + Self::CurrentRow => { s.append(TK_CURRENT, None)?; s.append(TK_ROW, None) } - FrameExclude::Group => s.append(TK_GROUP, None), - FrameExclude::Ties => s.append(TK_TIES, None), + Self::Group => s.append(TK_GROUP, None), + Self::Ties => s.append(TK_TIES, None), } } } diff --git a/src/parser/ast/mod.rs b/src/parser/ast/mod.rs index 67937a2..d133b02 100644 --- a/src/parser/ast/mod.rs +++ b/src/parser/ast/mod.rs @@ -393,39 +393,39 @@ pub enum Expr { impl Expr { /// Constructor - pub fn parenthesized(x: Expr) -> Expr { - Expr::Parenthesized(vec![x]) + pub fn parenthesized(x: Self) -> Self { + Self::Parenthesized(vec![x]) } /// Constructor - pub fn id(xt: YYCODETYPE, x: Token) -> Expr { - Expr::Id(Id::from_token(xt, x)) + pub fn id(xt: YYCODETYPE, x: Token) -> Self { + Self::Id(Id::from_token(xt, x)) } /// Constructor - pub fn collate(x: Expr, ct: YYCODETYPE, c: Token) -> Expr { - Expr::Collate(Box::new(x), from_token(ct, c)) + pub fn collate(x: Self, ct: YYCODETYPE, c: Token) -> Self { + Self::Collate(Box::new(x), from_token(ct, c)) } /// Constructor - pub fn cast(x: Expr, type_name: Option) -> Expr { - Expr::Cast { + pub fn cast(x: Self, type_name: Option) -> Self { + Self::Cast { expr: Box::new(x), type_name, } } /// Constructor - pub fn binary(left: Expr, op: YYCODETYPE, right: Expr) -> Expr { - Expr::Binary(Box::new(left), Operator::from(op), Box::new(right)) + pub fn binary(left: Self, op: YYCODETYPE, right: Self) -> Self { + Self::Binary(Box::new(left), Operator::from(op), Box::new(right)) } /// Constructor - pub fn ptr(left: Expr, op: Token, right: Expr) -> Expr { + pub fn ptr(left: Self, op: Token, right: Self) -> Self { let mut ptr = Operator::ArrowRight; if op.1 == b"->>" { ptr = Operator::ArrowRightShift; } - Expr::Binary(Box::new(left), ptr, Box::new(right)) + Self::Binary(Box::new(left), ptr, Box::new(right)) } /// Constructor - pub fn like(lhs: Expr, not: bool, op: LikeOperator, rhs: Expr, escape: Option) -> Expr { - Expr::Like { + pub fn like(lhs: Self, not: bool, op: LikeOperator, rhs: Self, escape: Option) -> Self { + Self::Like { lhs: Box::new(lhs), not, op, @@ -434,22 +434,22 @@ impl Expr { } } /// Constructor - pub fn not_null(x: Expr, op: YYCODETYPE) -> Expr { + pub fn not_null(x: Self, op: YYCODETYPE) -> Self { if op == TK_ISNULL as YYCODETYPE { - Expr::IsNull(Box::new(x)) + Self::IsNull(Box::new(x)) } else if op == TK_NOTNULL as YYCODETYPE { - Expr::NotNull(Box::new(x)) + Self::NotNull(Box::new(x)) } else { unreachable!() } } /// Constructor - pub fn unary(op: UnaryOperator, x: Expr) -> Expr { - Expr::Unary(op, Box::new(x)) + pub fn unary(op: UnaryOperator, x: Self) -> Self { + Self::Unary(op, Box::new(x)) } /// Constructor - pub fn between(lhs: Expr, not: bool, start: Expr, end: Expr) -> Expr { - Expr::Between { + pub fn between(lhs: Self, not: bool, start: Self, end: Self) -> Self { + Self::Between { lhs: Box::new(lhs), not, start: Box::new(start), @@ -457,24 +457,24 @@ impl Expr { } } /// Constructor - pub fn in_list(lhs: Expr, not: bool, rhs: Option>) -> Expr { - Expr::InList { + pub fn in_list(lhs: Self, not: bool, rhs: Option>) -> Self { + Self::InList { lhs: Box::new(lhs), not, rhs, } } /// Constructor - pub fn in_select(lhs: Expr, not: bool, rhs: Select) -> Expr { - Expr::InSelect { + pub fn in_select(lhs: Self, not: bool, rhs: Select) -> Self { + Self::InSelect { lhs: Box::new(lhs), not, rhs: Box::new(rhs), } } /// Constructor - pub fn in_table(lhs: Expr, not: bool, rhs: QualifiedName, args: Option>) -> Expr { - Expr::InTable { + pub fn in_table(lhs: Self, not: bool, rhs: QualifiedName, args: Option>) -> Self { + Self::InTable { lhs: Box::new(lhs), not, rhs, @@ -482,8 +482,8 @@ impl Expr { } } /// Constructor - pub fn sub_query(query: Select) -> Expr { - Expr::Subquery(Box::new(query)) + pub fn sub_query(query: Select) -> Self { + Self::Subquery(Box::new(query)) } } @@ -512,13 +512,13 @@ pub enum Literal { impl Literal { /// Constructor - pub fn from_ctime_kw(token: Token) -> Literal { + pub fn from_ctime_kw(token: Token) -> Self { if b"CURRENT_DATE".eq_ignore_ascii_case(token.1) { - Literal::CurrentDate + Self::CurrentDate } else if b"CURRENT_TIME".eq_ignore_ascii_case(token.1) { - Literal::CurrentTime + Self::CurrentTime } else if b"CURRENT_TIMESTAMP".eq_ignore_ascii_case(token.1) { - Literal::CurrentTimestamp + Self::CurrentTimestamp } else { unreachable!() } @@ -540,17 +540,17 @@ pub enum LikeOperator { impl LikeOperator { /// Constructor - pub fn from_token(token_type: YYCODETYPE, token: Token) -> LikeOperator { + pub fn from_token(token_type: YYCODETYPE, token: Token) -> Self { if token_type == TK_MATCH as YYCODETYPE { - return LikeOperator::Match; + return Self::Match; } else if token_type == TK_LIKE_KW as YYCODETYPE { let token = token.1; if b"LIKE".eq_ignore_ascii_case(token) { - return LikeOperator::Like; + return Self::Like; } else if b"GLOB".eq_ignore_ascii_case(token) { - return LikeOperator::Glob; + return Self::Glob; } else if b"REGEXP".eq_ignore_ascii_case(token) { - return LikeOperator::Regexp; + return Self::Regexp; } } unreachable!() @@ -607,28 +607,28 @@ pub enum Operator { } impl From for Operator { - fn from(token_type: YYCODETYPE) -> Operator { + fn from(token_type: YYCODETYPE) -> Self { match token_type { - x if x == TK_AND as YYCODETYPE => Operator::And, - x if x == TK_OR as YYCODETYPE => Operator::Or, - x if x == TK_LT as YYCODETYPE => Operator::Less, - x if x == TK_GT as YYCODETYPE => Operator::Greater, - x if x == TK_GE as YYCODETYPE => Operator::GreaterEquals, - x if x == TK_LE as YYCODETYPE => Operator::LessEquals, - x if x == TK_EQ as YYCODETYPE => Operator::Equals, - x if x == TK_NE as YYCODETYPE => Operator::NotEquals, - x if x == TK_BITAND as YYCODETYPE => Operator::BitwiseAnd, - x if x == TK_BITOR as YYCODETYPE => Operator::BitwiseOr, - x if x == TK_LSHIFT as YYCODETYPE => Operator::LeftShift, - x if x == TK_RSHIFT as YYCODETYPE => Operator::RightShift, - x if x == TK_PLUS as YYCODETYPE => Operator::Add, - x if x == TK_MINUS as YYCODETYPE => Operator::Substract, - x if x == TK_STAR as YYCODETYPE => Operator::Multiply, - x if x == TK_SLASH as YYCODETYPE => Operator::Divide, - x if x == TK_REM as YYCODETYPE => Operator::Modulus, - x if x == TK_CONCAT as YYCODETYPE => Operator::Concat, - x if x == TK_IS as YYCODETYPE => Operator::Is, - x if x == TK_NOT as YYCODETYPE => Operator::IsNot, + x if x == TK_AND as YYCODETYPE => Self::And, + x if x == TK_OR as YYCODETYPE => Self::Or, + x if x == TK_LT as YYCODETYPE => Self::Less, + x if x == TK_GT as YYCODETYPE => Self::Greater, + x if x == TK_GE as YYCODETYPE => Self::GreaterEquals, + x if x == TK_LE as YYCODETYPE => Self::LessEquals, + x if x == TK_EQ as YYCODETYPE => Self::Equals, + x if x == TK_NE as YYCODETYPE => Self::NotEquals, + x if x == TK_BITAND as YYCODETYPE => Self::BitwiseAnd, + x if x == TK_BITOR as YYCODETYPE => Self::BitwiseOr, + x if x == TK_LSHIFT as YYCODETYPE => Self::LeftShift, + x if x == TK_RSHIFT as YYCODETYPE => Self::RightShift, + x if x == TK_PLUS as YYCODETYPE => Self::Add, + x if x == TK_MINUS as YYCODETYPE => Self::Substract, + x if x == TK_STAR as YYCODETYPE => Self::Multiply, + x if x == TK_SLASH as YYCODETYPE => Self::Divide, + x if x == TK_REM as YYCODETYPE => Self::Modulus, + x if x == TK_CONCAT as YYCODETYPE => Self::Concat, + x if x == TK_IS as YYCODETYPE => Self::Is, + x if x == TK_NOT as YYCODETYPE => Self::IsNot, _ => unreachable!(), } } @@ -648,12 +648,12 @@ pub enum UnaryOperator { } impl From for UnaryOperator { - fn from(token_type: YYCODETYPE) -> UnaryOperator { + fn from(token_type: YYCODETYPE) -> Self { match token_type { - x if x == TK_BITNOT as YYCODETYPE => UnaryOperator::BitwiseNot, - x if x == TK_MINUS as YYCODETYPE => UnaryOperator::Negative, - x if x == TK_NOT as YYCODETYPE => UnaryOperator::Not, - x if x == TK_PLUS as YYCODETYPE => UnaryOperator::Positive, + x if x == TK_BITNOT as YYCODETYPE => Self::BitwiseNot, + x if x == TK_MINUS as YYCODETYPE => Self::Negative, + x if x == TK_NOT as YYCODETYPE => Self::Not, + x if x == TK_PLUS as YYCODETYPE => Self::Positive, _ => unreachable!(), } } @@ -762,8 +762,8 @@ pub struct FromClause { op: Option, // FIXME transient } impl FromClause { - pub(crate) fn empty() -> FromClause { - FromClause { + pub(crate) fn empty() -> Self { + Self { select: None, joins: None, op: None, @@ -879,7 +879,7 @@ impl JoinOperator { token: Token, n1: Option, n2: Option, - ) -> Result { + ) -> Result { Ok({ let mut jt = JoinType::try_from(token.1)?; for n in [&n1, &n2].into_iter().flatten() { @@ -895,12 +895,12 @@ impl JoinOperator { n2 )); } - JoinOperator::TypedJoin(Some(jt)) + Self::TypedJoin(Some(jt)) }) } fn is_natural(&self) -> bool { match self { - JoinOperator::TypedJoin(Some(jt)) => jt.contains(JoinType::NATURAL), + Self::TypedJoin(Some(jt)) => jt.contains(JoinType::NATURAL), _ => false, } } @@ -928,21 +928,21 @@ bitflags::bitflags! { impl TryFrom<&[u8]> for JoinType { type Error = ParserError; - fn try_from(s: &[u8]) -> Result { + fn try_from(s: &[u8]) -> Result { if b"CROSS".eq_ignore_ascii_case(s) { - Ok(JoinType::INNER | JoinType::CROSS) + Ok(Self::INNER | Self::CROSS) } else if b"FULL".eq_ignore_ascii_case(s) { - Ok(JoinType::LEFT | JoinType::RIGHT | JoinType::OUTER) + Ok(Self::LEFT | Self::RIGHT | Self::OUTER) } else if b"INNER".eq_ignore_ascii_case(s) { - Ok(JoinType::INNER) + Ok(Self::INNER) } else if b"LEFT".eq_ignore_ascii_case(s) { - Ok(JoinType::LEFT | JoinType::OUTER) + Ok(Self::LEFT | Self::OUTER) } else if b"NATURAL".eq_ignore_ascii_case(s) { - Ok(JoinType::NATURAL) + Ok(Self::NATURAL) } else if b"RIGHT".eq_ignore_ascii_case(s) { - Ok(JoinType::RIGHT | JoinType::OUTER) + Ok(Self::RIGHT | Self::OUTER) } else if b"OUTER".eq_ignore_ascii_case(s) { - Ok(JoinType::OUTER) + Ok(Self::OUTER) } else { Err(custom_err!( "unsupported JOIN type: {:?}", @@ -976,8 +976,8 @@ pub struct Id(pub String); impl Id { /// Constructor - pub fn from_token(ty: YYCODETYPE, token: Token) -> Id { - Id(from_token(ty, token)) + pub fn from_token(ty: YYCODETYPE, token: Token) -> Self { + Self(from_token(ty, token)) } } @@ -989,8 +989,8 @@ pub struct Name(pub String); // TODO distinction between Name and "Name"/[Name]/ impl Name { /// Constructor - pub fn from_token(ty: YYCODETYPE, token: Token) -> Name { - Name(from_token(ty, token)) + pub fn from_token(ty: YYCODETYPE, token: Token) -> Self { + Self(from_token(ty, token)) } fn as_bytes(&self) -> QuotedIterator<'_> { @@ -1062,21 +1062,18 @@ impl std::hash::Hash for Name { } /// Ignore case and quote impl PartialEq for Name { - #[inline(always)] - fn eq(&self, other: &Name) -> bool { + fn eq(&self, other: &Self) -> bool { eq_ignore_case_and_quote(self.as_bytes(), other.as_bytes()) } } /// Ignore case and quote impl PartialEq for Name { - #[inline(always)] fn eq(&self, other: &str) -> bool { eq_ignore_case_and_quote(self.as_bytes(), QuotedIterator(other.bytes(), 0u8)) } } /// Ignore case and quote impl PartialEq<&str> for Name { - #[inline(always)] fn eq(&self, other: &&str) -> bool { eq_ignore_case_and_quote(self.as_bytes(), QuotedIterator(other.bytes(), 0u8)) } @@ -1096,7 +1093,7 @@ pub struct QualifiedName { impl QualifiedName { /// Constructor pub fn single(name: Name) -> Self { - QualifiedName { + Self { db_name: None, name, alias: None, @@ -1104,7 +1101,7 @@ impl QualifiedName { } /// Constructor pub fn fullname(db_name: Name, name: Name) -> Self { - QualifiedName { + Self { db_name: Some(db_name), name, alias: None, @@ -1112,7 +1109,7 @@ impl QualifiedName { } /// Constructor pub fn xfullname(db_name: Name, name: Name, alias: Name) -> Self { - QualifiedName { + Self { db_name: Some(db_name), name, alias: Some(alias), @@ -1120,7 +1117,7 @@ impl QualifiedName { } /// Constructor pub fn alias(name: Name, alias: Name) -> Self { - QualifiedName { + Self { db_name: None, name, alias: Some(alias), @@ -1134,14 +1131,14 @@ pub struct DistinctNames(IndexSet); impl DistinctNames { /// Initialize - pub fn new(name: Name) -> DistinctNames { - let mut dn = DistinctNames(IndexSet::new()); + pub fn new(name: Name) -> Self { + let mut dn = Self(IndexSet::new()); dn.0.insert(name); dn } /// Single column name - pub fn single(name: Name) -> DistinctNames { - let mut dn = DistinctNames(IndexSet::with_capacity(1)); + pub fn single(name: Name) -> Self { + let mut dn = Self(IndexSet::with_capacity(1)); dn.0.insert(name); dn } @@ -1205,8 +1202,8 @@ impl CreateTableBody { columns: IndexMap, constraints: Option>, options: TableOptions, - ) -> Result { - Ok(CreateTableBody::ColumnsAndConstraints { + ) -> Result { + Ok(Self::ColumnsAndConstraints { columns, constraints, options, @@ -1228,10 +1225,7 @@ pub struct ColumnDefinition { impl ColumnDefinition { /// Constructor - pub fn add_column( - columns: &mut IndexMap, - mut cd: ColumnDefinition, - ) -> Result<(), ParserError> { + pub fn add_column(columns: &mut IndexMap, mut cd: Self) -> Result<(), ParserError> { let col_name = &cd.col_name; if columns.contains_key(col_name) { // TODO unquote @@ -1274,7 +1268,7 @@ impl ColumnDefinition { } = &constraint.constraint { // The child table may reference the primary key of the parent without specifying the primary key column - if columns.as_ref().map_or(0, |v| v.len()) > 1 { + if columns.as_ref().map_or(0, std::vec::Vec::len) > 1 { return Err(custom_err!( "foreign key on {} should reference only one column of table {}", col_name, @@ -1675,10 +1669,7 @@ pub struct CommonTableExpr { impl CommonTableExpr { /// Constructor - pub fn add_cte( - ctes: &mut Vec, - cte: CommonTableExpr, - ) -> Result<(), ParserError> { + pub fn add_cte(ctes: &mut Vec, cte: Self) -> Result<(), ParserError> { if ctes.iter().any(|c| c.tbl_name == cte.tbl_name) { return Err(custom_err!("duplicate WITH table name: {}", cte.tbl_name)); } diff --git a/src/parser/mod.rs b/src/parser/mod.rs index b3f9a8b..b4da1b7 100644 --- a/src/parser/mod.rs +++ b/src/parser/mod.rs @@ -31,11 +31,11 @@ pub enum ParserError { impl std::fmt::Display for ParserError { fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { match self { - ParserError::SyntaxError(s) => { - write!(f, "near \"{}\": syntax error", s) + Self::SyntaxError(s) => { + write!(f, "near \"{s}\": syntax error") } - ParserError::UnexpectedEof => f.write_str("unexpected end of input"), - ParserError::Custom(s) => f.write_str(s), + Self::UnexpectedEof => f.write_str("unexpected end of input"), + Self::Custom(s) => f.write_str(s), } } } @@ -69,7 +69,7 @@ pub struct Context<'input> { } impl<'input> Context<'input> { - pub fn new(input: &'input [u8]) -> Context<'input> { + pub fn new(input: &'input [u8]) -> Self { Context { input, explain: None,