diff options
author | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-01-18 18:06:21 +0100 |
---|---|---|
committer | Silvan Mosberger <silvan.mosberger@tweag.io> | 2023-01-18 18:19:17 +0100 |
commit | 5c4f6161981917504c50b84864dcfd2f67dc037d (patch) | |
tree | a84c535cf64b5ed6c9e61e856419024f2189009d /lib/path/default.nix | |
parent | 0667ef5dd5cc7aa00e7d5ebf4391b7ee1d414a0c (diff) |
lib.path: Minor improvements
- Use isValid when possible instead of subpathInvalidReason: https://github.com/NixOS/nixpkgs/pull/209099#discussion_r1068714681 - Add documentation to function arguments - Use newlines for error messages: https://github.com/NixOS/nixpkgs/pull/208887#discussion_r1069737602 - Add short comments for the unit test groups: https://github.com/NixOS/nixpkgs/pull/208887#discussion_r1072913051 - Slight formatting improvement for laws: https://github.com/NixOS/nixpkgs/pull/209099#discussion_r1068707955
Diffstat (limited to 'lib/path/default.nix')
-rw-r--r-- | lib/path/default.nix | 23 |
1 files changed, 16 insertions, 7 deletions
diff --git a/lib/path/default.nix b/lib/path/default.nix index 96a9244407bf5..6fa7c1dd6419f 100644 --- a/lib/path/default.nix +++ b/lib/path/default.nix @@ -25,6 +25,10 @@ let assertMsg ; + inherit (lib.path.subpath) + isValid + ; + # Return the reason why a subpath is invalid, or `null` if it's valid subpathInvalidReason = value: if ! isString value then @@ -133,7 +137,9 @@ in /* No rec! Add dependencies on this file at the top. */ { subpath.isValid "./foo//bar/" => true */ - subpath.isValid = value: + subpath.isValid = + # The value to check + value: subpathInvalidReason value == null; @@ -150,11 +156,11 @@ in /* No rec! Add dependencies on this file at the top. */ { Laws: - - (Idempotency) Normalising multiple times gives the same result: + - Idempotency - normalising multiple times gives the same result: subpath.normalise (subpath.normalise p) == subpath.normalise p - - (Uniqueness) There's only a single normalisation for the paths that lead to the same file system node: + - Uniqueness - there's only a single normalisation for the paths that lead to the same file system node: subpath.normalise p != subpath.normalise q -> $(realpath ${p}) != $(realpath ${q}) @@ -210,9 +216,12 @@ in /* No rec! Add dependencies on this file at the top. */ { subpath.normalise "/foo" => <error> */ - subpath.normalise = path: - assert assertMsg (subpathInvalidReason path == null) - "lib.path.subpath.normalise: Argument is not a valid subpath string: ${subpathInvalidReason path}"; - joinRelPath (splitRelPath path); + subpath.normalise = + # The subpath string to normalise + subpath: + assert assertMsg (isValid subpath) '' + lib.path.subpath.normalise: Argument is not a valid subpath string: + ${subpathInvalidReason subpath}''; + joinRelPath (splitRelPath subpath); } |