Skip to content

Commit

Permalink
Minor lints and CI updates (#9)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik authored Jun 20, 2024
1 parent f217b8f commit 6c541bd
Show file tree
Hide file tree
Showing 13 changed files with 119 additions and 59 deletions.
14 changes: 2 additions & 12 deletions .cargo-husky/hooks/pre-push
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,10 @@

echo "-------------------------------------------------------------------------------"
echo "Run CI steps"
echo "The following steps are also ran during git push command."
echo "The following steps are ran during git push command."
echo "If you want to push your changes without running CI, use git push --no-verify"
echo "-------------------------------------------------------------------------------"

set -ex

rustc --version
cargo --version
cargo fmt --all -- --check
cargo build
cargo build --no-default-features --features with-uds
cargo test
cargo test --no-default-features --features with-uds
cargo test --no-default-features
cargo test --doc
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps
cargo clippy -- -D warnings
just ci-test
64 changes: 58 additions & 6 deletions .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,70 @@ name: CI

on:
push:
branches: [main]
branches: [ main ]
pull_request:
branches: [main]
branches: [ main ]
release:
types: [published]
types: [ published ]
workflow_dispatch:

jobs:
build:
name: Build
test:
name: Test
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
- run: source .cargo-husky/hooks/pre-push
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'
- run: just ci-test
- name: Check semver
uses: obi1kenobi/cargo-semver-checks-action@v2
- name: Get local and published versions
id: get-versions
run: |
echo "local_version=$(grep '^version =' Cargo.toml | sed -E 's/version = "([^"]*)".*/\1/')" >> $GITHUB_OUTPUT
CRATE_NAME=$(grep '^name =' Cargo.toml | head -1 | sed -E 's/name = "(.*)"/\1/')
PUBLISHED_VERSION=$(cargo search ${CRATE_NAME} | grep "^${CRATE_NAME} =" | sed -E 's/.* = "(.*)".*/\1/')
echo "published_version=${PUBLISHED_VERSION}" >> $GITHUB_OUTPUT
- name: Test that we haven't published current version yet
run: |
LOCAL_VERSION=${{ steps.get-versions.outputs.local_version }}
PUBLISHED_VERSION=${{ steps.get-versions.outputs.published_version }}
if [ "$LOCAL_VERSION" = "$PUBLISHED_VERSION" ]; then
echo "The current crate version ($LOCAL_VERSION) has already been published."
exit 1
else
echo "The current crate version ($LOCAL_VERSION) has not been published yet."
fi
msrv:
name: Test MSRV
runs-on: ubuntu-latest
steps:
- uses: taiki-e/install-action@v2
with: { tool: just }
- uses: actions/checkout@v4
- uses: Swatinem/rust-cache@v2
if: github.event_name != 'release' && github.event_name != 'workflow_dispatch'
- name: Read crate metadata
id: metadata
run: echo "rust-version=$(sed -ne 's/rust-version *= *\"\(.*\)\"/\1/p' Cargo.toml)" >> $GITHUB_OUTPUT
- name: Install Rust
uses: dtolnay/rust-toolchain@stable
with:
toolchain: ${{ steps.metadata.outputs.rust-version }}
- run: just ci-test-msrv

publish:
name: Publish to crates.io
if: startsWith(github.ref, 'refs/tags/')
needs: [ test, msrv ]
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v4
- name: Publish to crates.io
run: cargo publish
env:
CARGO_REGISTRY_TOKEN: ${{ secrets.CARGO_REGISTRY_TOKEN }}
11 changes: 10 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "automotive_diag"
version = "0.1.6"
version = "0.1.7"
description = "Core definitions for various Automotive ECU Diagnostics such as UDS ISO-14229, KWP2000, OBD-II, etc."
authors = ["Yuri Astrakhan <YuriAstrakhan@gmail.com>", "Ashcon Mohseninia <ashconm@outlook.com>"]
repository = "https://github.com/nyurik/automotive_diag"
Expand Down Expand Up @@ -29,3 +29,12 @@ displaydoc = { version = "0.2", optional = true }

[dev-dependencies]
cargo-husky = { version = "1", features = ["user-hooks"], default-features = false }

[lints.rust]
unsafe_code = "forbid"
unused_qualifications = "warn"

[lints.clippy]
pedantic = { level = "warn", priority = -1 }
missing_errors_doc = "allow"
module_name_repetitions = "allow"
2 changes: 1 addition & 1 deletion LICENSE-MIT
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
MIT License

Copyright (c) 2021 Ashcon Mohseninia
Copyright (c) 2023 Yuri Astrakhan
Copyright (c) 2023-2024 Yuri Astrakhan

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
Expand Down
30 changes: 15 additions & 15 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,12 +1,12 @@
# automotive_diag
# Automotive diagnostics in Rust

[![GitHub](https://img.shields.io/badge/github-nyurik/automotive_diag-8da0cb?logo=github)](https://github.com/nyurik/automotive_diag)
[![crates.io version](https://img.shields.io/crates/v/automotive_diag.svg)](https://crates.io/crates/automotive_diag)
[![docs.rs docs](https://docs.rs/automotive_diag/badge.svg)](https://docs.rs/automotive_diag)
[![crates.io version](https://img.shields.io/crates/l/automotive_diag.svg)](https://github.com/nyurik/automotive_diag/blob/main/LICENSE-APACHE)
[![CI build](https://github.com/nyurik/automotive_diag/actions/workflows/ci.yml/badge.svg)](https://github.com/nyurik/automotive_diag/actions)

This crate provides low-level no_std structs and enums of
This crate provides low-level `no_std` structs and enums of
the [Unified Diagnostic Services](https://en.wikipedia.org/wiki/Unified_Diagnostic_Services) (ISO-14229-1),
[KWP2000](https://en.wikipedia.org/wiki/Keyword_Protocol_2000) (ISO-142330) and
[OBD-II](https://en.wikipedia.org/wiki/On-board_diagnostics) (ISO-9141)
Expand All @@ -29,20 +29,20 @@ use automotive_diag::uds::UdsCommandByte;

/// Handle a single command byte on the ECU side
fn handle_cmd_byte(cmd: u8) {
match UdsCommandByte::from(cmd) {
Standard(DiagnosticSessionControl) => {
// handle_diag_session()
match UdsCommandByte::from(cmd) {
Standard(DiagnosticSessionControl) => {
// handle_diag_session()
}
Standard(ECUReset) => {
// handle_ecu_reset()
}
Extended(0x42) => {
// handle_custom_cmd_42()
}
_ => {
// handle all other commands
}
}
Standard(ECUReset) => {
// handle_ecu_reset()
}
Extended(0x42) => {
// handle_custom_cmd_42()
}
_ => {
// handle all other commands
}
}
}
```

Expand Down
35 changes: 28 additions & 7 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,46 @@ clean:
cargo clean
rm -f Cargo.lock

# Run cargo fmt and cargo clippy
lint: fmt clippy
# Run cargo clippy
clippy:
cargo clippy --bins --tests --lib --benches --examples -- -D warnings
cargo clippy --no-default-features -- -D warnings

# Test code formatting
test-fmt:
cargo fmt --all -- --check

# Run cargo fmt
fmt:
cargo +nightly fmt -- --config imports_granularity=Module,group_imports=StdExternalCrate

# Run cargo clippy
clippy:
cargo clippy --workspace --all-targets --bins --tests --lib --benches -- -D warnings

# Build and open code documentation
docs:
cargo doc --no-deps --open

# Quick compile
check:
RUSTFLAGS='-D warnings' cargo check
RUSTFLAGS='-D warnings' cargo check --no-default-features --features with-uds


# Run all tests
test:
./.cargo-husky/hooks/pre-push
RUSTFLAGS='-D warnings' cargo test
RUSTFLAGS='-D warnings' cargo test --no-default-features --features with-uds
RUSTFLAGS='-D warnings' cargo test --no-default-features

# Test documentation
test-doc:
cargo test --doc
RUSTDOCFLAGS="-D warnings" cargo doc --no-deps

# Run all tests as expected by CI
ci-test: && test-fmt clippy check test test-doc
rustc --version
cargo --version

# Run minimal subset of tests to ensure compatibility with MSRV
ci-test-msrv: && check test
rustc --version
cargo --version
11 changes: 0 additions & 11 deletions src/kwp2000/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,34 +35,23 @@ pub enum KwpCommand {
DisableNormalMessageTransmission = 0x28,
/// Enables normal CAN message transmission from an ECU.
EnableNormalMessageTransmission = 0x29,
///
DynamicallyDefineLocalIdentifier = 0x2C,
///
WriteDataByIdentifier = 0x2E,
///
InputOutputControlByLocalIdentifier = 0x30,
/// Starts a ECU routine given a local identifier.
StartRoutineByLocalIdentifier = 0x31,
/// Stops a ECU routine given a local identifier.
StopRoutineByLocalIdentifier = 0x32,
/// requests results of an executed routine given a local identifier.
RequestRoutineResultsByLocalIdentifier = 0x33,
///
RequestDownload = 0x34,
///
RequestUpload = 0x35,
///
TransferData = 0x36,
///
RequestTransferExit = 0x37,
///
WriteDataByLocalIdentifier = 0x3B,
///
WriteMemoryByAddress = 0x3D,
/// Tester present message.
TesterPresent = 0x3E,
///
ControlDTCSettings = 0x85,
///
ResponseOnEvent = 0x86,
}
3 changes: 0 additions & 3 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,5 @@
#![cfg_attr(feature = "default", doc = include_str!("../README.md"))]
#![cfg_attr(not(feature = "std"), no_std)]
#![forbid(unsafe_code)]
#![deny(clippy::all, clippy::pedantic)]
#![allow(clippy::missing_errors_doc, clippy::module_name_repetitions)]

#[cfg(feature = "with-kwp2000")]
pub mod kwp2000;
Expand Down
1 change: 1 addition & 0 deletions src/obd2/obd_standard.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ enum_wrapper!(obd2, ObdStandard, ObdStandardByte);
#[repr(u8)]
#[derive(EnumRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[allow(clippy::doc_markdown)]
pub enum ObdStandard {
/// OBD-II as defined by the CARB
OBD_II_CARB = 1,
Expand Down
2 changes: 1 addition & 1 deletion src/uds/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ pub enum UdsError {
/// The client has tried to obtain security access to the ECU too many times with
/// incorrect keys
ExceedNumberOfAttempts = 0x36,
/// The client has tried to request seed_key's too quickly, before the ECU timeout's period
/// The client has tried to request `seed_key`'s too quickly, before the ECU timeout's period
/// has expired
RequiredTimeDelayNotExpired = 0x37,
/// The ECU cannot accept the requested upload/download request due to a fault condition
Expand Down
1 change: 1 addition & 0 deletions src/uds/read_dtc_information.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ enum_wrapper!(uds, DtcSubFunction, DtcSubFunctionByte);
#[repr(u8)]
#[derive(EnumRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[allow(clippy::doc_markdown)]
pub enum DtcSubFunction {
/// This function takes a 1 byte DTCStatusMask
ReportNumberOfDtcByStatusMask = 0x01,
Expand Down
2 changes: 1 addition & 1 deletion src/uds/reset_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ pub enum ResetType {
/// Enables a rapid power shutdown on the ECU during a key-off cycle.
///
/// IMPORTANT: Once this has been used, the diagnostic server **cannot** send
/// any other messages other than ECUReset in order to not disturb the rapid power
/// any other messages other than `ECUReset` in order to not disturb the rapid power
/// shutdown function.
EnableRapidPowerShutDown = 0x04,

Expand Down
2 changes: 1 addition & 1 deletion src/uds/session_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ enum_wrapper!(uds, UdsSessionType, UdsSessionTypeByte);
#[derive(EnumRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
pub enum UdsSessionType {
/// Default diagnostic session mode (ECU is normally in this mode on startup)
/// This session type does not require the diagnostic server to sent TesterPresent messages
/// This session type does not require the diagnostic server to sent `TesterPresent` messages
Default = 0x01,

/// This diagnostic session mode enables all diagnostic services related to flashing or programming
Expand Down

0 comments on commit 6c541bd

Please sign in to comment.