···187187188188</details>189189190190+### Prefer returning subpaths over components191191+[subpath-preference]: #prefer-returning-subpaths-over-components192192+193193+Observing: Functions could return subpaths or lists of path component strings.194194+195195+Considering: Subpaths are used as inputs for some functions. Using them for outputs, too, makes the library more consistent and composable.196196+197197+Decision: Subpaths should be preferred over list of path component strings.198198+199199+<details>200200+<summary>Arguments</summary>201201+202202+- (+) It is consistent with functions accepting subpaths, making the library more composable203203+- (-) It is less efficient when the components are needed, because after creating the normalised subpath string, it will have to be parsed into components again204204+ - (+) If necessary, we can still make it faster by adding builtins to Nix205205+ - (+) Alternatively if necessary, versions of these functions that return components could later still be introduced.206206+- (+) 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.207207+ And once we have a list of component strings, `lib.lists` and `lib.strings` can be used to operate on them.208208+ For completeness, `lib.path.subpath.join` allows converting the list of components back to a subpath.209209+</details>210210+190211## Other implementations and references191212192213- [Rust](https://doc.rust-lang.org/std/path/struct.Path.html)
+31
lib/path/default.nix
···438438 ${subpathInvalidReason path}''439439 ) 0 subpaths;440440441441+ /*442442+ Split [a subpath](#function-library-lib.path.subpath.isValid) into its path component strings.443443+ Throw an error if the subpath isn't valid.444444+ Note that the returned path components are also valid subpath strings, though they are intentionally not [normalised](#function-library-lib.path.subpath.normalise).445445+446446+ Laws:447447+448448+ - 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):449449+450450+ subpath.join (subpath.components s) == subpath.normalise s451451+452452+ Type:453453+ subpath.components :: String -> [ String ]454454+455455+ Example:456456+ subpath.components "."457457+ => [ ]458458+459459+ subpath.components "./foo//bar/./baz/"460460+ => [ "foo" "bar" "baz" ]461461+462462+ subpath.components "/foo"463463+ => <error>464464+ */465465+ subpath.components =466466+ subpath:467467+ assert assertMsg (isValid subpath) ''468468+ lib.path.subpath.components: Argument is not a valid subpath string:469469+ ${subpathInvalidReason subpath}'';470470+ splitRelPath subpath;471471+441472 /* Normalise a subpath. Throw an error if the subpath isn't valid, see442473 `lib.path.subpath.isValid`443474