diff options
Diffstat (limited to 'pkgs/development/tools/poetry2nix/poetry2nix/semver.nix')
-rw-r--r-- | pkgs/development/tools/poetry2nix/poetry2nix/semver.nix | 109 |
1 files changed, 56 insertions, 53 deletions
diff --git a/pkgs/development/tools/poetry2nix/poetry2nix/semver.nix b/pkgs/development/tools/poetry2nix/poetry2nix/semver.nix index 784589a4ca4..07dcbbc5eac 100644 --- a/pkgs/development/tools/poetry2nix/poetry2nix/semver.nix +++ b/pkgs/development/tools/poetry2nix/poetry2nix/semver.nix @@ -1,26 +1,27 @@ { lib, ireplace }: - let inherit (builtins) elemAt match; operators = let matchWildCard = s: match "([^\*])(\.[\*])" s; mkComparison = ret: version: v: builtins.compareVersions version v == ret; - mkIdxComparison = idx: version: v: let - ver = builtins.splitVersion v; - minor = builtins.toString (lib.toInt (elemAt ver idx) + 1); - upper = builtins.concatStringsSep "." (ireplace idx minor ver); - in - operators.">=" version v && operators."<" version upper; - dropWildcardPrecision = f: version: constraint: let - m = matchWildCard constraint; - hasWildcard = m != null; - c = if hasWildcard then (elemAt m 0) else constraint; - v = - if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version) - else version; - in - f v c; + mkIdxComparison = idx: version: v: + let + ver = builtins.splitVersion v; + minor = builtins.toString (lib.toInt (elemAt ver idx) + 1); + upper = builtins.concatStringsSep "." (ireplace idx minor ver); + in + operators.">=" version v && operators."<" version upper; + dropWildcardPrecision = f: version: constraint: + let + m = matchWildCard constraint; + hasWildcard = m != null; + c = if hasWildcard then (elemAt m 0) else constraint; + v = + if hasWildcard then (builtins.substring 0 (builtins.stringLength c) version) + else version; + in + f v c; in { # Prefix operators @@ -33,16 +34,17 @@ let # Semver specific operators "~" = mkIdxComparison 1; "^" = mkIdxComparison 0; - "~=" = v: c: let - # Prune constraint - parts = builtins.splitVersion c; - pruned = lib.take ((builtins.length parts) - 1) parts; - upper = builtins.toString ( - (lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1 - ); - upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned); - in - operators.">=" v c && operators."<" v upperConstraint; + "~=" = v: c: + let + # Prune constraint + parts = builtins.splitVersion c; + pruned = lib.take ((builtins.length parts) - 1) parts; + upper = builtins.toString ( + (lib.toInt (builtins.elemAt pruned (builtins.length pruned - 1))) + 1 + ); + upperConstraint = builtins.concatStringsSep "." (ireplace (builtins.length pruned - 1) upper pruned); + in + operators.">=" v c && operators."<" v upperConstraint; # Infix operators "-" = version: v: operators.">=" version v.vl && operators."<=" version v.vu; # Arbitrary equality clause, just run simple comparison @@ -55,33 +57,34 @@ let version = "([0-9\.\*x]+)"; }; - parseConstraint = constraint: let - constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint; - # The common prefix operators - mPre = match "${re.operators} *${re.version}" constraintStr; - # There is also an infix operator to match ranges - mIn = match "${re.version} *(-) *${re.version}" constraintStr; - in - ( - if mPre != null then { - op = elemAt mPre 0; - v = elemAt mPre 1; - } - # Infix operators are range matches - else if mIn != null then { - op = elemAt mIn 1; - v = { - vl = (elemAt mIn 0); - vu = (elemAt mIn 2); - }; - } - else throw "Constraint \"${constraintStr}\" could not be parsed" - ); - - satisfiesSemver = version: constraint: let - inherit (parseConstraint constraint) op v; - in - if constraint == "*" then true else operators."${op}" version v; + parseConstraint = constraint: + let + constraintStr = builtins.replaceStrings [ " " ] [ "" ] constraint; + # The common prefix operators + mPre = match "${re.operators} *${re.version}" constraintStr; + # There is also an infix operator to match ranges + mIn = match "${re.version} *(-) *${re.version}" constraintStr; + in + ( + if mPre != null then { + op = elemAt mPre 0; + v = elemAt mPre 1; + } + # Infix operators are range matches + else if mIn != null then { + op = elemAt mIn 1; + v = { + vl = (elemAt mIn 0); + vu = (elemAt mIn 2); + }; + } + else throw "Constraint \"${constraintStr}\" could not be parsed" + ); + satisfiesSemver = version: constraint: + let + inherit (parseConstraint constraint) op v; + in + if constraint == "*" then true else operators."${op}" version v; in { inherit satisfiesSemver; } |