diff options
author | José Romildo <malaquias@gmail.com> | 2022-01-09 19:12:27 -0300 |
---|---|---|
committer | José Romildo <malaquias@gmail.com> | 2022-01-09 19:21:08 -0300 |
commit | 4fc67da841d1c80b8d774a7b8c8e6baf13f9496e (patch) | |
tree | 546f91d9662ba677506a63ba3482a89b63170d33 /lib/trivial.nix | |
parent | 2ac65dd85b7fbe81b88e3c2a80d351aba4c4a9d8 (diff) |
lib.checkListOfEnum: init
Diffstat (limited to 'lib/trivial.nix')
-rw-r--r-- | lib/trivial.nix | 17 |
1 files changed, 17 insertions, 0 deletions
diff --git a/lib/trivial.nix b/lib/trivial.nix index c961d3aa73019..575aaf6a7ad4b 100644 --- a/lib/trivial.nix +++ b/lib/trivial.nix @@ -347,6 +347,23 @@ rec { */ throwIfNot = cond: msg: if cond then x: x else throw msg; + /* Check if the elements in a list are valid values from a enum, returning the identity function, or throwing an error message otherwise. + + Example: + let colorVariants = ["bright" "dark" "black"] + in checkListOfEnum "color variants" [ "standard" "light" "dark" ] colorVariants; + => + error: color variants: bright, black unexpected; valid ones: standard, light, dark + + Type: String -> List ComparableVal -> List ComparableVal -> a -> a + */ + checkListOfEnum = msg: valid: given: + let + unexpected = lib.subtractLists valid given; + in + lib.throwIfNot (unexpected == []) + "${msg}: ${builtins.concatStringsSep ", " (builtins.map builtins.toString unexpected)} unexpected; valid ones: ${builtins.concatStringsSep ", " (builtins.map builtins.toString valid)}"; + info = msg: builtins.trace "INFO: ${msg}"; showWarnings = warnings: res: lib.foldr (w: x: warn w x) res warnings; |