lib/utils: add mkAssertions

This commit is contained in:
Gaetan Lepage 2025-01-20 13:52:53 +01:00
parent 60adb6c56b
commit a7e516b322
3 changed files with 68 additions and 4 deletions

View file

@ -82,6 +82,7 @@ lib.makeExtensible (
ifNonNull' ifNonNull'
listToUnkeyedAttrs listToUnkeyedAttrs
literalLua literalLua
mkAssertions
mkIfNonNull mkIfNonNull
mkIfNonNull' mkIfNonNull'
mkRaw mkRaw

View file

@ -173,6 +173,37 @@ rec {
in in
lib.literalExpression exp; lib.literalExpression exp;
__messagePrefix = scope: "Nixvim (${scope}):";
/**
Process one or several assertions by prepending the common 'Nixvim (<scope>): ' prefix to messages.
The second argument can either be a list of assertions or a single one.
# Example
```nix
warnings = mkAssertions "plugins.foo" {
assertion = plugins.foo.settings.barIntegration && (!plugins.bar.enable);
message = "`barIntegration` is enabled but the `bar` plugin is not."
}
```
# Type
```
mkAssertions :: String -> List -> List
```
*/
mkAssertions =
scope: assertions:
let
prefix = __messagePrefix scope;
processAssertion = a: {
inherit (a) assertion;
message = "${prefix} ${lib.trim a.message}";
};
in
builtins.map processAssertion (lib.toList assertions);
/** /**
Convert one or several conditional warnings to a final warning list. Convert one or several conditional warnings to a final warning list.
The second argument can either be a list of _conditional warnings_ or a single one. The second argument can either be a list of _conditional warnings_ or a single one.
@ -195,9 +226,9 @@ rec {
mkWarnings = mkWarnings =
scope: warnings: scope: warnings:
let let
prefix = __messagePrefix scope;
processWarning = processWarning =
warning: warning: lib.optional (warning.when or true) "${prefix} ${lib.trim (warning.message or warning)}";
lib.optional (warning.when or true) "Nixvim (${scope}): ${lib.trim (warning.message or warning)}";
in in
builtins.concatMap processWarning (lib.toList warnings); builtins.concatMap processWarning (lib.toList warnings);

View file

@ -491,6 +491,38 @@ let
}; };
}; };
testMkAssertions = {
expr =
(lib.nixvim.mkAssertions "foo-bar" [
{
assertion = true;
message = "This is the message";
}
{
assertion = false;
message = "Another assertion";
}
])
++ (lib.nixvim.mkAssertions "single-element") {
assertion = true;
message = " Trailing whitespaces ";
};
expected = [
{
assertion = true;
message = "Nixvim (foo-bar): This is the message";
}
{
assertion = false;
message = "Nixvim (foo-bar): Another assertion";
}
{
assertion = true;
message = "Nixvim (single-element): Trailing whitespaces";
}
];
};
testMkWarnings = { testMkWarnings = {
expr = expr =
(lib.nixvim.mkWarnings "foo-bar" [ (lib.nixvim.mkWarnings "foo-bar" [
@ -506,12 +538,12 @@ let
]) ])
++ (lib.nixvim.mkWarnings "single-element") { ++ (lib.nixvim.mkWarnings "single-element") {
when = true; when = true;
message = "Hello"; message = " Trailing whitespaces ";
}; };
expected = [ expected = [
"Nixvim (foo-bar): This is the message" "Nixvim (foo-bar): This is the message"
"Nixvim (foo-bar): This is an unconditional warning" "Nixvim (foo-bar): This is an unconditional warning"
"Nixvim (single-element): Hello" "Nixvim (single-element): Trailing whitespaces"
]; ];
}; };
}; };