Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Add support for custom exception codes #279

Merged
merged 14 commits into from
Sep 11, 2024
Merged
Show file tree
Hide file tree
Changes from 13 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,14 @@

# Changelog

## v0.15.0 (Unreleased)

- Added `Exception::Custom` and `Exception::new`.

### Breaking Changes

- Removed `TryFrom<u8>` and `#[repr(u8)]` for Exception.

## v0.14.1 (2024-09-10)

- Implement `Report Server ID` (function code 17).
Expand Down
25 changes: 1 addition & 24 deletions src/codec/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -385,37 +385,14 @@ impl TryFrom<Bytes> for ExceptionResponse {
));
}
let function = fn_err_code - 0x80;
let exception = Exception::try_from(rdr.read_u8()?)?;
let exception = Exception::new(rdr.read_u8()?);
Ok(ExceptionResponse {
function: FunctionCode::new(function),
exception,
})
}
}

impl TryFrom<u8> for Exception {
type Error = Error;

fn try_from(code: u8) -> Result<Self, Self::Error> {
use crate::frame::Exception::*;
let ex = match code {
0x01 => IllegalFunction,
0x02 => IllegalDataAddress,
0x03 => IllegalDataValue,
0x04 => ServerDeviceFailure,
0x05 => Acknowledge,
0x06 => ServerDeviceBusy,
0x08 => MemoryParityError,
0x0A => GatewayPathUnavailable,
0x0B => GatewayTargetDevice,
_ => {
return Err(Error::new(ErrorKind::InvalidData, "Invalid exception code"));
}
};
Ok(ex)
}
}

impl TryFrom<Bytes> for ResponsePdu {
type Error = Error;

Expand Down
67 changes: 56 additions & 11 deletions src/frame/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -387,26 +387,70 @@ impl Response {

/// A server (slave) exception.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum Exception {
IllegalFunction = 0x01,
IllegalDataAddress = 0x02,
IllegalDataValue = 0x03,
ServerDeviceFailure = 0x04,
Acknowledge = 0x05,
ServerDeviceBusy = 0x06,
MemoryParityError = 0x08,
GatewayPathUnavailable = 0x0A,
GatewayTargetDevice = 0x0B,
/// 0x01
IllegalFunction,
/// 0x02
IllegalDataAddress,
/// 0x03
IllegalDataValue,
/// 0x04
ServerDeviceFailure,
/// 0x05
Acknowledge,
/// 0x06
ServerDeviceBusy,
/// 0x08
MemoryParityError,
/// 0x0A
GatewayPathUnavailable,
/// 0x0B
GatewayTargetDevice,
/// None of the above.
///
/// Although encoding one of the predefined values as this is possible, it is not recommended,
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please split into two sentences for readability.

/// instead prefer to use [`Self::new()`] to prevent such ambiguities.
Custom(u8),
}

impl From<Exception> for u8 {
fn from(from: Exception) -> Self {
from as u8
use crate::frame::Exception::*;
match from {
IllegalFunction => 0x01,
IllegalDataAddress => 0x02,
IllegalDataValue => 0x03,
ServerDeviceFailure => 0x04,
Acknowledge => 0x05,
ServerDeviceBusy => 0x06,
MemoryParityError => 0x08,
GatewayPathUnavailable => 0x0A,
GatewayTargetDevice => 0x0B,
Custom(code) => code,
}
}
}

impl Exception {
/// Create a new [`Exception`] with `value`.
#[must_use]
pub const fn new(value: u8) -> Self {
use crate::frame::Exception::*;

match value {
0x01 => IllegalFunction,
0x02 => IllegalDataAddress,
0x03 => IllegalDataValue,
0x04 => ServerDeviceFailure,
0x05 => Acknowledge,
0x06 => ServerDeviceBusy,
0x08 => MemoryParityError,
0x0A => GatewayPathUnavailable,
0x0B => GatewayTargetDevice,
other => Custom(other),
}
}

pub(crate) fn description(&self) -> &str {
use crate::frame::Exception::*;

Expand All @@ -420,6 +464,7 @@ impl Exception {
MemoryParityError => "Memory parity error",
GatewayPathUnavailable => "Gateway path unavailable",
GatewayTargetDevice => "Gateway target device failed to respond",
Custom(_) => "Custom",
}
}
}
Expand Down