2018-08-08 17:26:52 +00:00
|
|
|
{ lib }:
|
|
|
|
|
|
|
|
rec {
|
|
|
|
|
2022-01-17 18:12:54 +00:00
|
|
|
/* Throw if pred is false, else return pred.
|
2018-08-08 17:26:52 +00:00
|
|
|
Intended to be used to augment asserts with helpful error messages.
|
|
|
|
|
|
|
|
Example:
|
|
|
|
assertMsg false "nope"
|
2022-01-17 18:12:54 +00:00
|
|
|
stderr> error: nope
|
2018-08-08 17:26:52 +00:00
|
|
|
|
2022-01-17 18:12:54 +00:00
|
|
|
assert assertMsg ("foo" == "bar") "foo is not bar, silly"; ""
|
|
|
|
stderr> error: foo is not bar, silly
|
2018-08-08 17:26:52 +00:00
|
|
|
|
|
|
|
Type:
|
|
|
|
assertMsg :: Bool -> String -> Bool
|
|
|
|
*/
|
|
|
|
# TODO(Profpatsch): add tests that check stderr
|
2021-06-08 14:03:12 +00:00
|
|
|
assertMsg =
|
|
|
|
# Predicate that needs to succeed, otherwise `msg` is thrown
|
|
|
|
pred:
|
|
|
|
# Message to throw in case `pred` fails
|
|
|
|
msg:
|
2022-01-17 18:12:54 +00:00
|
|
|
pred || builtins.throw msg;
|
2018-08-08 17:26:52 +00:00
|
|
|
|
2021-06-08 14:03:12 +00:00
|
|
|
/* Specialized `assertMsg` for checking if `val` is one of the elements
|
|
|
|
of the list `xs`. Useful for checking enums.
|
2018-08-08 17:26:52 +00:00
|
|
|
|
|
|
|
Example:
|
2022-01-17 18:12:54 +00:00
|
|
|
let sslLibrary = "libressl";
|
2018-08-08 17:26:52 +00:00
|
|
|
in assertOneOf "sslLibrary" sslLibrary [ "openssl" "bearssl" ]
|
2022-01-17 18:12:54 +00:00
|
|
|
stderr> error: sslLibrary must be one of [
|
|
|
|
stderr> "openssl"
|
|
|
|
stderr> "bearssl"
|
|
|
|
stderr> ], but is: "libressl"
|
2018-08-08 17:26:52 +00:00
|
|
|
|
|
|
|
Type:
|
|
|
|
assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool
|
|
|
|
*/
|
2021-06-08 14:03:12 +00:00
|
|
|
assertOneOf =
|
|
|
|
# The name of the variable the user entered `val` into, for inclusion in the error message
|
|
|
|
name:
|
|
|
|
# The value of what the user provided, to be compared against the values in `xs`
|
|
|
|
val:
|
|
|
|
# The list of valid values
|
|
|
|
xs:
|
|
|
|
assertMsg
|
2018-08-08 17:26:52 +00:00
|
|
|
(lib.elem val xs)
|
|
|
|
"${name} must be one of ${
|
|
|
|
lib.generators.toPretty {} xs}, but is: ${
|
|
|
|
lib.generators.toPretty {} val}";
|
|
|
|
|
|
|
|
}
|