···
5
-
/* Throw if pred is false, else return pred.
6
-
Intended to be used to augment asserts with helpful error messages.
6
+
Throw if pred is false, else return pred.
7
+
Intended to be used to augment asserts with helpful error messages.
13
+
: Predicate that needs to succeed, otherwise `msg` is thrown
17
+
: Message to throw in case `pred` fails
22
+
assertMsg :: Bool -> String -> Bool
9
-
assertMsg false "nope"
27
+
## `lib.asserts.assertMsg` usage example
12
-
assert assertMsg ("foo" == "bar") "foo is not bar, silly"; ""
13
-
stderr> error: foo is not bar, silly
30
+
assertMsg false "nope"
32
+
assert assertMsg ("foo" == "bar") "foo is not bar, silly"; ""
33
+
stderr> error: foo is not bar, silly
16
-
assertMsg :: Bool -> String -> Bool
# TODO(Profpatsch): add tests that check stderr
20
-
# Predicate that needs to succeed, otherwise `msg` is thrown
22
-
# Message to throw in case `pred` fails
pred || builtins.throw msg;
26
-
/* Specialized `assertMsg` for checking if `val` is one of the elements
27
-
of the list `xs`. Useful for checking enums.
45
+
Specialized `assertMsg` for checking if `val` is one of the elements
46
+
of the list `xs`. Useful for checking enums.
30
-
let sslLibrary = "libressl";
31
-
in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
32
-
stderr> error: sslLibrary must be one of [
35
-
stderr> ], but is: "libressl"
38
-
assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
52
+
: The name of the variable the user entered `val` into, for inclusion in the error message
56
+
: The value of what the user provided, to be compared against the values in `xs`
60
+
: The list of valid values
65
+
assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
70
+
## `lib.asserts.assertOneOf` usage example
73
+
let sslLibrary = "libressl";
74
+
in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
75
+
stderr> error: sslLibrary must be one of [
78
+
stderr> ], but is: "libressl"
41
-
# The name of the variable the user entered `val` into, for inclusion in the error message
43
-
# The value of what the user provided, to be compared against the values in `xs`
45
-
# The list of valid values
···
lib.generators.toPretty {} xs}, but is: ${
lib.generators.toPretty {} val}";
53
-
/* Specialized `assertMsg` for checking if every one of `vals` is one of the elements
54
-
of the list `xs`. Useful for checking lists of supported attributes.
94
+
Specialized `assertMsg` for checking if every one of `vals` is one of the elements
95
+
of the list `xs`. Useful for checking lists of supported attributes.
101
+
: The name of the variable the user entered `val` into, for inclusion in the error message
105
+
: The list of values of what the user provided, to be compared against the values in `xs`
109
+
: The list of valid values
114
+
assertEachOneOf :: String -> List ComparableVal -> List ComparableVal -> Bool
57
-
let sslLibraries = [ "libressl" "bearssl" ];
58
-
in assertEachOneOf "sslLibraries" sslLibraries [ "openssl" "bearssl" ]
59
-
stderr> error: each element in sslLibraries must be one of [
62
-
stderr> ], but is: [
119
+
## `lib.asserts.assertEachOneOf` usage example
68
-
assertEachOneOf :: String -> List ComparableVal -> List ComparableVal -> Bool
122
+
let sslLibraries = [ "libressl" "bearssl" ];
123
+
in assertEachOneOf "sslLibraries" sslLibraries [ "openssl" "bearssl" ]
124
+
stderr> error: each element in sslLibraries must be one of [
127
+
stderr> ], but is: [
71
-
# The name of the variable the user entered `val` into, for inclusion in the error message
73
-
# The list of values of what the user provided, to be compared against the values in `xs`
75
-
# The list of valid values
(lib.all (val: lib.elem val xs) vals)