2018-08-08 19:26:52 +02:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
2022-01-17 19:12:54 +01:00
|
|
|
/* Throw if pred is false, else return pred.
|
2018-08-08 19:26:52 +02:00
|
|
|
Intended to be used to augment asserts with helpful error messages.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
assertMsg false "nope"
|
2022-01-17 19:12:54 +01:00
|
|
|
stderr> error: nope
|
2018-08-08 19:26:52 +02:00
|
|
|
|
2022-01-17 19:12:54 +01:00
|
|
|
assert assertMsg ("foo" == "bar") "foo is not bar, silly"; ""
|
|
|
|
stderr> error: foo is not bar, silly
|
2018-08-08 19:26:52 +02:00
|
|
|
|
|
|
|
Type:
|
|
|
|
assertMsg :: Bool -> String -> Bool
|
|
|
|
*/
|
|
|
|
# TODO(Profpatsch): add tests that check stderr
|
|
|
|
assertMsg = pred: msg:
|
2022-01-17 19:12:54 +01:00
|
|
|
pred || builtins.throw msg;
|
2018-08-08 19:26:52 +02:00
|
|
|
|
|
|
|
/* Specialized `assertMsg` for checking if val is one of the elements
|
|
|
|
of a list. Useful for checking enums.
|
|
|
|
|
|
|
|
Example:
|
2022-01-17 19:12:54 +01:00
|
|
|
let sslLibrary = "libressl";
|
2018-08-08 19:26:52 +02:00
|
|
|
in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
|
2022-01-17 19:12:54 +01:00
|
|
|
stderr> error: sslLibrary must be one of [
|
|
|
|
stderr> "openssl"
|
|
|
|
stderr> "bearssl"
|
|
|
|
stderr> ], but is: "libressl"
|
2018-08-08 19:26:52 +02:00
|
|
|
|
|
|
|
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}";
|
|
|
|
|
|
|
|
}
|