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

Removed num enum, updated derives #13

Merged
merged 1 commit into from
Mar 3, 2024
Merged
Show file tree
Hide file tree
Changes from all 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
3 changes: 0 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,3 @@ categories = [
"no-std::no-alloc",
"parser-implementations"
]

[dependencies]
num_enum = { version = "0.7.0" }
69 changes: 59 additions & 10 deletions src/errors.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,5 @@
//! Error enums and flags

use num_enum::{FromPrimitive, IntoPrimitive};

/// A result type which uses the [Error] enum as the error type.
pub type Result<T> = core::result::Result<T, Error>;

Expand All @@ -10,7 +8,7 @@ pub type Result<T> = core::result::Result<T, Error>;
/// These errors occur during parsing or other calculations when those
/// calculations fail. The [Error::SystemError] variant
/// contains any errors generated by the 1553 bus.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum Error {
/// An index was out of bounds for the data structure
Expand Down Expand Up @@ -63,7 +61,7 @@ pub enum Error {
///
/// These errors are generated during runtime by terminals and
/// provided in messages on the bus.
#[derive(Debug, Clone, PartialEq, Eq)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum SystemError {
/// No error
Expand All @@ -89,11 +87,10 @@ pub enum SystemError {
/// This flag is described on page 35 in the MIL-STD-1553 Tutorial[^1].
///
/// [^1]: [MIL-STD-1553 Tutorial](http://www.horntech.cn/techDocuments/MIL-STD-1553Tutorial.pdf)
#[derive(Debug, Clone, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum TerminalError {
/// No error
#[default]
None = 0,

/// An error has occurred
Expand All @@ -112,6 +109,24 @@ impl TerminalError {
}
}

impl From<u8> for TerminalError {
fn from(value: u8) -> Self {
match value {
1 => Self::Error,
_ => Self::None
}
}
}

impl From<TerminalError> for u8 {
fn from(value: TerminalError) -> Self {
match value {
TerminalError::Error => 1,
TerminalError::None => 0
}
}
}

/// This flag provides health data regarding subsystems of a remote terminal.
///
/// The Subsystem Flag bit located at bit time 17 (index 13) is used to provide
Expand All @@ -124,11 +139,10 @@ impl TerminalError {
/// This flag is described on page 34 in the MIL-STD-1553 Tutorial[^1].
///
/// [^1]: [MIL-STD-1553 Tutorial](http://www.horntech.cn/techDocuments/MIL-STD-1553Tutorial.pdf)
#[derive(Debug, Clone, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum SubsystemError {
/// No error
#[default]
None = 0,

/// An error has occurred
Expand All @@ -147,6 +161,24 @@ impl SubsystemError {
}
}

impl From<u8> for SubsystemError {
fn from(value: u8) -> Self {
match value {
1 => Self::Error,
_ => Self::None
}
}
}

impl From<SubsystemError> for u8 {
fn from(value: SubsystemError) -> Self {
match value {
SubsystemError::Error => 1,
SubsystemError::None => 0
}
}
}

/// This flag is set when a receiving terminal detects an error in a message.
///
/// The error may have occurred in any of the data words within the message, and
Expand All @@ -159,11 +191,10 @@ impl SubsystemError {
/// This flag is described on page 32 in the MIL-STD-1553 Tutorial[^1].
///
/// [^1]: [MIL-STD-1553 Tutorial](http://www.horntech.cn/techDocuments/MIL-STD-1553Tutorial.pdf)
#[derive(Debug, Clone, PartialEq, Eq, IntoPrimitive, FromPrimitive)]
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
#[repr(u8)]
pub enum MessageError {
/// No error
#[default]
None = 0,

/// An error has occurred
Expand All @@ -181,3 +212,21 @@ impl MessageError {
matches!(self, Self::Error)
}
}

impl From<u8> for MessageError {
fn from(value: u8) -> Self {
match value {
1 => Self::Error,
_ => Self::None
}
}
}

impl From<MessageError> for u8 {
fn from(value: MessageError) -> Self {
match value {
MessageError::Error => 1,
MessageError::None => 0
}
}
}
1 change: 1 addition & 0 deletions src/fields.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
///
/// Given a mask and offset, the Field struct can get
/// or set between 1 and 8 bits in a u16 word.
#[derive(Copy, Clone, Eq, PartialEq, Ord, PartialOrd, Hash, Debug)]
pub struct Field {
/// The mask used to isolate the value
mask: u16,
Expand Down
Loading