Use anyhow in improv lib
This commit is contained in:
parent
b4089a2b7e
commit
3bb08f22c8
@ -1,3 +1,11 @@
|
||||
use anyhow::{
|
||||
anyhow,
|
||||
bail,
|
||||
Context,
|
||||
Error,
|
||||
Result,
|
||||
};
|
||||
|
||||
pub const IMPROV_HEADER: [u8; 6] = [
|
||||
'I' as u8,
|
||||
'M' as u8,
|
||||
@ -19,7 +27,7 @@ pub enum PacketType {
|
||||
}
|
||||
|
||||
impl TryFrom<&u8> for PacketType {
|
||||
type Error= &'static str;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
@ -27,7 +35,7 @@ impl TryFrom<&u8> for PacketType {
|
||||
0x02 => Ok(Self::ErrorState),
|
||||
0x03 => Ok(Self::RPCCommand),
|
||||
0x04 => Ok(Self::RPCResult),
|
||||
_ => Err("Cannot convert to packet type"),
|
||||
_ => Err(anyhow!("Cannot convert to packet type")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -53,14 +61,14 @@ pub enum CurrentState {
|
||||
|
||||
|
||||
impl TryFrom<&u8> for CurrentState {
|
||||
type Error= &'static str;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
0x02 => Ok(Self::Ready),
|
||||
0x03 => Ok(Self::Provisioning),
|
||||
0x04 => Ok(Self::Provisioned),
|
||||
_ => Err("Cannot convert to current state"),
|
||||
_ => Err(anyhow!("Cannot convert to current state")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -86,7 +94,7 @@ pub enum ErrorState {
|
||||
}
|
||||
|
||||
impl TryFrom<&u8> for ErrorState {
|
||||
type Error= &'static str;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
@ -95,7 +103,7 @@ impl TryFrom<&u8> for ErrorState {
|
||||
0x02 => Ok(Self::UnknownRPCCommand),
|
||||
0x03 => Ok(Self::UnableToConnect),
|
||||
0x04 => Ok(Self::UnknownError),
|
||||
_ => Err("Cannot convert to error type"),
|
||||
_ => Err(anyhow!("Cannot convert to error type")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -121,14 +129,14 @@ pub enum RPCCommand {
|
||||
}
|
||||
|
||||
impl TryFrom<&u8> for RPCCommand {
|
||||
type Error= &'static str;
|
||||
type Error = Error;
|
||||
|
||||
fn try_from(b: &u8) -> Result<Self, Self::Error> {
|
||||
match b {
|
||||
0x01 => Ok(Self::SendWifiSettings),
|
||||
0x02 => Ok(Self::RequestCurrentState),
|
||||
0x03 => Ok(Self::RequestDeviceInformation),
|
||||
_ => Err("Cannot convert to RPC command"),
|
||||
_ => Err(anyhow!("Cannot convert to RPC command")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -176,21 +184,21 @@ pub struct RawPacket {
|
||||
}
|
||||
|
||||
impl RawPacket {
|
||||
pub fn try_from_bytes(bytes: &Vec<u8>) -> Result<Self, &str> {
|
||||
pub fn try_from_bytes(bytes: &Vec<u8>) -> Result<Self> {
|
||||
if bytes.len() < 11 {
|
||||
return Err("Packet too small");
|
||||
bail!("Packet too small");
|
||||
}
|
||||
|
||||
for i in 0..5 {
|
||||
if bytes[i] != IMPROV_HEADER[i] {
|
||||
return Err("Improv header not found");
|
||||
bail!("Improv header not found");
|
||||
}
|
||||
}
|
||||
|
||||
let length: usize = bytes[8].into();
|
||||
|
||||
if bytes.len() != length + 10 {
|
||||
return Err("Packet with invalid length");
|
||||
bail!("Packet with invalid length");
|
||||
}
|
||||
|
||||
return Ok(Self {
|
||||
@ -236,13 +244,13 @@ pub enum ImprovPacket {
|
||||
}
|
||||
|
||||
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> {
|
||||
match raw_packet.r#type {
|
||||
PacketType::CurrentState => Ok(ImprovPacket::CurrentStateResponse(CurrentStateResponse::try_from_raw_packet(raw_packet)?)),
|
||||
PacketType::ErrorState => Ok(ImprovPacket::ErrorState(ErrorStatePacket::try_from_raw_packet(raw_packet)?)),
|
||||
//PacketType::RPCCommand => _,
|
||||
PacketType::RPCResult => Ok(ImprovPacket::RPCResult(RPCResultPacket::try_from_raw_packet(raw_packet)?)),
|
||||
_ => Err("Conversion into packet type not implemented"),
|
||||
_ => Err(anyhow!("Conversion into packet type not implemented")),
|
||||
}
|
||||
}
|
||||
}
|
||||
@ -264,11 +272,11 @@ impl ImprovDataToPacket for CurrentStateResponse {
|
||||
}
|
||||
|
||||
impl ImprovDataFromPacket for CurrentStateResponse {
|
||||
type Error = &'static str;
|
||||
type Error = Error;
|
||||
|
||||
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 CurrentStateResponse");
|
||||
return Err(anyhow!("Packet is not CurrentStateResponse"));
|
||||
}
|
||||
|
||||
return Ok(Self {
|
||||
@ -286,11 +294,11 @@ impl ImprovDataPacketType for ErrorStatePacket {
|
||||
}
|
||||
|
||||
impl ImprovDataFromPacket for ErrorStatePacket {
|
||||
type Error = &'static str;
|
||||
type Error = Error;
|
||||
|
||||
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 Err(anyhow!("Packet is not ErrorState"));
|
||||
}
|
||||
|
||||
return Ok(Self {
|
||||
@ -341,11 +349,11 @@ impl ImprovDataPacketType for RPCResultPacket {
|
||||
}
|
||||
|
||||
impl ImprovDataFromPacket for RPCResultPacket {
|
||||
type Error = &'static str;
|
||||
type Error = Error;
|
||||
|
||||
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 RPCResult");
|
||||
return Err(anyhow!("Packet is not RPCResult"));
|
||||
}
|
||||
|
||||
let mut results: Vec<String> = Vec::new();
|
||||
|
Loading…
Reference in New Issue
Block a user