From 58b6f198ff487d09a31d3ced281524c168a59b08 Mon Sep 17 00:00:00 2001 From: David Beechey Date: Wed, 18 Sep 2024 12:13:38 +0100 Subject: [PATCH] INFRA - GitHub Actions CI (#9) Co-authored-by: Kshitij Sharma Co-authored-by: Kshitij Sharma --- .github/workflows/ci.yml | 24 +++++++++++++++++ .github/workflows/lint.yml | 53 ++++++++++++++++++++++++++++++++++++++ .rustfmt.toml | 2 ++ config/src/lib.rs | 8 +++--- lib/core/src/mqtt.rs | 6 ++--- 5 files changed, 86 insertions(+), 7 deletions(-) create mode 100644 .github/workflows/ci.yml create mode 100644 .github/workflows/lint.yml create mode 100644 .rustfmt.toml diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml new file mode 100644 index 00000000..8029dcdc --- /dev/null +++ b/.github/workflows/ci.yml @@ -0,0 +1,24 @@ +# https://doc.rust-lang.org/cargo/guide/continuous-integration.html + +name: Cargo Build & Test + +on: push + +env: + CARGO_TERM_COLOR: always + +jobs: + build_and_test: + name: Cargo Build & Test + runs-on: ubuntu-latest + strategy: + matrix: + toolchain: + - stable + - beta + - nightly + steps: + - uses: actions/checkout@v4 + - run: rustup update ${{ matrix.toolchain }} && rustup default ${{ matrix.toolchain }} + - run: cargo build --verbose + - run: cargo test --verbose diff --git a/.github/workflows/lint.yml b/.github/workflows/lint.yml new file mode 100644 index 00000000..cac08ed3 --- /dev/null +++ b/.github/workflows/lint.yml @@ -0,0 +1,53 @@ +name: Linter + +on: push + +jobs: + rust-clippy: + name: Run clippy + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run clippy + run: cargo clippy --all-targets --all-features -- -D warnings + + rust-fmt: + name: Run rustfmt + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run rustfmt + run: cargo fmt --all -- --check + + typos: + name: Spell check with typos + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run typos + uses: crate-ci/typos@master + + find-todos-fixme: + name: Find todos and fixmes + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run todo check + run: | + if grep -riE -n 'TODO|FIXME' * | grep -v -i 'TODOLater'; then + exit 1 + else + echo "All good" + fi + + check-no-crlf: + name: Check no crlf line endings + runs-on: ubuntu-latest + steps: + - uses: actions/checkout@v4 + - name: Run check for CRLF line endings + run: | + if git ls-files --eol | grep crlf; then + echo "[ERROR] found CRLF line endings, install dos2unix and run 'find . -type f -exec dos2unix {} \;' to fix" + exit 1 + fi diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 00000000..32c3d11a --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +reorder_imports = true +imports_granularity = "Crate" \ No newline at end of file diff --git a/config/src/lib.rs b/config/src/lib.rs index 527068f1..c699a3cf 100644 --- a/config/src/lib.rs +++ b/config/src/lib.rs @@ -1,5 +1,4 @@ use serde::{Deserialize, Serialize}; -use serde_yml; use std::collections::HashMap; #[derive(Debug, Serialize, Deserialize, PartialEq)] @@ -47,7 +46,7 @@ pub struct PodConfig { impl PodConfig { fn new(raw_config: &str) -> Result { - let config = match serde_yml::from_str::(&raw_config) { + let config = match serde_yml::from_str::(raw_config) { Ok(mut config) => { config.pod_ids = config.pods.keys().cloned().collect(); Ok(config) @@ -142,9 +141,10 @@ mod tests { min: -150 max: 150 "#; - let mut config = PodConfig::new(raw_config).unwrap(); + let config = PodConfig::new(raw_config).unwrap(); assert!(config.pod_ids.len() == 2); - assert!(config.pod_ids.sort() == vec!["pod_1", "pod_2"].sort()); + assert!(config.pod_ids[0] == "pod_1" || config.pod_ids[1] == "pod_1"); + assert!(config.pod_ids[0] == "pod_2" || config.pod_ids[1] == "pod_2"); let pod1 = config.pods.get("pod_1").unwrap(); let pod2 = config.pods.get("pod_2").unwrap(); assert_eq!(pod1.name, "Pod 1"); diff --git a/lib/core/src/mqtt.rs b/lib/core/src/mqtt.rs index 385bbd8e..3a2523a9 100644 --- a/lib/core/src/mqtt.rs +++ b/lib/core/src/mqtt.rs @@ -45,7 +45,7 @@ impl<'a> HypedMqttClient<'a, TcpSocket<'a>, CountingRng> { } /// Initialise the MQTT client configuration with the given client ID -pub fn initialise_mqtt_config<'a>(client_id: &'a str) -> ClientConfig<'a, 5, CountingRng> { +pub fn initialise_mqtt_config(client_id: &str) -> ClientConfig<'_, 5, CountingRng> { let mut config = ClientConfig::new( rust_mqtt::client::client_config::MqttVersion::MQTTv5, CountingRng(20000), @@ -119,11 +119,11 @@ impl<'a, T: embedded_io_async::Read + embedded_io_async::Write, R: rand_core::Rn Err(mqtt_error) => match mqtt_error { ReasonCode::NetworkError => { info!("MQTT Network Error"); - return Err(ReasonCode::NetworkError); + Err(ReasonCode::NetworkError) } _ => { warn!("Other MQTT Error: {:?}", mqtt_error); - return Err(mqtt_error); + Err(mqtt_error) } }, }