summary refs log tree commit diff
diff options
context:
space:
mode:
authorProfpatsch <mail@profpatsch.de>2018-08-06 01:35:48 +0200
committerProfpatsch <mail@profpatsch.de>2018-09-06 18:14:27 +0200
commit320cdecd1697020cb367adc1f8408dbf689ca254 (patch)
tree26bba1c8197c78582c6f51495cd1cad2e6999e78
parent0e2aa97f3aa1341693885511fd78394b828c6477 (diff)
downloadnixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar.gz
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar.bz2
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar.lz
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar.xz
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.tar.zst
nixpkgs-320cdecd1697020cb367adc1f8408dbf689ca254.zip
lib/trivial: add assertOneOf
-rw-r--r--lib/default.nix2
-rw-r--r--lib/trivial.nix18
2 files changed, 19 insertions, 1 deletions
diff --git a/lib/default.nix b/lib/default.nix
index fd3be3c6f4b..358c8ca0b8f 100644
--- a/lib/default.nix
+++ b/lib/default.nix
@@ -60,7 +60,7 @@ let
       boolToString mergeAttrs flip mapNullable inNixShell min max
       importJSON warn info nixpkgsVersion version mod compare
       splitByAndCompare functionArgs setFunctionArgs isFunction
-      assertMsg;
+      assertMsg assertOneOf;
 
     inherit (fixedPoints) fix fix' extends composeExtensions
       makeExtensible makeExtensibleWithCustomName;
diff --git a/lib/trivial.nix b/lib/trivial.nix
index bba284548d9..f1001ee10ca 100644
--- a/lib/trivial.nix
+++ b/lib/trivial.nix
@@ -203,11 +203,29 @@ rec {
      Type:
        assertMsg :: Bool -> String -> Bool
   */
+  # TODO(Profpatsch): add tests that check stderr
   assertMsg = pred: msg:
     if pred
     then true
     else builtins.trace msg false;
 
+  /* Specialized `assertMsg` for checking if val is one of the elements
+     of a list. Useful for checking enums.
+
+     Example:
+       let sslLibrary = "libressl"
+       in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
+       => false
+       stderr> trace: sslLibrary must be one of "openssl", "bearssl", but is: "libressl"
+
+     Type:
+       assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
+  */
+  assertOneOf = name: val: xs: assertMsg
+    (lib.elem val xs)
+    "${name} must be one of ${
+      lib.generators.toPretty {} xs}, but is: ${
+        lib.generators.toPretty {} val}";
 
   ## Function annotations