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