nixpkgs mirror (for testing) github.com/NixOS/nixpkgs
nix

Merge pull request #242695 from tweag/lib.path.subpath.components

`lib.path.subpath.components`: init

authored by

Robert Hensing and committed by
GitHub
8fa16970 35184dd3

+65
+21
lib/path/README.md
··· 187 187 188 188 </details> 189 189 190 + ### Prefer returning subpaths over components 191 + [subpath-preference]: #prefer-returning-subpaths-over-components 192 + 193 + Observing: Functions could return subpaths or lists of path component strings. 194 + 195 + Considering: Subpaths are used as inputs for some functions. Using them for outputs, too, makes the library more consistent and composable. 196 + 197 + Decision: Subpaths should be preferred over list of path component strings. 198 + 199 + <details> 200 + <summary>Arguments</summary> 201 + 202 + - (+) It is consistent with functions accepting subpaths, making the library more composable 203 + - (-) It is less efficient when the components are needed, because after creating the normalised subpath string, it will have to be parsed into components again 204 + - (+) If necessary, we can still make it faster by adding builtins to Nix 205 + - (+) Alternatively if necessary, versions of these functions that return components could later still be introduced. 206 + - (+) It makes the path library simpler because there's only two types (paths and subpaths). Only `lib.path.subpath.components` can be used to get a list of components. 207 + And once we have a list of component strings, `lib.lists` and `lib.strings` can be used to operate on them. 208 + For completeness, `lib.path.subpath.join` allows converting the list of components back to a subpath. 209 + </details> 210 + 190 211 ## Other implementations and references 191 212 192 213 - [Rust](https://doc.rust-lang.org/std/path/struct.Path.html)
+31
lib/path/default.nix
··· 438 438 ${subpathInvalidReason path}'' 439 439 ) 0 subpaths; 440 440 441 + /* 442 + Split [a subpath](#function-library-lib.path.subpath.isValid) into its path component strings. 443 + Throw an error if the subpath isn't valid. 444 + Note that the returned path components are also valid subpath strings, though they are intentionally not [normalised](#function-library-lib.path.subpath.normalise). 445 + 446 + Laws: 447 + 448 + - Splitting a subpath into components and [joining](#function-library-lib.path.subpath.join) the components gives the same subpath but [normalised](#function-library-lib.path.subpath.normalise): 449 + 450 + subpath.join (subpath.components s) == subpath.normalise s 451 + 452 + Type: 453 + subpath.components :: String -> [ String ] 454 + 455 + Example: 456 + subpath.components "." 457 + => [ ] 458 + 459 + subpath.components "./foo//bar/./baz/" 460 + => [ "foo" "bar" "baz" ] 461 + 462 + subpath.components "/foo" 463 + => <error> 464 + */ 465 + subpath.components = 466 + subpath: 467 + assert assertMsg (isValid subpath) '' 468 + lib.path.subpath.components: Argument is not a valid subpath string: 469 + ${subpathInvalidReason subpath}''; 470 + splitRelPath subpath; 471 + 441 472 /* Normalise a subpath. Throw an error if the subpath isn't valid, see 442 473 `lib.path.subpath.isValid` 443 474
+13
lib/path/tests/unit.nix
··· 238 238 expr = (builtins.tryEval (subpath.normalise "..")).success; 239 239 expected = false; 240 240 }; 241 + 242 + testSubpathComponentsExample1 = { 243 + expr = subpath.components "."; 244 + expected = [ ]; 245 + }; 246 + testSubpathComponentsExample2 = { 247 + expr = subpath.components "./foo//bar/./baz/"; 248 + expected = [ "foo" "bar" "baz" ]; 249 + }; 250 + testSubpathComponentsExample3 = { 251 + expr = (builtins.tryEval (subpath.components "/foo")).success; 252 + expected = false; 253 + }; 241 254 }; 242 255 in 243 256 if cases == [] then "Unit tests successful"