From 7d42a56199ffe05a5d567f4f705264ffbe7d0795 Mon Sep 17 00:00:00 2001 From: Xiretza Date: Fri, 2 Feb 2024 15:26:44 +0000 Subject: [PATCH] Add error enum --- Cargo.lock | 21 +++++++++++++++++++++ Cargo.toml | 1 + src/fronious.rs | 34 +++++++++++++++++++++------------- 3 files changed, 43 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 8675251..6c5fb01 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -179,6 +179,7 @@ dependencies = [ "serde_json", "serde_repr", "strum_macros", + "thiserror", "time", ] @@ -840,6 +841,26 @@ dependencies = [ "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]] name = "time" version = "0.3.32" diff --git a/Cargo.toml b/Cargo.toml index de655b8..56aa8cd 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -12,3 +12,4 @@ strum_macros = { version = "0.26.1"} time = { version = "0.3.32", features = ["serde", "serde-well-known"]} serde_json = "1.0.113" serde_repr = "0.1.18" +thiserror = "1.0.56" diff --git a/src/fronious.rs b/src/fronious.rs index 7b3274a..4451b03 100644 --- a/src/fronious.rs +++ b/src/fronious.rs @@ -1,18 +1,22 @@ use serde::{de::DeserializeOwned, Deserialize, Serialize}; use serde_repr::{Deserialize_repr, Serialize_repr}; use std::net::IpAddr; +use thiserror::Error; -#[derive(Debug)] -pub struct ValueError(String); - -impl std::fmt::Display for ValueError { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "Invalid value given! Reason: {}", self.0) - } +#[derive(Debug, Error)] +pub enum Error { + #[error("unsupported API version {0}")] + UnsupportedApiVersion(u64), + #[error("invalid endpoint {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)] #[serde(rename_all = "PascalCase")] pub struct FroniousResponse { @@ -94,14 +98,18 @@ pub fn get_api_version(ip: IpAddr) -> Result for DeviceId { - type Error = ValueError; +#[derive(Debug, Clone, Copy, PartialEq, Eq, Error)] +#[error("invalid device ID, must be less than 100: {0}")] +pub struct InvalidDeviceId(u8); - fn try_from(device_id: u8) -> Result { +impl TryFrom for DeviceId { + type Error = InvalidDeviceId; + + fn try_from(device_id: u8) -> Result { if device_id <= 99 { Ok(Self(device_id)) } else { - Err(ValueError("device id not in range!".into())) + Err(InvalidDeviceId(device_id)) } } }