1
0
mirror of https://github.com/vlang/v.git synced 2023-08-10 21:13:21 +03:00

semver: refactor, simplify, fix typo (#18272)

This commit is contained in:
Turiiya 2023-05-27 00:57:02 +02:00 committed by GitHub
parent 3e08487198
commit f9efbdff10
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 64 additions and 102 deletions

View File

@ -13,47 +13,29 @@ fn compare_eq(v1 Version, v2 Version) bool {
}
fn compare_gt(v1 Version, v2 Version) bool {
if v1.major < v2.major {
return false
return match true {
v1.major < v2.major { false }
v1.major > v2.major { true }
v1.minor < v2.minor { false }
v1.minor > v2.minor { true }
else { v1.patch > v2.patch }
}
if v1.major > v2.major {
return true
}
if v1.minor < v2.minor {
return false
}
if v1.minor > v2.minor {
return true
}
return v1.patch > v2.patch
}
fn compare_lt(v1 Version, v2 Version) bool {
if v1.major > v2.major {
return false
return match true {
v1.major > v2.major { false }
v1.major < v2.major { true }
v1.minor > v2.minor { false }
v1.minor < v2.minor { true }
else { v1.patch < v2.patch }
}
if v1.major < v2.major {
return true
}
if v1.minor > v2.minor {
return false
}
if v1.minor < v2.minor {
return true
}
return v1.patch < v2.patch
}
fn compare_ge(v1 Version, v2 Version) bool {
if compare_eq(v1, v2) {
return true
}
return compare_gt(v1, v2)
return if compare_eq(v1, v2) { true } else { compare_gt(v1, v2) }
}
fn compare_le(v1 Version, v2 Version) bool {
if compare_eq(v1, v2) {
return true
}
return compare_lt(v1, v2)
return if compare_eq(v1, v2) { true } else { compare_lt(v1, v2) }
}

View File

@ -34,11 +34,7 @@ struct InvalidComparatorFormatError {
}
fn (r Range) satisfies(ver Version) bool {
mut final_result := false
for set in r.comparator_sets {
final_result = final_result || set.satisfies(ver)
}
return final_result
return true in r.comparator_sets.map(it.satisfies(ver))
}
fn (set ComparatorSet) satisfies(ver Version) bool {
@ -51,22 +47,13 @@ fn (set ComparatorSet) satisfies(ver Version) bool {
}
fn (c Comparator) satisfies(ver Version) bool {
if c.op == .gt {
return ver.gt(c.ver)
return match c.op {
.gt { ver.gt(c.ver) }
.lt { ver.lt(c.ver) }
.ge { ver.ge(c.ver) }
.le { ver.le(c.ver) }
.eq { ver.eq(c.ver) }
}
if c.op == .lt {
return ver.lt(c.ver)
}
if c.op == .ge {
return ver.ge(c.ver)
}
if c.op == .le {
return ver.le(c.ver)
}
if c.op == .eq {
return ver.eq(c.ver)
}
return false
}
fn parse_range(input string) !Range {
@ -105,23 +92,29 @@ fn parse_comparator_set(input string) !ComparatorSet {
fn parse_comparator(input string) ?Comparator {
mut op := Operator.eq
mut raw_version := ''
if input.starts_with('>=') {
op = .ge
raw_version = input[2..]
} else if input.starts_with('<=') {
op = .le
raw_version = input[2..]
} else if input.starts_with('>') {
op = .gt
raw_version = input[1..]
} else if input.starts_with('<') {
op = .lt
raw_version = input[1..]
} else if input.starts_with('=') {
raw_version = input[1..]
} else {
raw_version = input
raw_version := match true {
input.starts_with('>=') {
op = .ge
input[2..]
}
input.starts_with('<=') {
op = .le
input[2..]
}
input.starts_with('>') {
op = .gt
input[1..]
}
input.starts_with('<') {
op = .lt
input[1..]
}
input.starts_with('=') {
input[1..]
}
else {
input
}
}
version := coerce_version(raw_version) or { return none }
return Comparator{version, op}
@ -149,10 +142,7 @@ fn parse_xrange(input string) ?Version {
else {}
}
}
if !raw_ver.is_valid() {
return none
}
return raw_ver.to_version()
return raw_ver.validate()
}
fn can_expand(input string) bool {
@ -174,22 +164,20 @@ fn expand_comparator_set(input string) ?ComparatorSet {
fn expand_tilda(raw_version string) ?ComparatorSet {
min_ver := coerce_version(raw_version) or { return none }
mut max_ver := min_ver
if min_ver.minor == 0 && min_ver.patch == 0 {
max_ver = min_ver.increment(.major)
max_ver := if min_ver.minor == 0 && min_ver.patch == 0 {
min_ver.increment(.major)
} else {
max_ver = min_ver.increment(.minor)
min_ver.increment(.minor)
}
return make_comparator_set_ge_lt(min_ver, max_ver)
}
fn expand_caret(raw_version string) ?ComparatorSet {
min_ver := coerce_version(raw_version) or { return none }
mut max_ver := min_ver
if min_ver.major == 0 {
max_ver = min_ver.increment(.minor)
max_ver := if min_ver.major == 0 {
min_ver.increment(.minor)
} else {
max_ver = min_ver.increment(.major)
min_ver.increment(.major)
}
return make_comparator_set_ge_lt(min_ver, max_ver)
}
@ -204,43 +192,37 @@ fn expand_hyphen(raw_range string) ?ComparatorSet {
if raw_max_ver.is_missing(ver_major) {
return none
}
mut max_ver := raw_max_ver.coerce() or { return none }
if raw_max_ver.is_missing(ver_minor) {
max_ver = max_ver.increment(.minor)
max_ver := raw_max_ver.coerce() or { return none }.increment(.minor)
return make_comparator_set_ge_lt(min_ver, max_ver)
}
max_ver := raw_max_ver.coerce() or { return none }
return make_comparator_set_ge_le(min_ver, max_ver)
}
fn expand_xrange(raw_range string) ?ComparatorSet {
min_ver := parse_xrange(raw_range) or { return none }
if min_ver.major == 0 {
comparators := [
Comparator{min_ver, Operator.ge},
]
return ComparatorSet{comparators}
return ComparatorSet{[Comparator{min_ver, Operator.ge}]}
}
mut max_ver := min_ver
if min_ver.minor == 0 {
max_ver = min_ver.increment(.major)
max_ver := if min_ver.minor == 0 {
min_ver.increment(.major)
} else {
max_ver = min_ver.increment(.minor)
min_ver.increment(.minor)
}
return make_comparator_set_ge_lt(min_ver, max_ver)
}
fn make_comparator_set_ge_lt(min Version, max Version) ComparatorSet {
comparators := [
return ComparatorSet{[
Comparator{min, Operator.ge},
Comparator{max, Operator.lt},
]
return ComparatorSet{comparators}
]}
}
fn make_comparator_set_ge_le(min Version, max Version) ComparatorSet {
comparators := [
return ComparatorSet{[
Comparator{min, Operator.ge},
Comparator{max, Operator.le},
]
return ComparatorSet{comparators}
]}
}

View File

@ -43,10 +43,9 @@ pub fn from(input string) !Version {
return &EmptyInputError{}
}
raw_version := parse(input)
version := raw_version.validate() or { return &InvalidVersionFormatError{
return raw_version.validate() or { return &InvalidVersionFormatError{
input: input
} }
return version
}
// build returns a `Version` structure with given `major`, `minor` and `patch` versions.
@ -105,7 +104,7 @@ pub fn (ver Version) str() string {
return '${common_string}${prerelease_string}${metadata_string}'
}
// * Utilites.
// * Utilities.
// coerce converts the `input` version to a `Version` struct.
// coerce will strip any contents *after* the parsed version string:
/*

View File

@ -10,8 +10,7 @@ fn is_version_valid(input string) bool {
[inline]
fn coerce_version(input string) !Version {
raw_ver := parse(input)
ver := raw_ver.coerce() or { return error('Invalid version for input "${input}"') }
return ver
return raw_ver.coerce() or { return error('Invalid version for input "${input}"') }
}
[inline]