buss_protocol/
actions.rs

1use std::fmt::Display;
2
3/// This is an enum of values that tell the server what action
4/// to perform and the client what was the response code of the
5/// action performed. There are possible 256 different actions possible and response codes of between 0 and 255.
6#[repr(u8)]
7#[derive(Copy, Clone)]
8pub enum BussAction {
9    /// do nothing
10    Noop = 0,
11    /// Read request
12    Read,
13    /// Write request
14    Write,
15    /// Modify request
16    Modify,
17    /// Delete request
18    Delete,
19}
20
21/// Parsing error for buss action
22#[derive(Debug)]
23pub enum BussActionError {
24    /// Unexpected value was encountered which can not be handled internally
25    Unhandled(u8),
26}
27impl Display for BussActionError {
28    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
29        match self {
30            Self::Unhandled(value) => write!(
31                f,
32                "Unexpected action value 0x{:x} which has not been implemented internally.",
33                value
34            ),
35        }
36    }
37}
38
39impl TryFrom<u8> for BussAction {
40    type Error = BussActionError;
41    fn try_from(value: u8) -> Result<Self, Self::Error> {
42        match value {
43            0 => Ok(BussAction::Noop),
44            1 => Ok(BussAction::Read),
45            2 => Ok(BussAction::Write),
46            3 => Ok(BussAction::Modify),
47            4 => Ok(BussAction::Delete),
48            _ => Err(BussActionError::Unhandled(value)),
49        }
50    }
51}