diff options
author | Valentin Gagarin <valentin.gagarin@tweag.io> | 2022-12-09 09:33:35 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2022-12-09 09:33:35 +0100 |
commit | a2b6bd7b354c93ecbc65284c55821458fab4e2b5 (patch) | |
tree | 4cd3fa3013023805b7664ab674c926dbba167603 /lib/asserts.nix | |
parent | 320a953f18b06fe346bf23b232abc59c30540d63 (diff) | |
parent | ac1ae0a58e4fa3d7d5065850b2c82dc2935d0636 (diff) |
Merge pull request #126213 from ryantm/lib-doc-gen
doc: auto-generate asserts and attrset library docs
Diffstat (limited to 'lib/asserts.nix')
-rw-r--r-- | lib/asserts.nix | 19 |
1 files changed, 15 insertions, 4 deletions
diff --git a/lib/asserts.nix b/lib/asserts.nix index 9ae357cbc935e..98e0b490acf29 100644 --- a/lib/asserts.nix +++ b/lib/asserts.nix @@ -16,11 +16,15 @@ rec { assertMsg :: Bool -> String -> Bool */ # TODO(Profpatsch): add tests that check stderr - assertMsg = pred: msg: + assertMsg = + # Predicate that needs to succeed, otherwise `msg` is thrown + pred: + # Message to throw in case `pred` fails + msg: pred || builtins.throw msg; - /* Specialized `assertMsg` for checking if val is one of the elements - of a list. Useful for checking enums. + /* Specialized `assertMsg` for checking if `val` is one of the elements + of the list `xs`. Useful for checking enums. Example: let sslLibrary = "libressl"; @@ -33,7 +37,14 @@ rec { Type: assertOneOf :: String -> ComparableVal -> List ComparableVal -> Bool */ - assertOneOf = name: val: xs: assertMsg + 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 (lib.elem val xs) "${name} must be one of ${ lib.generators.toPretty {} xs}, but is: ${ |