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