lib: move assertMsg and assertOneOf to their own library file
Since the `assertOneOf` uses `lib.generators`, they are not really trivial anymore and should go into their own library file.
This commit is contained in:
parent
3e45b61a99
commit
efdf618330
|
@ -0,0 +1,44 @@
|
||||||
|
{ lib }:
|
||||||
|
|
||||||
|
rec {
|
||||||
|
|
||||||
|
/* Print a trace message if pred is false.
|
||||||
|
Intended to be used to augment asserts with helpful error messages.
|
||||||
|
|
||||||
|
Example:
|
||||||
|
assertMsg false "nope"
|
||||||
|
=> false
|
||||||
|
stderr> trace: nope
|
||||||
|
|
||||||
|
assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); ""
|
||||||
|
stderr> trace: foo is not bar, silly
|
||||||
|
stderr> assert failed at …
|
||||||
|
|
||||||
|
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}";
|
||||||
|
|
||||||
|
}
|
|
@ -38,10 +38,11 @@ let
|
||||||
systems = callLibs ./systems;
|
systems = callLibs ./systems;
|
||||||
|
|
||||||
# misc
|
# misc
|
||||||
|
asserts = callLibs ./asserts.nix;
|
||||||
debug = callLibs ./debug.nix;
|
debug = callLibs ./debug.nix;
|
||||||
|
|
||||||
generators = callLibs ./generators.nix;
|
generators = callLibs ./generators.nix;
|
||||||
misc = callLibs ./deprecated.nix;
|
misc = callLibs ./deprecated.nix;
|
||||||
|
|
||||||
# domain-specific
|
# domain-specific
|
||||||
fetchers = callLibs ./fetchers.nix;
|
fetchers = callLibs ./fetchers.nix;
|
||||||
|
|
||||||
|
@ -59,9 +60,7 @@ let
|
||||||
inherit (trivial) id const concat or and bitAnd bitOr bitXor bitNot
|
inherit (trivial) id const concat or and bitAnd bitOr bitXor bitNot
|
||||||
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 assertOneOf;
|
|
||||||
|
|
||||||
inherit (fixedPoints) fix fix' extends composeExtensions
|
inherit (fixedPoints) fix fix' extends composeExtensions
|
||||||
makeExtensible makeExtensibleWithCustomName;
|
makeExtensible makeExtensibleWithCustomName;
|
||||||
inherit (attrsets) attrByPath hasAttrByPath setAttrByPath
|
inherit (attrsets) attrByPath hasAttrByPath setAttrByPath
|
||||||
|
@ -118,6 +117,8 @@ let
|
||||||
unknownModule mkOption;
|
unknownModule mkOption;
|
||||||
inherit (types) isType setType defaultTypeMerge defaultFunctor
|
inherit (types) isType setType defaultTypeMerge defaultFunctor
|
||||||
isOptionType mkOptionType;
|
isOptionType mkOptionType;
|
||||||
|
inherit (asserts)
|
||||||
|
assertMsg assertOneOf;
|
||||||
inherit (debug) addErrorContextToAttrs traceIf traceVal traceValFn
|
inherit (debug) addErrorContextToAttrs traceIf traceVal traceValFn
|
||||||
traceXMLVal traceXMLValMarked traceSeq traceSeqN traceValSeq
|
traceXMLVal traceXMLValMarked traceSeq traceSeqN traceValSeq
|
||||||
traceValSeqFn traceValSeqN traceValSeqNFn traceShowVal
|
traceValSeqFn traceValSeqN traceValSeqNFn traceShowVal
|
||||||
|
|
|
@ -509,7 +509,7 @@ rec {
|
||||||
=> 3
|
=> 3
|
||||||
*/
|
*/
|
||||||
last = list:
|
last = list:
|
||||||
assert assertMsg (list != []) "lists.last: list must not be empty!";
|
assert lib.assertMsg (list != []) "lists.last: list must not be empty!";
|
||||||
elemAt list (length list - 1);
|
elemAt list (length list - 1);
|
||||||
|
|
||||||
/* Return all elements but the last
|
/* Return all elements but the last
|
||||||
|
@ -519,7 +519,7 @@ rec {
|
||||||
=> [ 1 2 ]
|
=> [ 1 2 ]
|
||||||
*/
|
*/
|
||||||
init = list:
|
init = list:
|
||||||
assert assertMsg (list != []) "lists.init: list must not be empty!";
|
assert lib.assertMsg (list != []) "lists.init: list must not be empty!";
|
||||||
take (length list - 1) list;
|
take (length list - 1) list;
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -171,7 +171,7 @@ rec {
|
||||||
builtins.fromJSON (builtins.readFile path);
|
builtins.fromJSON (builtins.readFile path);
|
||||||
|
|
||||||
|
|
||||||
## Warnings and asserts
|
## Warnings
|
||||||
|
|
||||||
/* See https://github.com/NixOS/nix/issues/749. Eventually we'd like these
|
/* See https://github.com/NixOS/nix/issues/749. Eventually we'd like these
|
||||||
to expand to Nix builtins that carry metadata so that Nix can filter out
|
to expand to Nix builtins that carry metadata so that Nix can filter out
|
||||||
|
@ -188,44 +188,6 @@ rec {
|
||||||
warn = msg: builtins.trace "WARNING: ${msg}";
|
warn = msg: builtins.trace "WARNING: ${msg}";
|
||||||
info = msg: builtins.trace "INFO: ${msg}";
|
info = msg: builtins.trace "INFO: ${msg}";
|
||||||
|
|
||||||
/* Print a trace message if pred is false.
|
|
||||||
Intended to be used to augment asserts with helpful error messages.
|
|
||||||
|
|
||||||
Example:
|
|
||||||
assertMsg false "nope"
|
|
||||||
=> false
|
|
||||||
stderr> trace: nope
|
|
||||||
|
|
||||||
assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); ""
|
|
||||||
stderr> trace: foo is not bar, silly
|
|
||||||
stderr> assert failed at …
|
|
||||||
|
|
||||||
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
|
## Function annotations
|
||||||
|
|
||||||
|
|
|
@ -441,7 +441,7 @@ rec {
|
||||||
# Either value of type `finalType` or `coercedType`, the latter is
|
# Either value of type `finalType` or `coercedType`, the latter is
|
||||||
# converted to `finalType` using `coerceFunc`.
|
# converted to `finalType` using `coerceFunc`.
|
||||||
coercedTo = coercedType: coerceFunc: finalType:
|
coercedTo = coercedType: coerceFunc: finalType:
|
||||||
assert assertMsg (coercedType.getSubModules == null)
|
assert lib.assertMsg (coercedType.getSubModules == null)
|
||||||
"coercedTo: coercedType must not have submodules (it’s a ${
|
"coercedTo: coercedType must not have submodules (it’s a ${
|
||||||
coercedType.description})";
|
coercedType.description})";
|
||||||
mkOptionType rec {
|
mkOptionType rec {
|
||||||
|
|
Loading…
Reference in New Issue