Skip to content

Commit

Permalink
refactor(types): extract fixed-size RON array type into separate func…
Browse files Browse the repository at this point in the history
…tion

Create ronArrayOf type constructor to handle fixed-size array validation and
description generation, reducing code duplication in array type handling.
  • Loading branch information
HeitorAugustoLN committed Dec 31, 2024
1 parent ed3482d commit 5bd2add
Show file tree
Hide file tree
Showing 2 changed files with 11 additions and 7 deletions.
10 changes: 10 additions & 0 deletions lib/types.nix
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,16 @@
name = "rawRon";
};

ronArrayOf =
elemType: size:
with lib.types;
addCheck (listOf elemType) (x: builtins.length x == size)
// {
description = "list of ${
optionDescriptionPhrase (class: class == "noun" || class == "composite") elemType
} with a fixed-size of ${toString size} elements";
};

ronChar = lib.mkOptionType {
check =
value:
Expand Down
8 changes: 1 addition & 7 deletions lib/utils.nix
Original file line number Diff line number Diff line change
Expand Up @@ -52,13 +52,7 @@
size = if matches != null then builtins.elemAt matches 1 else null;
in
if matches != null then
with lib.types;
(addCheck (listOf (rustToNixType' innerType)) (x: builtins.length x == (builtins.fromJSON size)))
// {
description = "list of ${
optionDescriptionPhrase (class: class == "noun" || class == "composite") (rustToNixType' innerType)
} with a fixed-size of ${size} elements";
}
lib.types.ronArrayOf (rustToNixType' innerType) (builtins.fromJSON size)
else
null;
handleTuple =
Expand Down

0 comments on commit 5bd2add

Please sign in to comment.