diff options
author | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-04-05 20:31:50 +0200 |
---|---|---|
committer | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-06-15 22:29:46 +0200 |
commit | 592213ad3f4de2cf3a0f13ab8271122e5e9f9822 (patch) | |
tree | 37b96530c9c7ca6eb3e862511043d1b9455a2b76 /lib/path/default.nix | |
parent | 866f75e5b9579a4c4f6391b68c2a31633ab13e30 (diff) |
lib.path.hasPrefix: init
Diffstat (limited to 'lib/path/default.nix')
-rw-r--r-- | lib/path/default.nix | 63 |
1 files changed, 63 insertions, 0 deletions
diff --git a/lib/path/default.nix b/lib/path/default.nix index 442060c186432..936e9b0302534 100644 --- a/lib/path/default.nix +++ b/lib/path/default.nix @@ -7,6 +7,7 @@ let isPath split match + typeOf ; inherit (lib.lists) @@ -18,6 +19,7 @@ let all concatMap foldl' + take ; inherit (lib.strings) @@ -100,6 +102,22 @@ let # An empty string is not a valid relative path, so we need to return a `.` when we have no components (if components == [] then "." else concatStringsSep "/" components); + # Type: Path -> { root :: Path, components :: [ String ] } + # + # Deconstruct a path value type into: + # - root: The filesystem root of the path, generally `/` + # - components: All the path's components + # + # This is similar to `splitString "/" (toString path)` but safer + # because it can distinguish different filesystem roots + deconstructPath = + let + recurse = components: base: + # If the parent of a path is the path itself, then it's a filesystem root + if base == dirOf base then { root = base; inherit components; } + else recurse ([ (baseNameOf base) ] ++ components) (dirOf base); + in recurse []; + in /* No rec! Add dependencies on this file at the top. */ { /* Append a subpath string to a path. @@ -155,6 +173,51 @@ in /* No rec! Add dependencies on this file at the top. */ { ${subpathInvalidReason subpath}''; path + ("/" + subpath); + /* + Whether the first path is a component-wise prefix of the second path. + + Laws: + + - `hasPrefix p q` is only true if `q == append p s` for some subpath `s`. + + - `hasPrefix` is a [non-strict partial order](https://en.wikipedia.org/wiki/Partially_ordered_set#Non-strict_partial_order) over the set of all path values + + Type: + hasPrefix :: Path -> Path -> Bool + + Example: + hasPrefix /foo /foo/bar + => true + hasPrefix /foo /foo + => true + hasPrefix /foo/bar /foo + => false + hasPrefix /. /foo + => true + */ + hasPrefix = + path1: + assert assertMsg + (isPath path1) + "lib.path.hasPrefix: First argument is of type ${typeOf path1}, but a path was expected"; + let + path1Deconstructed = deconstructPath path1; + in + path2: + assert assertMsg + (isPath path2) + "lib.path.hasPrefix: Second argument is of type ${typeOf path2}, but a path was expected"; + let + path2Deconstructed = deconstructPath path2; + in + assert assertMsg + (path1Deconstructed.root == path2Deconstructed.root) '' + lib.path.hasPrefix: Filesystem roots must be the same for both paths, but paths with different roots were given: + first argument: "${toString path1}" with root "${toString path1Deconstructed.root}" + second argument: "${toString path2}" with root "${toString path2Deconstructed.root}"''; + take (length path1Deconstructed.components) path2Deconstructed.components == path1Deconstructed.components; + + /* Whether a value is a valid subpath string. - The value is a string |