From 6fe9fef474b9e68cd437cc5338472d91e043b590 Mon Sep 17 00:00:00 2001 From: Josh Holmer Date: Fri, 6 Sep 2024 08:30:52 -0400 Subject: [PATCH] fix: compatibility with older versions of rust compiler --- .github/workflows/sqlformat.yml | 77 ++++++++++++++++++--------------- CHANGELOG.md | 4 +- Cargo.toml | 6 +-- benches/bench.rs | 2 - src/formatter.rs | 13 +++--- src/lib.rs | 9 +--- 6 files changed, 57 insertions(+), 54 deletions(-) diff --git a/.github/workflows/sqlformat.yml b/.github/workflows/sqlformat.yml index ef5caee..183d216 100644 --- a/.github/workflows/sqlformat.yml +++ b/.github/workflows/sqlformat.yml @@ -14,10 +14,13 @@ jobs: strategy: matrix: conf: + - minimum - latest-stable - latest-beta - latest-nightly include: + - conf: minimum + toolchain: 1.61.0 - conf: latest-stable toolchain: stable - conf: latest-beta @@ -25,37 +28,43 @@ jobs: - conf: latest-nightly toolchain: nightly steps: - - uses: actions/checkout@v2 - - name: Install ${{ matrix.toolchain }} - uses: actions-rs/toolchain@v1 - with: - profile: minimal - toolchain: ${{ matrix.toolchain }} - override: true - components: clippy, rustfmt - - name: Cache cargo registry - uses: actions/cache@v1 - with: - path: ~/.cargo/registry/cache - key: ${{ runner.os }}-${{ matrix.conf }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} - restore-keys: | - ${{ runner.os }}-${{ matrix.conf }}-cargo-registry- - - name: Run rustfmt - if: matrix.toolchain == 'stable' - uses: actions-rs/cargo@v1 - with: - command: fmt - args: -- --check - - name: Run clippy - if: matrix.toolchain == 'stable' - uses: actions-rs/clippy-check@v1 - with: - token: ${{ secrets.GITHUB_TOKEN }} - args: -- -D warnings - - name: Run tests - run: cargo test - - name: Build benchmarks - if: matrix.toolchain == 'stable' - run: cargo bench --no-run - - name: Build docs - run: cargo doc --no-deps + - uses: actions/checkout@v2 + - name: Install ${{ matrix.toolchain }} + uses: actions-rs/toolchain@v1 + with: + profile: minimal + toolchain: ${{ matrix.toolchain }} + override: true + components: clippy, rustfmt + - name: Cache cargo registry + uses: actions/cache@v1 + with: + path: ~/.cargo/registry/cache + key: ${{ runner.os }}-${{ matrix.conf }}-cargo-registry-${{ hashFiles('**/Cargo.lock') }} + restore-keys: | + ${{ runner.os }}-${{ matrix.conf }}-cargo-registry- + - name: Run rustfmt + if: matrix.toolchain == 'stable' + uses: actions-rs/cargo@v1 + with: + command: fmt + args: -- --check + - name: Run clippy + if: matrix.toolchain == 'stable' + uses: actions-rs/clippy-check@v1 + with: + token: ${{ secrets.GITHUB_TOKEN }} + args: -- -D warnings + # FIXME: criterion and its dependencies require a newer version than 1.61, but it is only used for benchmarks. + # Is there a way to not have criterion built when we run tests? + - name: Run cargo check + if: matrix.toolchain == '1.61.0' + run: cargo check + - name: Run tests + if: matrix.toolchain != '1.61.0' + run: cargo test + - name: Build benchmarks + if: matrix.toolchain == 'stable' + run: cargo bench --no-run + - name: Build docs + run: cargo doc --no-deps diff --git a/CHANGELOG.md b/CHANGELOG.md index 72f13f2..8424215 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,4 +1,4 @@ -### Version 0.2.5 +### Version 0.2.6 - fix: ON UPDATE with two many blank formatted incorrectly (#46) - fix: `EXCEPT` not handled well @@ -35,7 +35,7 @@ - Fix extra spaces in string escaping [#13](https://github.com/shssoichiro/sqlformat-rs/pull/13) - Fix panic on overflowing integer [#14](https://github.com/shssoichiro/sqlformat-rs/pull/14) - Bump Rust edition to 2021 - - This is technically a breaking change as it bumps the minimum Rust version to 1.56 + - This is technically a breaking change as it bumps the minimum Rust version to 1.61 ### Version 0.1.8 diff --git a/Cargo.toml b/Cargo.toml index 582efbf..55f1154 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,9 +1,9 @@ [package] name = "sqlformat" -version = "0.2.5" +version = "0.2.6" authors = ["Josh Holmer "] edition = "2021" -rust-version = "1.56" +rust-version = "1.61" license = "MIT OR Apache-2.0" homepage = "https://github.com/shssoichiro/sqlformat-rs" repository = "https://github.com/shssoichiro/sqlformat-rs" @@ -17,7 +17,7 @@ nom = "7.0.0" unicode_categories = "0.1.1" [dev-dependencies] -criterion = "0.5" +criterion = "0.4" indoc = "2.0" [[bench]] diff --git a/benches/bench.rs b/benches/bench.rs index b10b315..a461597 100644 --- a/benches/bench.rs +++ b/benches/bench.rs @@ -77,7 +77,6 @@ fn issue_633(c: &mut Criterion) { const SIZE: usize = 1000; pub struct UserData { - pub id: i64, pub first_name: String, pub last_name: String, pub address: String, @@ -87,7 +86,6 @@ fn issue_633(c: &mut Criterion) { fn sample() -> UserData { UserData { - id: -1, first_name: "FIRST_NAME".to_string(), last_name: "LAST_NAME".to_string(), address: "SOME_ADDRESS".to_string(), diff --git a/src/formatter.rs b/src/formatter.rs index 66126d4..2135816 100644 --- a/src/formatter.rs +++ b/src/formatter.rs @@ -136,13 +136,14 @@ impl<'a> Formatter<'a> { } fn format_with_spaces(&self, token: &Token<'_>, query: &mut String) { - let value = if token.kind == TokenKind::Reserved { - &self.equalize_whitespace(&self.format_reserved_word(token.value)) + if token.kind == TokenKind::Reserved { + let value = self.equalize_whitespace(&self.format_reserved_word(token.value)); + query.push_str(&value); + query.push(' '); } else { - token.value + query.push_str(token.value); + query.push(' '); }; - query.push_str(value); - query.push(' '); } // Opening parentheses increase the block indent level and start a new line @@ -248,7 +249,7 @@ impl<'a> Formatter<'a> { } fn trim_spaces_end(&self, query: &mut String) { - query.truncate(query.trim_end_matches(|c| c == ' ' || c == '\t').len()); + query.truncate(query.trim_end_matches([' ', '\t']).len()); } fn trim_all_spaces_end(&self, query: &mut String) { diff --git a/src/lib.rs b/src/lib.rs index ad5b166..f130053 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -55,19 +55,14 @@ pub enum Indent { Tabs, } -#[derive(Debug, Clone)] +#[derive(Debug, Clone, Default)] pub enum QueryParams { Named(Vec<(String, String)>), Indexed(Vec), + #[default] None, } -impl Default for QueryParams { - fn default() -> Self { - QueryParams::None - } -} - #[cfg(test)] mod tests { use super::*;