diff --git a/.github/workflows/check-rust.yaml b/.github/workflows/check-rust.yaml new file mode 100644 index 00000000..558d3d03 --- /dev/null +++ b/.github/workflows/check-rust.yaml @@ -0,0 +1,50 @@ +--- +name: PR checks for rust server backend + +on: + pull_request: + push: + +concurrency: + group: '${{ github.workflow }} @ ${{ github.head_ref || github.ref }}' + cancel-in-progress: true + +jobs: + changed-files: + name: Check changes for rust server app + runs-on: minafoundation-default-interruptible-runners + steps: + - name: 📥 Checkout + uses: actions/checkout@v4 + - uses: tj-actions/changed-files@v39 + id: changed + with: + files_yaml: | + server: + - 'server/**' + write_output_files: true + outputs: + modified_keys: ${{ steps.changed.outputs.modified_keys }} + rust-checks: + needs: changed-files + if: needs.changed-files.outputs.modified_keys != '[]' && needs.changed-files.outputs.modified_keys != '' + name: Rust PR Checks + runs-on: minafoundation-default-interruptible-runners + strategy: + matrix: + # In case in the future we want to add beta/nightly + toolchain: [stable] + steps: + - uses: actions/checkout@v4 + - name: Install ${{ matrix.toolchain }} + uses: dtolnay/rust-toolchain@master + with: + toolchain: ${{ matrix.toolchain }} + - name: ✍️ Check formatting + run: cargo fmt --all -- --check + - name: 🧐 Check linting + run: cargo clippy -- -D warnings -D clippy::unwrap_used + - name: 🏗️ Build the app + run: cargo build --release + - name: 📋 Run the tests + run: cargo test --locked --all-features --all-targets diff --git a/server/src/ledger.rs b/server/src/ledger.rs index f8996421..f79dd86c 100644 --- a/server/src/ledger.rs +++ b/server/src/ledger.rs @@ -1,12 +1,12 @@ use std::{collections::HashMap, fs, io::Read, path::PathBuf}; -use anyhow::{Result, anyhow}; +use anyhow::{anyhow, Result}; use flate2::read::GzDecoder; use rust_decimal::Decimal; use serde::{Deserialize, Serialize}; use tar::Archive; -use crate::{Ocv, ProposalVersion, Vote, Wrapper, s3_client}; +use crate::{s3_client, Ocv, ProposalVersion, Vote, Wrapper}; #[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq, Hash)] pub struct Ledger(pub Vec); @@ -18,7 +18,7 @@ impl Ledger { Self::download(ocv, hash, &dest).await?; } let contents = fs::read(dest)?; - Ok(Ledger(serde_json::from_slice(&contents[..]).unwrap())) + Ok(Ledger(serde_json::from_slice(&contents[..]).expect("Expecting a valid list of ledger accounts."))) } async fn download(ocv: &Ocv, hash: &String, to: &PathBuf) -> Result<()> { @@ -30,10 +30,7 @@ impl Ledger { .await? .contents .and_then(|objects| { - objects - .into_iter() - .find(|object| object.key.as_ref().map_or(false, |key| key.contains(hash))) - .and_then(|x| x.key) + objects.into_iter().find(|object| object.key.as_ref().is_some_and(|key| key.contains(hash))).and_then(|x| x.key) }) .ok_or(anyhow!("Could not retrieve dump corresponding to {hash}"))?; let bytes = @@ -42,7 +39,7 @@ impl Ledger { let mut archive = Archive::new(tar_gz); for entry in archive.entries()? { let mut entry = entry?; - let path = entry.path()?.to_str().unwrap().to_owned(); + let path = entry.path()?.to_str().expect("Expecting a valid path").to_owned(); if s3_path.contains(&path) { let mut buffer = Vec::new(); entry.read_to_end(&mut buffer)?;