1{ lib }:
2
3rec {
4
5 /* Print a trace message if pred is false.
6 Intended to be used to augment asserts with helpful error messages.
7
8 Example:
9 assertMsg false "nope"
10 => false
11 stderr> trace: nope
12
13 assert (assertMsg ("foo" == "bar") "foo is not bar, silly"); ""
14 stderr> trace: foo is not bar, silly
15 stderr> assert failed at …
16
17 Type:
18 assertMsg :: Bool -> String -> Bool
19 */
20 # TODO(Profpatsch): add tests that check stderr
21 assertMsg = pred: msg:
22 if pred
23 then true
24 else builtins.trace msg false;
25
26 /* Specialized `assertMsg` for checking if val is one of the elements
27 of a list. Useful for checking enums.
28
29 Example:
30 let sslLibrary = "libressl"
31 in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
32 => false
33 stderr> trace: sslLibrary must be one of "openssl", "bearssl", but is: "libressl"
34
35 Type:
36 assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
37 */
38 assertOneOf = name: val: xs: assertMsg
39 (lib.elem val xs)
40 "${name} must be one of ${
41 lib.generators.toPretty {} xs}, but is: ${
42 lib.generators.toPretty {} val}";
43
44}