mirror of
https://gitlab.steamos.cloud/holo/steamos-manager.git
synced 2025-07-07 15:10:29 -04:00
Use num_enum instead of home-rolled TryFrom definitions
This commit is contained in:
parent
111498513b
commit
b71ccfcc92
6 changed files with 40 additions and 101 deletions
22
Cargo.lock
generated
22
Cargo.lock
generated
|
@ -725,6 +725,27 @@ dependencies = [
|
||||||
"minimal-lexical",
|
"minimal-lexical",
|
||||||
]
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_enum"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "4e613fc340b2220f734a8595782c551f1250e969d87d3be1ae0579e8d4065179"
|
||||||
|
dependencies = [
|
||||||
|
"num_enum_derive",
|
||||||
|
]
|
||||||
|
|
||||||
|
[[package]]
|
||||||
|
name = "num_enum_derive"
|
||||||
|
version = "0.7.3"
|
||||||
|
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||||
|
checksum = "af1844ef2428cc3e1cb900be36181049ef3d3193c63e43026cfe202983b27a56"
|
||||||
|
dependencies = [
|
||||||
|
"proc-macro-crate",
|
||||||
|
"proc-macro2",
|
||||||
|
"quote",
|
||||||
|
"syn",
|
||||||
|
]
|
||||||
|
|
||||||
[[package]]
|
[[package]]
|
||||||
name = "object"
|
name = "object"
|
||||||
version = "0.36.2"
|
version = "0.36.2"
|
||||||
|
@ -1079,6 +1100,7 @@ dependencies = [
|
||||||
"lazy_static",
|
"lazy_static",
|
||||||
"libc",
|
"libc",
|
||||||
"nix",
|
"nix",
|
||||||
|
"num_enum",
|
||||||
"regex",
|
"regex",
|
||||||
"serde",
|
"serde",
|
||||||
"strum",
|
"strum",
|
||||||
|
|
|
@ -19,6 +19,7 @@ lazy_static = "1"
|
||||||
libc = "0.2"
|
libc = "0.2"
|
||||||
itertools = "0.13"
|
itertools = "0.13"
|
||||||
nix = { version = "0.29", default-features = false, features = ["fs", "poll", "signal"] }
|
nix = { version = "0.29", default-features = false, features = ["fs", "poll", "signal"] }
|
||||||
|
num_enum = "0.7"
|
||||||
regex = "1"
|
regex = "1"
|
||||||
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
serde = { version = "1.0", default-features = false, features = ["derive"] }
|
||||||
tokio = { version = "1", default-features = false, features = ["fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync"] }
|
tokio = { version = "1", default-features = false, features = ["fs", "io-std", "io-util", "macros", "process", "rt-multi-thread", "signal", "sync"] }
|
||||||
|
|
18
src/cec.rs
18
src/cec.rs
|
@ -6,32 +6,22 @@
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, Error, Result};
|
use anyhow::{bail, Error, Result};
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use zbus::Connection;
|
use zbus::Connection;
|
||||||
|
|
||||||
use crate::systemd::{daemon_reload, EnableState, SystemdUnit};
|
use crate::systemd::{daemon_reload, EnableState, SystemdUnit};
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Copy, Clone)]
|
#[derive(PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
|
#[repr(u32)]
|
||||||
pub enum HdmiCecState {
|
pub enum HdmiCecState {
|
||||||
Disabled = 0,
|
Disabled = 0,
|
||||||
ControlOnly = 1,
|
ControlOnly = 1,
|
||||||
ControlAndWake = 2,
|
ControlAndWake = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for HdmiCecState {
|
|
||||||
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(anyhow!("No enum match for value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for HdmiCecState {
|
impl FromStr for HdmiCecState {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
fn from_str(input: &str) -> Result<HdmiCecState, Self::Err> {
|
fn from_str(input: &str) -> Result<HdmiCecState, Self::Err> {
|
||||||
|
|
|
@ -5,7 +5,8 @@
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, ensure, Error, Result};
|
use anyhow::{bail, ensure, Error, Result};
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
use std::fmt;
|
use std::fmt;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
@ -27,7 +28,7 @@ pub(crate) enum HardwareVariant {
|
||||||
Galileo,
|
Galileo,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Copy, Clone)]
|
#[derive(PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub(crate) enum HardwareCurrentlySupported {
|
pub(crate) enum HardwareCurrentlySupported {
|
||||||
Unknown = 0,
|
Unknown = 0,
|
||||||
|
@ -35,7 +36,7 @@ pub(crate) enum HardwareCurrentlySupported {
|
||||||
Supported = 2,
|
Supported = 2,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(PartialEq, Debug, Copy, Clone)]
|
#[derive(PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum FanControlState {
|
pub enum FanControlState {
|
||||||
Bios = 0,
|
Bios = 0,
|
||||||
|
@ -53,24 +54,6 @@ impl FromStr for HardwareVariant {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for HardwareCurrentlySupported {
|
|
||||||
type Error = anyhow::Error;
|
|
||||||
fn try_from(v: u32) -> Result<Self, Self::Error> {
|
|
||||||
match v {
|
|
||||||
x if x == HardwareCurrentlySupported::Unknown as u32 => {
|
|
||||||
Ok(HardwareCurrentlySupported::Unknown)
|
|
||||||
}
|
|
||||||
x if x == HardwareCurrentlySupported::UnsupportedPrototype as u32 => {
|
|
||||||
Ok(HardwareCurrentlySupported::UnsupportedPrototype)
|
|
||||||
}
|
|
||||||
x if x == HardwareCurrentlySupported::Supported as u32 => {
|
|
||||||
Ok(HardwareCurrentlySupported::Supported)
|
|
||||||
}
|
|
||||||
_ => Err(anyhow!("No enum match for value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl fmt::Display for HardwareCurrentlySupported {
|
impl fmt::Display for HardwareCurrentlySupported {
|
||||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||||
match self {
|
match self {
|
||||||
|
@ -81,17 +64,6 @@ impl fmt::Display for HardwareCurrentlySupported {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for FanControlState {
|
|
||||||
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(anyhow!("No enum match for value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl FromStr for FanControlState {
|
impl FromStr for FanControlState {
|
||||||
type Err = Error;
|
type Err = Error;
|
||||||
fn from_str(input: &str) -> Result<FanControlState, Self::Err> {
|
fn from_str(input: &str) -> Result<FanControlState, Self::Err> {
|
||||||
|
@ -172,7 +144,7 @@ impl FanControl {
|
||||||
}) => {
|
}) => {
|
||||||
let res = script_exit_code(&status.script, &status.script_args).await?;
|
let res = script_exit_code(&status.script, &status.script_args).await?;
|
||||||
ensure!(res >= 0, "Script exited abnormally");
|
ensure!(res >= 0, "Script exited abnormally");
|
||||||
FanControlState::try_from(res as u32)
|
Ok(FanControlState::try_from(res as u32)?)
|
||||||
}
|
}
|
||||||
None => bail!("Fan control not configured"),
|
None => bail!("Fan control not configured"),
|
||||||
}
|
}
|
||||||
|
|
20
src/power.rs
20
src/power.rs
|
@ -7,6 +7,7 @@
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, ensure, Result};
|
use anyhow::{anyhow, bail, ensure, Result};
|
||||||
use lazy_static::lazy_static;
|
use lazy_static::lazy_static;
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
use regex::Regex;
|
use regex::Regex;
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::{Path, PathBuf};
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
|
@ -43,8 +44,9 @@ lazy_static! {
|
||||||
Regex::new(r"^\s*(?<index>[0-9]+): (?<value>[0-9]+)Mhz").unwrap();
|
Regex::new(r"^\s*(?<index>[0-9]+): (?<value>[0-9]+)Mhz").unwrap();
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[strum(serialize_all = "snake_case")]
|
#[strum(serialize_all = "snake_case")]
|
||||||
|
#[repr(u32)]
|
||||||
pub enum GPUPowerProfile {
|
pub enum GPUPowerProfile {
|
||||||
// Currently firmware exposes these values, though
|
// Currently firmware exposes these values, though
|
||||||
// deck doesn't support them yet
|
// deck doesn't support them yet
|
||||||
|
@ -60,22 +62,6 @@ pub enum GPUPowerProfile {
|
||||||
Uncapped = 9,
|
Uncapped = 9,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for GPUPowerProfile {
|
|
||||||
type Error = &'static str;
|
|
||||||
fn try_from(v: u32) -> Result<Self, Self::Error> {
|
|
||||||
match v {
|
|
||||||
x if x == GPUPowerProfile::FullScreen as u32 => Ok(GPUPowerProfile::FullScreen),
|
|
||||||
x if x == GPUPowerProfile::Video as u32 => Ok(GPUPowerProfile::Video),
|
|
||||||
x if x == GPUPowerProfile::VR as u32 => Ok(GPUPowerProfile::VR),
|
|
||||||
x if x == GPUPowerProfile::Compute as u32 => Ok(GPUPowerProfile::Compute),
|
|
||||||
x if x == GPUPowerProfile::Custom as u32 => Ok(GPUPowerProfile::Custom),
|
|
||||||
x if x == GPUPowerProfile::Capped as u32 => Ok(GPUPowerProfile::Capped),
|
|
||||||
x if x == GPUPowerProfile::Uncapped as u32 => Ok(GPUPowerProfile::Uncapped),
|
|
||||||
_ => Err("No GPUPowerProfile for value"),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
||||||
#[strum(serialize_all = "snake_case")]
|
#[strum(serialize_all = "snake_case")]
|
||||||
pub enum GPUPerformanceLevel {
|
pub enum GPUPerformanceLevel {
|
||||||
|
|
42
src/wifi.rs
42
src/wifi.rs
|
@ -5,9 +5,10 @@
|
||||||
* SPDX-License-Identifier: MIT
|
* SPDX-License-Identifier: MIT
|
||||||
*/
|
*/
|
||||||
|
|
||||||
use anyhow::{anyhow, bail, ensure, Error, Result};
|
use anyhow::{bail, ensure, Result};
|
||||||
use config::builder::AsyncState;
|
use config::builder::AsyncState;
|
||||||
use config::{ConfigBuilder, FileFormat};
|
use config::{ConfigBuilder, FileFormat};
|
||||||
|
use num_enum::TryFromPrimitive;
|
||||||
use std::str::FromStr;
|
use std::str::FromStr;
|
||||||
use strum::{Display, EnumString};
|
use strum::{Display, EnumString};
|
||||||
use tokio::fs;
|
use tokio::fs;
|
||||||
|
@ -37,7 +38,7 @@ const WIFI_BACKEND_PATHS: &[&str] = &[
|
||||||
"/etc/NetworkManager/conf.d",
|
"/etc/NetworkManager/conf.d",
|
||||||
];
|
];
|
||||||
|
|
||||||
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum WifiDebugMode {
|
pub enum WifiDebugMode {
|
||||||
|
@ -51,7 +52,7 @@ pub enum WifiDebugMode {
|
||||||
Tracing = 1,
|
Tracing = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[strum(ascii_case_insensitive)]
|
#[strum(ascii_case_insensitive)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum WifiPowerManagement {
|
pub enum WifiPowerManagement {
|
||||||
|
@ -71,7 +72,7 @@ pub enum WifiPowerManagement {
|
||||||
Enabled = 1,
|
Enabled = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone)]
|
#[derive(Display, EnumString, PartialEq, Debug, Copy, Clone, TryFromPrimitive)]
|
||||||
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
#[strum(serialize_all = "snake_case", ascii_case_insensitive)]
|
||||||
#[repr(u32)]
|
#[repr(u32)]
|
||||||
pub enum WifiBackend {
|
pub enum WifiBackend {
|
||||||
|
@ -79,39 +80,6 @@ pub enum WifiBackend {
|
||||||
WPASupplicant = 1,
|
WPASupplicant = 1,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl TryFrom<u32> for WifiDebugMode {
|
|
||||||
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(anyhow!("No enum match for value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<u32> for WifiPowerManagement {
|
|
||||||
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(anyhow!("No enum match for value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl TryFrom<u32> for WifiBackend {
|
|
||||||
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(anyhow!("No enum match for WifiBackend value {v}")),
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
pub(crate) async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> {
|
pub(crate) async fn setup_iwd_config(want_override: bool) -> std::io::Result<()> {
|
||||||
// Copy override.conf file into place or out of place depending
|
// Copy override.conf file into place or out of place depending
|
||||||
// on install value
|
// on install value
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue