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`.

+29
+29
lib/asserts.nix
··· 50 50 lib.generators.toPretty {} xs}, but is: ${ 51 51 lib.generators.toPretty {} val}"; 52 52 53 + /* Specialized `assertMsg` for checking if every one of `vals` is one of the elements 54 + of the list `xs`. Useful for checking lists of supported attributes. 55 + 56 + Example: 57 + let sslLibraries = [ "libressl" "bearssl" ]; 58 + in assertEachOneOf "sslLibraries" sslLibraries [ "openssl" "bearssl" ] 59 + stderr> error: each element in sslLibraries must be one of [ 60 + stderr> "openssl" 61 + stderr> "bearssl" 62 + stderr> ], but is: [ 63 + stderr> "libressl" 64 + stderr> "bearssl" 65 + stderr> ] 66 + 67 + Type: 68 + assertEachOneOf :: String -> List ComparableVal -> List ComparableVal -> Bool 69 + */ 70 + assertEachOneOf = 71 + # The name of the variable the user entered `val` into, for inclusion in the error message 72 + name: 73 + # The list of values of what the user provided, to be compared against the values in `xs` 74 + vals: 75 + # The list of valid values 76 + xs: 77 + assertMsg 78 + (lib.all (val: lib.elem val xs) vals) 79 + "each element in ${name} must be one of ${ 80 + lib.generators.toPretty {} xs}, but is: ${ 81 + lib.generators.toPretty {} vals}"; 53 82 }