TryFrom on enums should use anyhow::Error as the error type

This commit is contained in:
Vicki Pfau 2024-08-13 22:30:23 -07:00
parent 3cd834b385
commit d465bc2750
4 changed files with 16 additions and 16 deletions

View file

@ -6,7 +6,7 @@
* SPDX-License-Identifier: MIT
*/
use anyhow::{bail, Error, Result};
use anyhow::{anyhow, bail, Error, Result};
use std::fmt;
use std::str::FromStr;
use zbus::Connection;
@ -21,13 +21,13 @@ pub enum HdmiCecState {
}
impl TryFrom<u32> for HdmiCecState {
type Error = String;
type Error = Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == HdmiCecState::Disabled as u32 => Ok(HdmiCecState::Disabled),
x if x == HdmiCecState::ControlOnly as u32 => Ok(HdmiCecState::ControlOnly),
x if x == HdmiCecState::ControlAndWake as u32 => Ok(HdmiCecState::ControlAndWake),
_ => Err(format!("No enum match for value {v}")),
_ => Err(anyhow!("No enum match for value {v}")),
}
}
}

View file

@ -5,7 +5,7 @@
* SPDX-License-Identifier: MIT
*/
use anyhow::{bail, Error, Result};
use anyhow::{anyhow, bail, Error, Result};
use std::fmt;
use std::str::FromStr;
use tokio::fs;
@ -52,7 +52,7 @@ impl FromStr for HardwareVariant {
}
impl TryFrom<u32> for HardwareCurrentlySupported {
type Error = String;
type Error = anyhow::Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == HardwareCurrentlySupported::Unknown as u32 => {
@ -64,7 +64,7 @@ impl TryFrom<u32> for HardwareCurrentlySupported {
x if x == HardwareCurrentlySupported::Supported as u32 => {
Ok(HardwareCurrentlySupported::Supported)
}
_ => Err(format!("No enum match for value {v}")),
_ => Err(anyhow!("No enum match for value {v}")),
}
}
}
@ -80,12 +80,12 @@ impl fmt::Display for HardwareCurrentlySupported {
}
impl TryFrom<u32> for FanControlState {
type Error = String;
type Error = Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == FanControlState::Bios as u32 => Ok(FanControlState::Bios),
x if x == FanControlState::Os as u32 => Ok(FanControlState::Os),
_ => Err(format!("No enum match for value {v}")),
_ => Err(anyhow!("No enum match for value {v}")),
}
}
}

View file

@ -34,7 +34,7 @@ macro_rules! enum_roundtrip {
};
($enum:ident => $value:literal : $ty:ty = $variant:ident) => {
assert_eq!($enum::$variant as $ty, $value);
assert_eq!($enum::try_from($value), Ok($enum::$variant));
assert_eq!($enum::try_from($value).unwrap(), $enum::$variant);
};
($enum:ident { $($value:literal : $ty:ident = $variant:ident,)+ }) => {

View file

@ -5,7 +5,7 @@
* SPDX-License-Identifier: MIT
*/
use anyhow::{bail, ensure, Result};
use anyhow::{anyhow, bail, ensure, Error, Result};
use std::str::FromStr;
use strum::{Display, EnumString};
use tokio::fs;
@ -75,34 +75,34 @@ pub enum WifiBackend {
}
impl TryFrom<u32> for WifiDebugMode {
type Error = String;
type Error = Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == WifiDebugMode::Off as u32 => Ok(WifiDebugMode::Off),
x if x == WifiDebugMode::Tracing as u32 => Ok(WifiDebugMode::Tracing),
_ => Err(format!("No enum match for value {v}")),
_ => Err(anyhow!("No enum match for value {v}")),
}
}
}
impl TryFrom<u32> for WifiPowerManagement {
type Error = String;
type Error = Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == WifiPowerManagement::Disabled as u32 => Ok(WifiPowerManagement::Disabled),
x if x == WifiPowerManagement::Enabled as u32 => Ok(WifiPowerManagement::Enabled),
_ => Err(format!("No enum match for value {v}")),
_ => Err(anyhow!("No enum match for value {v}")),
}
}
}
impl TryFrom<u32> for WifiBackend {
type Error = String;
type Error = Error;
fn try_from(v: u32) -> Result<Self, Self::Error> {
match v {
x if x == WifiBackend::Iwd as u32 => Ok(WifiBackend::Iwd),
x if x == WifiBackend::WPASupplicant as u32 => Ok(WifiBackend::WPASupplicant),
_ => Err(format!("No enum match for WifiBackend value {v}")),
_ => Err(anyhow!("No enum match for WifiBackend value {v}")),
}
}
}