lib/trivial: add assertOneOf
This commit is contained in:
parent
0e2aa97f3a
commit
320cdecd16
|
@ -60,7 +60,7 @@ let
|
||||||
boolToString mergeAttrs flip mapNullable inNixShell min max
|
boolToString mergeAttrs flip mapNullable inNixShell min max
|
||||||
importJSON warn info nixpkgsVersion version mod compare
|
importJSON warn info nixpkgsVersion version mod compare
|
||||||
splitByAndCompare functionArgs setFunctionArgs isFunction
|
splitByAndCompare functionArgs setFunctionArgs isFunction
|
||||||
assertMsg;
|
assertMsg assertOneOf;
|
||||||
|
|
||||||
inherit (fixedPoints) fix fix' extends composeExtensions
|
inherit (fixedPoints) fix fix' extends composeExtensions
|
||||||
makeExtensible makeExtensibleWithCustomName;
|
makeExtensible makeExtensibleWithCustomName;
|
||||||
|
|
|
@ -203,11 +203,29 @@ rec {
|
||||||
Type:
|
Type:
|
||||||
assertMsg :: Bool -> String -> Bool
|
assertMsg :: Bool -> String -> Bool
|
||||||
*/
|
*/
|
||||||
|
# TODO(Profpatsch): add tests that check stderr
|
||||||
assertMsg = pred: msg:
|
assertMsg = pred: msg:
|
||||||
if pred
|
if pred
|
||||||
then true
|
then true
|
||||||
else builtins.trace msg false;
|
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
|
## Function annotations
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue