Skip to content

Commit

Permalink
Simplify code a bit and use prefix/suffix naming
Browse files Browse the repository at this point in the history
  • Loading branch information
infinisil committed Mar 14, 2023
1 parent 8f094bc commit d469d68
Showing 1 changed file with 13 additions and 17 deletions.
30 changes: 13 additions & 17 deletions lib/path/default.nix
Original file line number Diff line number Diff line change
Expand Up @@ -193,20 +193,16 @@ in /* No rec! Add dependencies on this file at the top. */ {
# Fast happy path in case all roots are the same
if all (path: path.root == firstPath.root) deconstructedPaths then firstPath.root
# Slower sad path when that's not the case and we need to throw an error
else let
trigger = foldl'
(skip: path:
if path.root == firstPath.root then skip
else throw ''
lib.path.difference: Filesystem roots must be the same for all paths, but paths with different roots were given:
${firstPath.name} = ${toString firstPath.value} (root ${toString firstPath.root})
${toString path.name} = ${toString path.value} (root ${toString path.root})''
)
null
deconstructedPaths;
fallbackAbort =
abort "lib.path.difference: This should never happen and indicates a bug! Some paths don't have the same filesystem root but it couldn't be found!";
in seq trigger fallbackAbort;
else foldl'
(skip: path:
if path.root == firstPath.root then skip
else throw ''
lib.path.difference: Filesystem roots must be the same for all paths, but paths with different roots were given:
${firstPath.name} = ${toString firstPath.value} (root ${toString firstPath.root})
${toString path.name} = ${toString path.value} (root ${toString path.root})''
)
null
deconstructedPaths;

commonAncestorLength =
let
Expand All @@ -225,18 +221,18 @@ in /* No rec! Add dependencies on this file at the top. */ {
# If we didn't do this one could evaluate `relativePaths` without an error even when there's no common root
seq commonRoot (recurse 0);

commonAncestor = commonRoot
commonPrefix = commonRoot
+ ("/" + joinRelPath (take commonAncestorLength firstPath.components));

subpaths = listToAttrs (map (path:
suffix = listToAttrs (map (path:
nameValuePair path.name (joinRelPath (drop commonAncestorLength path.components))
) deconstructedPaths);
in
assert assertMsg
(isAttrs paths)
"lib.path.difference: The given argument is of type ${typeOf paths}, but an attribute set was expected";
{
inherit commonAncestor subpaths;
inherit commonPrefix suffix;
};

/* Whether a value is a valid subpath string.
Expand Down

0 comments on commit d469d68

Please sign in to comment.