From 89252f7f15728975a775d8782832c4c6efca5f56 Mon Sep 17 00:00:00 2001 From: Sridhar Ratnakumar Date: Thu, 14 Sep 2023 14:39:23 -0400 Subject: [PATCH] Revert "Refactor: IntoIterator+struct no longer necessary after the refactor" This reverts commit d066835a5ef0b54aad87758dc5c58ff5f1c7fc4a. --- crates/nix_health/src/lib.rs | 42 +++++++++++++++++++++++++----------- 1 file changed, 29 insertions(+), 13 deletions(-) diff --git a/crates/nix_health/src/lib.rs b/crates/nix_health/src/lib.rs index 9ee7dcb5..f0e5540a 100644 --- a/crates/nix_health/src/lib.rs +++ b/crates/nix_health/src/lib.rs @@ -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, @@ -14,27 +15,42 @@ use self::check::{ use self::traits::*; /// Nix Health check information for user's install -pub struct NixHealth(Vec>); +/// +/// Each field represents an individual check which satisfies the [Check] trait. +/// +/// NOTE: This struct is isomorphic to [Vec>]. 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; -impl Default for NixHealth { - fn default() -> Self { - let checks: Vec> = vec![ - // NOTE: UI will use this exact order. - Box::::default(), - Box::::default(), - Box::::default(), - Box::::default(), - Box::::default(), + /// Return an iterator to iterate on the fields of [NixHealth] + fn into_iter(self) -> Self::IntoIter { + let items: Vec = 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 { - self.0 - .iter() + self.into_iter() .flat_map(|c| c.check(nix_info, nix_env)) .collect() }