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