-
-
Notifications
You must be signed in to change notification settings - Fork 13.7k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
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.
- Loading branch information
1 parent
5582a69
commit eca136a
Showing
5 changed files
with
53 additions
and
46 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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}"; | ||
|
||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters