Add error enum
This commit is contained in:
parent
1da05e29e1
commit
7d42a56199
3 changed files with 43 additions and 13 deletions
21
Cargo.lock
generated
21
Cargo.lock
generated
|
@ -179,6 +179,7 @@ dependencies = [
|
||||||
"serde_json",
|
"serde_json",
|
||||||
"serde_repr",
|
"serde_repr",
|
||||||
"strum_macros",
|
"strum_macros",
|
||||||
|
"thiserror",
|
||||||
"time",
|
"time",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
@ -840,6 +841,26 @@ dependencies = [
|
||||||
"windows-sys 0.52.0",
|
"windows-sys 0.52.0",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror"
|
||||||
|
version = "1.0.56"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "d54378c645627613241d077a3a79db965db602882668f9136ac42af9ecb730ad"
|
||||||
|
dependencies = [
|
||||||
|
"thiserror-impl",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "thiserror-impl"
|
||||||
|
version = "1.0.56"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "fa0faa943b50f3db30a20aa7e265dbc66076993efed8463e8de414e5d06d3471"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "time"
|
name = "time"
|
||||||
version = "0.3.32"
|
version = "0.3.32"
|
||||||
|
|
|
@ -12,3 +12,4 @@ strum_macros = { version = "0.26.1"}
|
||||||
time = { version = "0.3.32", features = ["serde", "serde-well-known"]}
|
time = { version = "0.3.32", features = ["serde", "serde-well-known"]}
|
||||||
serde_json = "1.0.113"
|
serde_json = "1.0.113"
|
||||||
serde_repr = "0.1.18"
|
serde_repr = "0.1.18"
|
||||||
|
thiserror = "1.0.56"
|
||||||
|
|
|
@ -1,18 +1,22 @@
|
||||||
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
use serde::{de::DeserializeOwned, Deserialize, Serialize};
|
||||||
use serde_repr::{Deserialize_repr, Serialize_repr};
|
use serde_repr::{Deserialize_repr, Serialize_repr};
|
||||||
use std::net::IpAddr;
|
use std::net::IpAddr;
|
||||||
|
use thiserror::Error;
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug, Error)]
|
||||||
pub struct ValueError(String);
|
pub enum Error {
|
||||||
|
#[error("unsupported API version {0}")]
|
||||||
impl std::fmt::Display for ValueError {
|
UnsupportedApiVersion(u64),
|
||||||
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
|
#[error("invalid endpoint {0:?}")]
|
||||||
write!(f, "Invalid value given! Reason: {}", self.0)
|
InvalidEndpoint(String),
|
||||||
}
|
#[error("request failed")]
|
||||||
|
Request(#[from] reqwest::Error),
|
||||||
|
#[error("decoding response body failed")]
|
||||||
|
Decode(#[from] serde_json::Error),
|
||||||
|
#[error("received error response {:?}: {}", .0.code, .0.reason)]
|
||||||
|
Response(Status),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl std::error::Error for ValueError {}
|
|
||||||
|
|
||||||
#[derive(Debug, Serialize, Deserialize)]
|
#[derive(Debug, Serialize, Deserialize)]
|
||||||
#[serde(rename_all = "PascalCase")]
|
#[serde(rename_all = "PascalCase")]
|
||||||
pub struct FroniousResponse<T> {
|
pub struct FroniousResponse<T> {
|
||||||
|
@ -94,14 +98,18 @@ pub fn get_api_version(ip: IpAddr) -> Result<ApiVersion, Box<dyn std::error::Err
|
||||||
|
|
||||||
pub struct DeviceId(u8);
|
pub struct DeviceId(u8);
|
||||||
|
|
||||||
impl TryFrom<u8> for DeviceId {
|
#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)]
|
||||||
type Error = ValueError;
|
#[error("invalid device ID, must be less than 100: {0}")]
|
||||||
|
pub struct InvalidDeviceId(u8);
|
||||||
|
|
||||||
fn try_from(device_id: u8) -> Result<Self, ValueError> {
|
impl TryFrom<u8> for DeviceId {
|
||||||
|
type Error = InvalidDeviceId;
|
||||||
|
|
||||||
|
fn try_from(device_id: u8) -> Result<Self, InvalidDeviceId> {
|
||||||
if device_id <= 99 {
|
if device_id <= 99 {
|
||||||
Ok(Self(device_id))
|
Ok(Self(device_id))
|
||||||
} else {
|
} else {
|
||||||
Err(ValueError("device id not in range!".into()))
|
Err(InvalidDeviceId(device_id))
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue