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