Skip to content

Commit

Permalink
Add optional serde feature (#12)
Browse files Browse the repository at this point in the history
  • Loading branch information
nyurik authored Jul 1, 2024
1 parent 90bdc5c commit f4ae6c7
Show file tree
Hide file tree
Showing 26 changed files with 61 additions and 2 deletions.
6 changes: 5 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ edition = "2021"
license = "MIT OR Apache-2.0"
keywords = ["uds", "kwp2000", "obd", "automotive", "no_std"]
categories = ["embedded", "no-std", "encoding", "parsing"]
rust-version = "1.61.0"
rust-version = "1.63.0"

[features]
default = ["std", "iter", "display", "with-kwp2000", "with-obd2", "with-uds"]
Expand All @@ -24,13 +24,17 @@ with-kwp2000 = []
with-obd2 = []
# Include support for Unified Diagnostic Services
with-uds = []
# Include support for serde serialization
serde = ["dep:serde"]

[dependencies]
displaydoc = { version = "0.2", optional = true }
serde = { version = "1", features = ["derive"], optional = true }
strum = { version = "0.26.3", features = ["derive"] }

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

[lints.rust]
unsafe_code = "forbid"
Expand Down
1 change: 1 addition & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ test:
RUSTFLAGS='-D warnings' cargo test --no-default-features --features with-kwp2000
RUSTFLAGS='-D warnings' cargo test --no-default-features --features with-obd2
RUSTFLAGS='-D warnings' cargo test --no-default-features --features with-uds
RUSTFLAGS='-D warnings' cargo test --features serde

# Test documentation
test-doc:
Expand Down
1 change: 1 addition & 0 deletions src/kwp2000/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ crate::utils::enum_wrapper!(kwp2000, KwpCommand, KwpCommandByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KwpCommand {
/// Start or change ECU diagnostic session mode.
StartDiagnosticSession = 0x10,
Expand Down
1 change: 1 addition & 0 deletions src/kwp2000/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ crate::utils::enum_wrapper!(kwp2000, KwpError, KwpErrorByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KwpError {
/// ECU rejected the request for unknown reason
GeneralReject = 0x10,
Expand Down
1 change: 1 addition & 0 deletions src/kwp2000/reset_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ crate::utils::enum_wrapper!(kwp2000, ResetType, ResetTypeByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResetType {
/// Simulates a power off/on reset of the ECU.
PowerOnReset = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/kwp2000/routine_exit_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ crate::utils::enum_wrapper!(kwp2000, RoutineExitStatus, RoutineExitStatusByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RoutineExitStatus {
/// Normal exit with results available
NormalExitWithResults = 0x61,
Expand Down
1 change: 1 addition & 0 deletions src/kwp2000/session_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ crate::utils::enum_wrapper!(kwp2000, KwpSessionType, KwpSessionTypeByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum KwpSessionType {
/// Normal session. The ECU will typically boot in this state.
/// In this mode, only non-intrusive functions are supported.
Expand Down
1 change: 1 addition & 0 deletions src/obd2/command_2nd_air_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate::utils::enum_wrapper!(
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CommandedSecondaryAirStatus {
/// Upstream
Upstream = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/obd2/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(obd2, Obd2Command, Obd2CommandByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Obd2Command {
/// Service 01 - Show current data
Service01 = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/obd2/data_pids.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ crate::utils::enum_wrapper!(obd2, DataPid, DataPidByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum DataPid {
PidSupport0120 = 0x00,
StatusSinceDTCCleared = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/obd2/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(obd2, Obd2Error, Obd2ErrorByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Obd2Error {
/// ECU general reject
GeneralReject = 0x10,
Expand Down
1 change: 1 addition & 0 deletions src/obd2/fuel_system_status.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(obd2, FuelSystemStatus, FuelSystemStatusByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FuelSystemStatus {
/// The motor is off
Off = 0x00,
Expand Down
1 change: 1 addition & 0 deletions src/obd2/fuel_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ crate::utils::enum_wrapper!(obd2, FuelTypeCoding, FuelTypeCodingByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum FuelTypeCoding {
/// Fuel type unavailable
NotAvailable = 0,
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 @@ -6,6 +6,7 @@ crate::utils::enum_wrapper!(obd2, ObdStandard, ObdStandardByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(clippy::doc_markdown)]
pub enum ObdStandard {
/// OBD-II as defined by the CARB
Expand Down
1 change: 1 addition & 0 deletions src/obd2/service09.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(obd2, Service09Pid, Service09PidByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum Service09Pid {
/// VIN message count (Only for LIN)
VinMsgCount = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/uds/comm_level.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ crate::utils::enum_wrapper!(uds, CommunicationLevel, CommunicationLevelByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum CommunicationLevel {
/// This value indicates that the reception and transmission of messages
/// shall be enabled for the specified communicationType.
Expand Down
31 changes: 31 additions & 0 deletions src/uds/commands.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(uds, UdsCommand, UdsCommandByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UdsCommand {
/// The client requests to control a diagnostic session with a server(s).
DiagnosticSessionControl = 0x10,
Expand Down Expand Up @@ -59,3 +60,33 @@ pub enum UdsCommand {
/// The client requests the negotiation of a file transfer between server and client.
RequestFileTransfer = 0x38,
}

#[cfg(all(test, feature = "serde"))]
mod tests {
use super::*;

#[derive(serde::Serialize, serde::Deserialize)]
struct TestStruct {
command: UdsCommand,
command_byte: UdsCommandByte,
}

#[test]
fn test_serde() {
let test = TestStruct {
command: UdsCommand::DiagnosticSessionControl,
command_byte: UdsCommandByte::from(UdsCommand::DiagnosticSessionControl),
};

let json = serde_json::to_string(&test).unwrap();
assert_eq!(
json,
r###"{"command":"DiagnosticSessionControl","command_byte":{"Standard":"DiagnosticSessionControl"}}"###
);

let deserialized: TestStruct = serde_json::from_str(&json).unwrap();

assert_eq!(test.command, deserialized.command);
assert_eq!(test.command_byte, deserialized.command_byte);
}
}
1 change: 1 addition & 0 deletions src/uds/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ enum_wrapper!(uds, UdsError, UdsErrorByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum UdsError {
/// ECU rejected the request (No specific error)
GeneralReject = 0x10,
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 @@ -9,6 +9,7 @@ enum_wrapper!(uds, DtcSubFunction, DtcSubFunctionByte);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[allow(clippy::doc_markdown)]
pub enum DtcSubFunction {
/// This function takes a 1 byte DTCStatusMask
Expand Down
1 change: 1 addition & 0 deletions src/uds/reset_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ crate::utils::enum_wrapper!(uds, ResetType, ResetTypeByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ResetType {
/// Signals the ECU to perform a hard-reset,
/// simulating a forceful power off/on cycle
Expand Down
1 change: 1 addition & 0 deletions src/uds/routine.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ crate::utils::enum_wrapper!(uds, RoutineControlType, RoutineControlTypeByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum RoutineControlType {
/// Launches a routine on the ECU
StartRoutine = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/uds/scaling_byte.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ crate::utils::enum_impls!(uds, ScalingType);
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "display", derive(displaydoc::Display))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ScalingType {
/// Unsigned numeric integer. Must be followed by 1..4 bytes, given as a low nibble of the byte.
UnsignedNumeric = 0x00,
Expand Down
3 changes: 2 additions & 1 deletion src/uds/scaling_byte_ext.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,8 @@ generate_enum! {
/// or [`ScalingExtension::get_prefix`] to return the optional prefix of the scaling byte.
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ScalingExtension {
/// No unit or presentation
NoUnit = 0x00,
Expand Down
1 change: 1 addition & 0 deletions src/uds/security_access.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ crate::utils::enum_wrapper!(uds, SecurityOperation, SecurityOperationByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum SecurityOperation {
/// Asks the ECU for a security seed
RequestSeed = 0x01,
Expand Down
1 change: 1 addition & 0 deletions src/uds/session_types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ crate::utils::enum_wrapper!(uds, UdsSessionType, UdsSessionTypeByte);
#[repr(u8)]
#[derive(strum::FromRepr, Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "iter", derive(strum::EnumIter))]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
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
Expand Down
1 change: 1 addition & 0 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use core::fmt::Debug;
/// A wrapper around a byte, which can be either an ISO-standardized value for a specific enum,
/// or an implementation-specific/invalid `Extended` value wrapping original byte.
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
pub enum ByteWrapper<T> {
Standard(T),
Extended(u8),
Expand Down

0 comments on commit f4ae6c7

Please sign in to comment.