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 { fn compare_gt(v1 Version, v2 Version) bool {
if v1.major < v2.major { return match true {
return false 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 { fn compare_lt(v1 Version, v2 Version) bool {
if v1.major > v2.major { return match true {
return false 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 { fn compare_ge(v1 Version, v2 Version) bool {
if compare_eq(v1, v2) { return if compare_eq(v1, v2) { true } else { compare_gt(v1, v2) }
return true
}
return compare_gt(v1, v2)
} }
fn compare_le(v1 Version, v2 Version) bool { fn compare_le(v1 Version, v2 Version) bool {
if compare_eq(v1, v2) { return if compare_eq(v1, v2) { true } else { compare_lt(v1, v2) }
return true
}
return compare_lt(v1, v2)
} }

View File

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

View File

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

View File

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