Skip to content

Commit

Permalink
rustfmt
Browse files Browse the repository at this point in the history
  • Loading branch information
divi255 committed Feb 23, 2024
1 parent 83833bc commit 03d9e0d
Show file tree
Hide file tree
Showing 3 changed files with 52 additions and 53 deletions.
35 changes: 19 additions & 16 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,22 +40,21 @@ impl ErrorKind {
}
}

pub fn is_modbus_error(&self) -> bool
{
pub fn is_modbus_error(&self) -> bool {
use ErrorKind::*;

match self {
IllegalFunction |
IllegalDataAddress |
IllegalDataValue |
SlaveDeviceFailure |
Acknowledge |
SlaveDeviceBusy |
NegativeAcknowledge |
MemoryParityError |
GatewayPathUnavailable |
GatewayTargetFailed => true,
_ => false
IllegalFunction
| IllegalDataAddress
| IllegalDataValue
| SlaveDeviceFailure
| Acknowledge
| SlaveDeviceBusy
| NegativeAcknowledge
| MemoryParityError
| GatewayPathUnavailable
| GatewayTargetFailed => true,
_ => false,
}
}

Expand All @@ -73,7 +72,7 @@ impl ErrorKind {
MemoryParityError => Ok(8),
GatewayPathUnavailable => Ok(9),
GatewayTargetFailed => Ok(10),
_ => Err(self.clone())
_ => Err(self.clone()),
}
}
}
Expand Down Expand Up @@ -102,8 +101,12 @@ impl core::fmt::Display for ErrorKind {
}
ErrorKind::UnknownError => "UNKNOWN MODBUS ERROR",
ErrorKind::Utf8Error => "UTF8 CONVERTION ERROR",
ErrorKind::ReadCallOnWriteFrame => "FRAME DESCRIBING WRITE HAD FUNCTION CALLED FOR FRAMES DESCRIBING READ",
ErrorKind::WriteCallOnReadFrame => "FRAME DESCRIBING READ HAD FUNCTION CALLED FOR FRAMES DESCRIBING WRITE",
ErrorKind::ReadCallOnWriteFrame => {
"FRAME DESCRIBING WRITE HAD FUNCTION CALLED FOR FRAMES DESCRIBING READ"
}
ErrorKind::WriteCallOnReadFrame => {
"FRAME DESCRIBING READ HAD FUNCTION CALLED FOR FRAMES DESCRIBING WRITE"
}
};
write!(f, "{}", msg)
}
Expand Down
46 changes: 15 additions & 31 deletions src/server/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
return Err(ErrorKind::OOB);
}
#[allow(clippy::cast_possible_truncation)]
let crc = calc_crc16(self.response.as_slice(), len as u8);
let crc = calc_crc16(self.response.as_slice(), len as u8);
self.response.extend(&crc.to_le_bytes())
}
ModbusProto::Ascii => {
Expand All @@ -136,7 +136,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
return Err(ErrorKind::OOB);
}
#[allow(clippy::cast_possible_truncation)]
let lrc = calc_lrc(self.response.as_slice(), len as u8);
let lrc = calc_lrc(self.response.as_slice(), len as u8);
self.response.push(lrc)
}
ModbusProto::TcpUdp => Ok(()),
Expand Down Expand Up @@ -213,10 +213,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
Ok(())
}
}
MODBUS_GET_HOLDINGS
| MODBUS_GET_INPUTS
| MODBUS_GET_COILS
| MODBUS_GET_DISCRETES => {
MODBUS_GET_HOLDINGS | MODBUS_GET_INPUTS | MODBUS_GET_COILS | MODBUS_GET_DISCRETES => {
return Err(ErrorKind::ReadCallOnWriteFrame);
}
_ => Ok(()),
Expand Down Expand Up @@ -296,10 +293,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {

return Ok(write);
}
MODBUS_GET_HOLDINGS
| MODBUS_GET_INPUTS
| MODBUS_GET_COILS
| MODBUS_GET_DISCRETES => {
MODBUS_GET_HOLDINGS | MODBUS_GET_INPUTS | MODBUS_GET_COILS | MODBUS_GET_DISCRETES => {
return Err(ErrorKind::ReadCallOnWriteFrame);
}
_ => {
Expand Down Expand Up @@ -330,9 +324,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
self.response
.extend(&self.buf[self.frame_start..self.frame_start + 6])
}
MODBUS_GET_HOLDINGS
| MODBUS_GET_INPUTS
| MODBUS_GET_COILS
MODBUS_GET_HOLDINGS | MODBUS_GET_INPUTS | MODBUS_GET_COILS
| MODBUS_GET_DISCRETES => {
return Err(ErrorKind::ReadCallOnWriteFrame);
}
Expand All @@ -346,9 +338,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
self.set_modbus_error_if_unset(&e);
return Ok(());
}
Err(e) => {
Err(e)
}
Err(e) => Err(e),
}
}

