Skip to content

Commit

Permalink
Revert "Refactor: IntoIterator+struct no longer necessary after the r…
Browse files Browse the repository at this point in the history
…efactor"

This reverts commit d066835.
  • Loading branch information
srid committed Sep 14, 2023
1 parent d066835 commit 89252f7
Showing 1 changed file with 29 additions and 13 deletions.
42 changes: 29 additions & 13 deletions crates/nix_health/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ pub mod report;
pub mod traits;

use nix_rs::{env, info};
use serde::{Deserialize, Serialize};

use self::check::{
caches::Caches, flake_enabled::FlakeEnabled, max_jobs::MaxJobs, min_nix_version::MinNixVersion,
Expand All @@ -14,27 +15,42 @@ use self::check::{
use self::traits::*;

/// Nix Health check information for user's install
pub struct NixHealth(Vec<Box<dyn Checkable>>);
///
/// Each field represents an individual check which satisfies the [Check] trait.
///
/// NOTE: This struct is isomorphic to [Vec<Box<&dyn Check>>]. We cannot use the
/// latter due to (wasm) serialization limitation with dyn trait objects. An
// [IntoIterator] impl is provide towards this end.
#[derive(Debug, Default, Serialize, Deserialize, Clone)]
pub struct NixHealth {
pub max_jobs: MaxJobs,
pub caches: Caches,
pub flake_enabled: FlakeEnabled,
pub min_nix_version: MinNixVersion,
pub trusted_users: TrustedUsers,
}

impl<'a> IntoIterator for &'a NixHealth {
type Item = &'a dyn Checkable;
type IntoIter = std::vec::IntoIter<Self::Item>;

impl Default for NixHealth {
fn default() -> Self {
let checks: Vec<Box<dyn Checkable>> = vec![
// NOTE: UI will use this exact order.
Box::<MinNixVersion>::default(),
Box::<FlakeEnabled>::default(),
Box::<MaxJobs>::default(),
Box::<Caches>::default(),
Box::<TrustedUsers>::default(),
/// Return an iterator to iterate on the fields of [NixHealth]
fn into_iter(self) -> Self::IntoIter {
let items: Vec<Self::Item> = vec![
&self.min_nix_version,
&self.flake_enabled,
&self.max_jobs,
&self.caches,
&self.trusted_users,
];
Self(checks)
items.into_iter()
}
}

impl NixHealth {
/// Run all checks and collect the results
pub fn run_checks(&self, nix_info: &info::NixInfo, nix_env: &env::NixEnv) -> Vec<Check> {
self.0
.iter()
self.into_iter()
.flat_map(|c| c.check(nix_info, nix_env))
.collect()
}
Expand Down

0 comments on commit 89252f7

Please sign in to comment.