Skip to content

Commit

Permalink
Utilize serde_with to directly parse it into enum
Browse files Browse the repository at this point in the history
  • Loading branch information
srid committed Dec 16, 2023
1 parent 62a0d69 commit 4b6cb8b
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 18 deletions.
18 changes: 11 additions & 7 deletions crates/nix_health/src/check/trusted_users.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand All @@ -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,
})
}
35 changes: 24 additions & 11 deletions crates/nix_rs/src/config.rs
Original file line number Diff line number Diff line change
@@ -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;

Expand All @@ -18,7 +20,7 @@ pub struct NixConfig {
pub max_jobs: ConfigVal<i32>,
pub substituters: ConfigVal<Vec<Url>>,
pub system: ConfigVal<System>,
pub trusted_users: ConfigVal<Vec<String>>,
pub trusted_users: ConfigVal<Vec<TrustedUserValue>>,
}

/// The value for each 'nix show-config --json' key.
Expand All @@ -45,17 +47,9 @@ impl NixConfig {
.await?;
Ok(v)
}

pub fn get_trusted_users_vals(&self) -> Vec<TrustedUserValue> {
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,
Expand 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::<Vec<String>>()
.join(" ")
}
}

impl From<String> for TrustedUserValue {
Expand All @@ -85,6 +90,14 @@ impl From<String> for TrustedUserValue {
}
}

impl FromStr for TrustedUserValue {
type Err = Infallible;

fn from_str(s: &str) -> Result<Self, Self::Err> {
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?;
Expand Down

0 comments on commit 4b6cb8b

Please sign in to comment.