Skip to content

Commit

Permalink
refactor(lib): refactor enum and namedStruct assertion checks
Browse files Browse the repository at this point in the history
Refactors the attribute validations for the enum and namedStruct types
by replacing manual error throwing with lib.assertMsg assertions,
while maintaining the same behavior.
  • Loading branch information
HeitorAugustoLN committed Feb 1, 2025
1 parent 90bd33f commit 8e7ccf8
Showing 1 changed file with 20 additions and 23 deletions.
43 changes: 20 additions & 23 deletions lib/utils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -83,18 +83,18 @@ in
};
enum =
if builtins.isAttrs value then
if
builtins.attrNames value == [
"value"
"variant"
]
then
{
__type = "enum";
inherit (value) value variant;
}
else
throw "lib.cosmic.ron: enum type must receive a string or an attribute set with value and variant keys value"
assert lib.assertMsg
(
builtins.attrNames value == [
"value"
"variant"
]
)
"lib.cosmic.mkRon: enum type must receive a string or an attribute set with value and variant keys value";
{
__type = "enum";
inherit (value) value variant;
}
else
{
__type = "enum";
Expand All @@ -105,21 +105,18 @@ in
inherit value;
};
namedStruct =
if builtins.isAttrs value then
if
assert lib.assertMsg (
builtins.isAttrs value
&&
builtins.attrNames value == [
"name"
"value"
]
then
{
__type = "namedStruct";
inherit (value) name value;
}
else
throw "lib.cosmic.ron: namedStruct type must receive a attribute set with name and value keys."
else
throw "lib.cosmic.ron: namedStruct type must receive an attribute set as value.";
) "lib.cosmic.mkRon: namedStruct type must receive an attribute set with name and value keys.";
{
__type = "namedStruct";
inherit (value) name value;
};
optional = {
__type = "optional";
inherit value;
Expand Down

0 comments on commit 8e7ccf8

Please sign in to comment.