From 4b6cb8b1c1879800be02de1710c8ece9d382f490 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Sat, 16 Dec 2023 14:09:09 -0500 Subject: [PATCH] Utilize serde_with to directly parse it into enum --- crates/nix_health/src/check/trusted_users.rs | 18 ++++++---- crates/nix_rs/src/config.rs | 35 ++++++++++++++------ 2 files changed, 35 insertions(+), 18 deletions(-) diff --git a/crates/nix_health/src/check/trusted_users.rs b/crates/nix_health/src/check/trusted_users.rs index 9ae302a2..a798f473 100644 --- a/crates/nix_health/src/check/trusted_users.rs +++ b/crates/nix_health/src/check/trusted_users.rs @@ -37,7 +37,7 @@ impl Checkable for TrustedUsers { title: "Trusted Users".to_string(), info: format!( "trusted-users = {}", - nix_info.nix_config.trusted_users.value.join(" ") + TrustedUserValue::display_original(&nix_info.nix_config.trusted_users.value) ), result, required: true, @@ -52,10 +52,14 @@ fn is_current_user_trusted(nix_info: &nix_rs::info::NixInfo) -> bool { let current_user = &nix_info.nix_env.current_user; let current_user_groups: HashSet<&String> = nix_info.nix_env.current_user_groups.iter().collect(); - let trusted_user_values = nix_info.nix_config.get_trusted_users_vals(); - trusted_user_values.iter().any(|x| match x { - TrustedUserValue::Group(x) => current_user_groups.contains(&x), - TrustedUserValue::User(x) => x == current_user, - TrustedUserValue::All => true, - }) + nix_info + .nix_config + .trusted_users + .value + .iter() + .any(|x| match x { + TrustedUserValue::Group(x) => current_user_groups.contains(&x), + TrustedUserValue::User(x) => x == current_user, + TrustedUserValue::All => true, + }) } diff --git a/crates/nix_rs/src/config.rs b/crates/nix_rs/src/config.rs index 7dcf6153..60cdc234 100644 --- a/crates/nix_rs/src/config.rs +++ b/crates/nix_rs/src/config.rs @@ -1,7 +1,9 @@ //! Rust module for `nix show-config` -use serde::{Deserialize, Serialize}; +use std::{convert::Infallible, str::FromStr}; +use serde::{Deserialize, Serialize}; +use serde_with::DeserializeFromStr; use tracing::instrument; use url::Url; @@ -18,7 +20,7 @@ pub struct NixConfig { pub max_jobs: ConfigVal, pub substituters: ConfigVal>, pub system: ConfigVal, - pub trusted_users: ConfigVal>, + pub trusted_users: ConfigVal>, } /// The value for each 'nix show-config --json' key. @@ -45,17 +47,9 @@ impl NixConfig { .await?; Ok(v) } - - pub fn get_trusted_users_vals(&self) -> Vec { - self.trusted_users - .value - .iter() - .map(|s| TrustedUserValue::from_str(s)) - .collect() - } } -#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)] +#[derive(Debug, Clone, PartialEq, Eq, Serialize, DeserializeFromStr)] pub enum TrustedUserValue { /// All users are trusted All, @@ -77,6 +71,17 @@ impl TrustedUserValue { None => Self::User(s.to_string()), } } + + pub fn display_original(val: &[TrustedUserValue]) -> String { + val.iter() + .map(|x| match x { + TrustedUserValue::All => "*".to_string(), + TrustedUserValue::User(x) => x.to_string(), + TrustedUserValue::Group(x) => format!("@{}", x), + }) + .collect::>() + .join(" ") + } } impl From for TrustedUserValue { @@ -85,6 +90,14 @@ impl From for TrustedUserValue { } } +impl FromStr for TrustedUserValue { + type Err = Infallible; + + fn from_str(s: &str) -> Result { + Ok(Self::from_str(s)) + } +} + #[tokio::test] async fn test_nix_config() -> Result<(), crate::command::NixCmdError> { let v = NixConfig::from_nix(&crate::command::NixCmd::default()).await?;