Skip to content

Commit

Permalink
feat(RecorderError): add a new error type for the frame recorder
Browse files Browse the repository at this point in the history
  • Loading branch information
Kay Grewe authored and Kay Grewe committed Feb 28, 2024
1 parent 6fc31f5 commit 8b085f2
Showing 1 changed file with 30 additions and 0 deletions.
30 changes: 30 additions & 0 deletions rodbus/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
use scursor::WriteError;
use tracing::span::Record;
use crate::InternalError::{RecordDoesNotExist, RecordKeyExists};

/// The task processing requests has terminated
#[derive(Clone, Copy, Debug)]
Expand Down Expand Up @@ -157,6 +159,15 @@ pub enum InvalidRange {
CountTooLargeForType(u16, u16), // actual and limit
}

/// Errors that can be produced when the Frame Recorder is used incorrectly
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
pub enum RecorderError {
///Record Key already in use.
RecordKeyExists(&'static str),
///Record Key not found.
RecordDoesNotExist(&'static str),
}

/// Errors that indicate faulty logic in the library itself if they occur
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum InternalError {
Expand All @@ -170,6 +181,19 @@ pub enum InternalError {
BadSeekOperation,
/// Byte count would exceed maximum allowed size in the ADU of u8
BadByteCount(usize),
/// A position with that name was already recorded.
RecordKeyExists(&'static str),
/// The recorded position was not found under the specified key.
RecordDoesNotExist(&'static str),
}

impl From<RecorderError> for InternalError {
fn from(value: RecorderError) -> Self {
match value {
RecorderError::RecordKeyExists(key) => RecordKeyExists(key),
RecorderError::RecordDoesNotExist(key) => RecordDoesNotExist(key),
}
}
}

impl std::error::Error for InternalError {}
Expand All @@ -195,6 +219,12 @@ impl std::fmt::Display for InternalError {
InternalError::BadByteCount(size) => {
write!(f, "Byte count of in ADU {size} exceeds maximum size of u8")
}
RecordKeyExists(key) => {
write!(f, "The key \"{key}\" is already stored inside the recorder")
},
RecordDoesNotExist(key) => {
write!(f, "The position with the key \"{key}\" was never recorded")
},
}
}
}
Expand Down

0 comments on commit 8b085f2

Please sign in to comment.