cargo2ebuild: Fix handling versions with `~`

The previous script behaviour was completely wrong for 2-part and 3-part
version specifiers according to the Cargo reference:
https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html#tilde-requirements

Test uses versions from the reference, i.e. `~1`, `~1.2` and `~1.2.3`
with "test" as a package name.

Before these specifiers were returned as equivalent in ebuilds:
=test-1*:=
>=test-1.2.0:= <test-0.3.0
~test-1.2.3:=

Now these specifiers are returned:
=test-1*:=
=test-1.2*:=
>=test-1.2.3:= <test-1.3.0

BUG=None
TEST=python3 -c '
from cargo2ebuild import VersionRange
for version in ["~1", "~1.2", "~1.2.3"]:
  print(VersionRange.from_str(version).to_ebuild_str("test"))
'

Change-Id: I20060fa38db4e5f80ce70356d2ccc2baae509795
Reviewed-on: https://chromium-review.googlesource.com/c/chromiumos/platform/dev-util/+/3087652
Tested-by: Adam Jelinski <ajelinski@antmicro.com>
Reviewed-by: Allen Webb <allenwebb@google.com>
Commit-Queue: Adam Jelinski <ajelinski@antmicro.com>
Commit-Queue: Allen Webb <allenwebb@google.com>
diff --git a/contrib/cargo2ebuild.py b/contrib/cargo2ebuild.py
index a413e05..b114ea6 100755
--- a/contrib/cargo2ebuild.py
+++ b/contrib/cargo2ebuild.py
@@ -165,13 +165,10 @@
                 if v_min[0] < 0:
                     raise VersionParseError(
                         'The ~ constraint operator requires a major version: {}'.format(value))
-                if v_min[1] < 0:
+                if v_min[1] < 0 and v_min[2] < 0:
                     v_max = (major + 1, 0, 0)
-                elif v_min[2] < 0:
-                    v_max = (0, minor + 1, 0)
                 else:
-                    v_max_inclusive = True
-                    v_max = (major, minor, patch)
+                    v_max = (major, minor + 1, 0)
                 v_min = (major, minor, patch)
             elif dep and dep != '^':
                 raise VersionParseError('Unrecognized operator: "{}"'.format(dep))