Convert to error states
This commit is contained in:
parent
8f6fbb4778
commit
cd3282a69d
@ -85,6 +85,33 @@ pub enum ErrorState {
|
|||||||
UnknownError = 0xFF,
|
UnknownError = 0xFF,
|
||||||
}
|
}
|
||||||
|
|
||||||
|
impl TryFrom<&u8> for ErrorState {
|
||||||
|
type Error= &'static str;
|
||||||
|
|
||||||
|
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||||
|
match b {
|
||||||
|
0x00 => Ok(Self::NoError),
|
||||||
|
0x01 => Ok(Self::InvalidRPCPacket),
|
||||||
|
0x02 => Ok(Self::UnknownRPCCommand),
|
||||||
|
0x03 => Ok(Self::UnableToConnect),
|
||||||
|
0x04 => Ok(Self::UnknownError),
|
||||||
|
_ => Err("Cannot convert to error type"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
impl std::fmt::Display for ErrorState {
|
||||||
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||||
|
match self {
|
||||||
|
Self::NoError => write!(f, "No Error"),
|
||||||
|
Self::InvalidRPCPacket => write!(f, "Invalid RPC Packet"),
|
||||||
|
Self::UnknownRPCCommand => write!(f, "Unknown RPC Command"),
|
||||||
|
Self::UnableToConnect => write!(f, "Unable To Connect"),
|
||||||
|
Self::UnknownError => write!(f, "Unknown Error"),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
#[derive(Clone)]
|
#[derive(Clone)]
|
||||||
#[repr(u8)]
|
#[repr(u8)]
|
||||||
pub enum RPCCommand {
|
pub enum RPCCommand {
|
||||||
@ -187,6 +214,7 @@ impl RawPacket {
|
|||||||
|
|
||||||
pub enum ImprovPacket {
|
pub enum ImprovPacket {
|
||||||
CurrentStateResponse(CurrentStateResponse),
|
CurrentStateResponse(CurrentStateResponse),
|
||||||
|
ErrorState(ErrorStatePacket),
|
||||||
RequestCurrentStateCommand(RequestCurrentStateCommand),
|
RequestCurrentStateCommand(RequestCurrentStateCommand),
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -194,7 +222,7 @@ impl ImprovPacket {
|
|||||||
pub fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, &str> {
|
pub fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, &str> {
|
||||||
match raw_packet.r#type {
|
match raw_packet.r#type {
|
||||||
PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)),
|
PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)),
|
||||||
//PacketType::ErrorState => _,
|
PacketType::ErrorState => Ok(ImprovPacket::ErrorState(ErrorStatePacket::try_from_raw_packet(raw_packet)?)),
|
||||||
//PacketType::RPCCommand => _,
|
//PacketType::RPCCommand => _,
|
||||||
//PacketType::RPCResult => _,
|
//PacketType::RPCResult => _,
|
||||||
_ => Err("Conversion into packet type {} not implemented"),
|
_ => Err("Conversion into packet type {} not implemented"),
|
||||||
@ -232,6 +260,28 @@ impl ImprovDataFromPacket for CurrentStateResponse {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
pub struct ErrorStatePacket {
|
||||||
|
pub error_state: ErrorState,
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImprovDataPacketType for ErrorStatePacket {
|
||||||
|
const packet_type: PacketType = PacketType::ErrorState;
|
||||||
|
}
|
||||||
|
|
||||||
|
impl ImprovDataFromPacket for ErrorStatePacket {
|
||||||
|
type Error = &'static str;
|
||||||
|
|
||||||
|
fn try_from_raw_packet(raw_packet: &RawPacket) -> Result<Self, Self::Error>{
|
||||||
|
if raw_packet.r#type != Self::packet_type {
|
||||||
|
return Err("Packet is not ErrorState");
|
||||||
|
}
|
||||||
|
|
||||||
|
return Ok(Self {
|
||||||
|
error_state: ErrorState::try_from(&raw_packet.data[0])?,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
pub struct RequestCurrentStateCommand {
|
pub struct RequestCurrentStateCommand {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -152,6 +152,10 @@ async fn main() -> Result<()>{
|
|||||||
println!("Current state: {}", ¤t_state_response.current_state);
|
println!("Current state: {}", ¤t_state_response.current_state);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if let ImprovPacket::ErrorState(error_state) = ImprovPacket::try_from_raw_packet(&raw_packet).unwrap() {
|
||||||
|
println!("Error state: {}", &error_state.error_state);
|
||||||
|
}
|
||||||
|
|
||||||
},
|
},
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
Reference in New Issue
Block a user