2020-10-26 20:05:18 +03:00
|
|
|
module semver
|
|
|
|
|
|
|
|
// * Private functions.
|
|
|
|
const (
|
|
|
|
comparator_sep = ' '
|
|
|
|
comparator_set_sep = ' || '
|
|
|
|
hyphen_range_sep = ' - '
|
|
|
|
x_range_symbols = 'Xx*'
|
|
|
|
)
|
|
|
|
|
|
|
|
enum Operator {
|
|
|
|
gt
|
|
|
|
lt
|
|
|
|
ge
|
|
|
|
le
|
|
|
|
eq
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Comparator {
|
|
|
|
ver Version
|
|
|
|
op Operator
|
|
|
|
}
|
|
|
|
|
|
|
|
struct ComparatorSet {
|
|
|
|
comparators []Comparator
|
|
|
|
}
|
|
|
|
|
|
|
|
struct Range {
|
|
|
|
comparator_sets []ComparatorSet
|
|
|
|
}
|
|
|
|
|
2021-03-29 12:17:00 +03:00
|
|
|
struct InvalidComparatorFormatError {
|
2022-02-11 16:52:33 +03:00
|
|
|
MessageError
|
2021-03-29 12:17:00 +03:00
|
|
|
}
|
|
|
|
|
2020-10-26 20:05:18 +03:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (set ComparatorSet) satisfies(ver Version) bool {
|
|
|
|
for comp in set.comparators {
|
|
|
|
if !comp.satisfies(ver) {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (c Comparator) satisfies(ver Version) bool {
|
2021-06-07 16:24:42 +03:00
|
|
|
if c.op == .gt {
|
|
|
|
return ver.gt(c.ver)
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
2021-06-07 16:24:42 +03:00
|
|
|
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
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_range(input string) ?Range {
|
2021-01-25 12:26:20 +03:00
|
|
|
raw_comparator_sets := input.split(semver.comparator_set_sep)
|
2020-10-26 20:05:18 +03:00
|
|
|
mut comparator_sets := []ComparatorSet{}
|
|
|
|
for raw_comp_set in raw_comparator_sets {
|
|
|
|
if can_expand(raw_comp_set) {
|
2021-02-28 23:20:21 +03:00
|
|
|
s := expand_comparator_set(raw_comp_set) or { return err }
|
2020-10-26 20:05:18 +03:00
|
|
|
comparator_sets << s
|
|
|
|
} else {
|
2021-02-28 23:20:21 +03:00
|
|
|
s := parse_comparator_set(raw_comp_set) or { return err }
|
2020-10-26 20:05:18 +03:00
|
|
|
comparator_sets << s
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return Range{comparator_sets}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_comparator_set(input string) ?ComparatorSet {
|
2021-01-25 12:26:20 +03:00
|
|
|
raw_comparators := input.split(semver.comparator_sep)
|
2020-10-26 20:05:18 +03:00
|
|
|
if raw_comparators.len > 2 {
|
2022-10-28 19:08:30 +03:00
|
|
|
return &InvalidComparatorFormatError{
|
2021-03-29 12:17:00 +03:00
|
|
|
msg: 'Invalid format of comparator set for input "$input"'
|
2022-10-28 19:08:30 +03:00
|
|
|
}
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
mut comparators := []Comparator{}
|
|
|
|
for raw_comp in raw_comparators {
|
2020-12-30 19:07:21 +03:00
|
|
|
c := parse_comparator(raw_comp) or {
|
2022-10-28 19:08:30 +03:00
|
|
|
return &InvalidComparatorFormatError{
|
2021-03-29 12:17:00 +03:00
|
|
|
msg: 'Invalid comparator "$raw_comp" in input "$input"'
|
2022-10-28 19:08:30 +03:00
|
|
|
}
|
2020-12-30 19:07:21 +03:00
|
|
|
}
|
2020-10-26 20:05:18 +03:00
|
|
|
comparators << c
|
|
|
|
}
|
|
|
|
return ComparatorSet{comparators}
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2020-12-21 22:25:19 +03:00
|
|
|
version := coerce_version(raw_version) or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
return Comparator{version, op}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn parse_xrange(input string) ?Version {
|
|
|
|
mut raw_ver := parse(input).complete()
|
|
|
|
for typ in versions {
|
2021-01-25 12:26:20 +03:00
|
|
|
if raw_ver.raw_ints[typ].index_any(semver.x_range_symbols) == -1 {
|
2020-10-26 20:05:18 +03:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
match typ {
|
|
|
|
ver_major {
|
|
|
|
raw_ver.raw_ints[ver_major] = '0'
|
|
|
|
raw_ver.raw_ints[ver_minor] = '0'
|
|
|
|
raw_ver.raw_ints[ver_patch] = '0'
|
|
|
|
}
|
|
|
|
ver_minor {
|
|
|
|
raw_ver.raw_ints[ver_minor] = '0'
|
|
|
|
raw_ver.raw_ints[ver_patch] = '0'
|
|
|
|
}
|
|
|
|
ver_patch {
|
|
|
|
raw_ver.raw_ints[ver_patch] = '0'
|
|
|
|
}
|
|
|
|
else {}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if !raw_ver.is_valid() {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return raw_ver.to_version()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn can_expand(input string) bool {
|
2021-01-25 12:26:20 +03:00
|
|
|
return input[0] == `~` || input[0] == `^` || input.contains(semver.hyphen_range_sep)
|
|
|
|
|| input.index_any(semver.x_range_symbols) > -1
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_comparator_set(input string) ?ComparatorSet {
|
|
|
|
match input[0] {
|
|
|
|
`~` { return expand_tilda(input[1..]) }
|
|
|
|
`^` { return expand_caret(input[1..]) }
|
|
|
|
else {}
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
if input.contains(semver.hyphen_range_sep) {
|
2020-10-26 20:05:18 +03:00
|
|
|
return expand_hyphen(input)
|
|
|
|
}
|
|
|
|
return expand_xrange(input)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_tilda(raw_version string) ?ComparatorSet {
|
2020-12-21 22:25:19 +03:00
|
|
|
min_ver := coerce_version(raw_version) or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
mut max_ver := min_ver
|
|
|
|
if min_ver.minor == 0 && min_ver.patch == 0 {
|
|
|
|
max_ver = min_ver.increment(.major)
|
|
|
|
} else {
|
|
|
|
max_ver = min_ver.increment(.minor)
|
|
|
|
}
|
|
|
|
return make_comparator_set_ge_lt(min_ver, max_ver)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_caret(raw_version string) ?ComparatorSet {
|
2020-12-21 22:25:19 +03:00
|
|
|
min_ver := coerce_version(raw_version) or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
mut max_ver := min_ver
|
|
|
|
if min_ver.major == 0 {
|
|
|
|
max_ver = min_ver.increment(.minor)
|
|
|
|
} else {
|
|
|
|
max_ver = min_ver.increment(.major)
|
|
|
|
}
|
|
|
|
return make_comparator_set_ge_lt(min_ver, max_ver)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_hyphen(raw_range string) ?ComparatorSet {
|
2021-01-25 12:26:20 +03:00
|
|
|
raw_versions := raw_range.split(semver.hyphen_range_sep)
|
2020-10-26 20:05:18 +03:00
|
|
|
if raw_versions.len != 2 {
|
|
|
|
return none
|
|
|
|
}
|
2020-12-21 22:25:19 +03:00
|
|
|
min_ver := coerce_version(raw_versions[0]) or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
raw_max_ver := parse(raw_versions[1])
|
|
|
|
if raw_max_ver.is_missing(ver_major) {
|
|
|
|
return none
|
|
|
|
}
|
2020-12-21 22:25:19 +03:00
|
|
|
mut max_ver := raw_max_ver.coerce() or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
if raw_max_ver.is_missing(ver_minor) {
|
|
|
|
max_ver = max_ver.increment(.minor)
|
|
|
|
return make_comparator_set_ge_lt(min_ver, max_ver)
|
|
|
|
}
|
|
|
|
return make_comparator_set_ge_le(min_ver, max_ver)
|
|
|
|
}
|
|
|
|
|
|
|
|
fn expand_xrange(raw_range string) ?ComparatorSet {
|
2020-12-21 22:25:19 +03:00
|
|
|
min_ver := parse_xrange(raw_range) or { return none }
|
2020-10-26 20:05:18 +03:00
|
|
|
if min_ver.major == 0 {
|
|
|
|
comparators := [
|
|
|
|
Comparator{min_ver, Operator.ge},
|
|
|
|
]
|
|
|
|
return ComparatorSet{comparators}
|
|
|
|
}
|
|
|
|
mut max_ver := min_ver
|
|
|
|
if min_ver.minor == 0 {
|
|
|
|
max_ver = min_ver.increment(.major)
|
|
|
|
} else {
|
|
|
|
max_ver = 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 := [
|
|
|
|
Comparator{min, Operator.ge},
|
|
|
|
Comparator{max, Operator.lt},
|
|
|
|
]
|
|
|
|
return ComparatorSet{comparators}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn make_comparator_set_ge_le(min Version, max Version) ComparatorSet {
|
|
|
|
comparators := [
|
|
|
|
Comparator{min, Operator.ge},
|
|
|
|
Comparator{max, Operator.le},
|
|
|
|
]
|
|
|
|
return ComparatorSet{comparators}
|
|
|
|
}
|