Commit 2099ebdf authored by Profpatsch's avatar Profpatsch
Browse files

lib: add asserts.assertEachOneOf

Along the lines of `assertOneOf`, but expects a list of values to be
compared. This gives a good error message and is useful for lists of
values, like `supportedGhcVersions` in the arguments of
`haskell-language-server`.
parent 811db8c6
Loading
Loading
Loading
Loading
+29 −0
Original line number Diff line number Diff line
@@ -50,4 +50,33 @@ rec {
      lib.generators.toPretty {} xs}, but is: ${
        lib.generators.toPretty {} val}";

  /* 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}";
}