From e98d22851b67a6125683f80735e4bc1042252aef Mon Sep 17 00:00:00 2001 From: Silvan Mosberger Date: Thu, 14 Dec 2023 03:03:04 +0100 Subject: [PATCH] tests.nixpkgs-check-by-name: Introduce result_map Convenience function to run another validation over a successful validation result. This will be usable in more locations in future commits, making the code nicer. --- pkgs/test/nixpkgs-check-by-name/src/main.rs | 9 ++------- pkgs/test/nixpkgs-check-by-name/src/validation.rs | 9 +++++++++ 2 files changed, 11 insertions(+), 7 deletions(-) diff --git a/pkgs/test/nixpkgs-check-by-name/src/main.rs b/pkgs/test/nixpkgs-check-by-name/src/main.rs index 4cabf8f446f5..567da00333e6 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/main.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/main.rs @@ -86,14 +86,9 @@ pub fn check_nixpkgs( ); Success(()) } else { - match check_structure(&nixpkgs_path)? { - Failure(errors) => Failure(errors), - Success(package_names) => + check_structure(&nixpkgs_path)?.result_map(|package_names| // Only if we could successfully parse the structure, we do the evaluation checks - { - eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths)? - } - } + eval::check_values(version, &nixpkgs_path, package_names, eval_accessible_paths))? }; match check_result { diff --git a/pkgs/test/nixpkgs-check-by-name/src/validation.rs b/pkgs/test/nixpkgs-check-by-name/src/validation.rs index e72793851521..b14bfb92eb2e 100644 --- a/pkgs/test/nixpkgs-check-by-name/src/validation.rs +++ b/pkgs/test/nixpkgs-check-by-name/src/validation.rs @@ -58,6 +58,15 @@ impl Validation { Success(value) => Success(f(value)), } } + + /// Map a `Validation` to a `Result` by applying a function `A -> Result` + /// only if there is a `Success` value + pub fn result_map(self, f: impl FnOnce(A) -> Result) -> Result { + match self { + Failure(err) => Ok(Failure(err)), + Success(value) => f(value), + } + } } impl Validation<()> {