Expand Down Expand Up @@ -514,9 +504,7 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
match read_result {
Ok(_) => {
match self.func {
MODBUS_GET_COILS
| MODBUS_GET_DISCRETES
| MODBUS_GET_HOLDINGS
MODBUS_GET_COILS | MODBUS_GET_DISCRETES | MODBUS_GET_HOLDINGS
| MODBUS_GET_INPUTS => {
return Ok(());
}
Expand Down Expand Up @@ -717,12 +705,11 @@ impl<'a, V: VectorTrait<u8>> ModbusFrame<'a, V> {
pub fn set_modbus_error_if_unset(&mut self, err: &ErrorKind) {
if self.error == 0 && err.is_modbus_error() {
// leave 0 bytes for RTU/ASCII, leave 4 bytes for TCP/UDP (Transaction ID and Protocol ID)
let len_leave_before_finalize =
if self.proto == ModbusProto::TcpUdp {
4
} else {
0
};
let len_leave_before_finalize = if self.proto == ModbusProto::TcpUdp {
4
} else {
0
};

self.response.resize(len_leave_before_finalize, 0);
self.error = err
Expand Down Expand Up @@ -764,26 +751,23 @@ pub enum Write<'a> {

/// See [`get_external_read`](ModbusFrame::get_external_read)
#[derive(Debug, Eq, PartialEq)]
pub struct ReadBits<'a>
{
pub struct ReadBits<'a> {
pub address: u16,
pub count: u16,
pub buf: &'a mut [u8],
}

/// See [`get_external_read`](ModbusFrame::get_external_read)
#[derive(Debug, Eq, PartialEq)]
pub struct ReadWords<'a>
{
pub struct ReadWords<'a> {
pub address: u16,
pub count: u16,
pub buf: &'a mut [u8],
}

/// See [`get_external_read`](ModbusFrame::get_external_read)
#[derive(Debug, Eq, PartialEq)]
pub enum Read<'a>
{
pub enum Read<'a> {
Bits(ReadBits<'a>),
Words(ReadWords<'a>),
}
24 changes: 18 additions & 6 deletions src/vector.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,9 +58,13 @@ impl<T: Copy> VectorTrait<T> for Vec<T> {
Vec::as_slice(self)
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] { Vec::as_mut_slice(self) }
fn as_mut_slice(&mut self) -> &mut [T] {
Vec::as_mut_slice(self)
}
#[inline]
fn resize(&mut self, new_len: usize, value: T) { Vec::resize(self, new_len, value) }
fn resize(&mut self, new_len: usize, value: T) {
Vec::resize(self, new_len, value)
}
#[inline]
fn replace(&mut self, index: usize, value: T) {
self[index] = value;
Expand Down Expand Up @@ -105,9 +109,13 @@ impl<'a, T: Copy> VectorTrait<T> for FixedVec<'a, T> {
FixedVec::as_slice(self)
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] { FixedVec::as_mut_slice(self) }
fn as_mut_slice(&mut self) -> &mut [T] {
FixedVec::as_mut_slice(self)
}
#[inline]
fn resize(&mut self, new_len: usize, value: T) { FixedVec::resize(self, new_len, value) }
fn resize(&mut self, new_len: usize, value: T) {
FixedVec::resize(self, new_len, value)
}
#[inline]
fn replace(&mut self, index: usize, value: T) {
self[index] = value;
Expand Down Expand Up @@ -152,9 +160,13 @@ impl<T: Copy, const N: usize> VectorTrait<T> for HeaplessVec<T, N> {
HeaplessVec::as_slice(self)
}
#[inline]
fn as_mut_slice(&mut self) -> &mut [T] { HeaplessVec::as_mut_slice(self) }
fn as_mut_slice(&mut self) -> &mut [T] {
HeaplessVec::as_mut_slice(self)
}
#[inline]
fn resize(&mut self, new_len: usize, value: T) { HeaplessVec::resize(self, new_len, value)? }
fn resize(&mut self, new_len: usize, value: T) {
HeaplessVec::resize(self, new_len, value)?

Check failure on line 168 in src/vector.rs

View workflow job for this annotation

GitHub Actions / Compile (no_std)

the `?` operator can only be used in a method that returns `Result` or `Option` (or another type that implements `FromResidual`)
}
#[inline]
fn replace(&mut self, index: usize, value: T) {
self[index] = value;
Expand Down

0 comments on commit 03d9e0d

Please sign in to comment.