2020-10-26 20:05:18 +03:00
|
|
|
module semver
|
|
|
|
|
|
|
|
// * Private structs and functions.
|
|
|
|
struct RawVersion {
|
|
|
|
prerelease string
|
|
|
|
metadata string
|
|
|
|
mut:
|
2021-01-12 06:38:43 +03:00
|
|
|
raw_ints []string
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
ver_major = 0
|
|
|
|
ver_minor = 1
|
|
|
|
ver_patch = 2
|
|
|
|
versions = [ver_major, ver_minor, ver_patch]
|
|
|
|
)
|
|
|
|
|
|
|
|
// TODO: Rewrite using regexps?
|
|
|
|
// /(\d+)\.(\d+)\.(\d+)(?:\-([0-9A-Za-z-.]+))?(?:\+([0-9A-Za-z-]+))?/
|
|
|
|
fn parse(input string) RawVersion {
|
|
|
|
mut raw_version := input
|
|
|
|
mut prerelease := ''
|
|
|
|
mut metadata := ''
|
2020-12-21 22:25:19 +03:00
|
|
|
plus_idx := raw_version.last_index('+') or { -1 }
|
2020-10-26 20:05:18 +03:00
|
|
|
if plus_idx > 0 {
|
|
|
|
metadata = raw_version[(plus_idx + 1)..]
|
|
|
|
raw_version = raw_version[0..plus_idx]
|
|
|
|
}
|
2020-12-21 22:25:19 +03:00
|
|
|
hyphen_idx := raw_version.index('-') or { -1 }
|
2020-10-26 20:05:18 +03:00
|
|
|
if hyphen_idx > 0 {
|
|
|
|
prerelease = raw_version[(hyphen_idx + 1)..]
|
|
|
|
raw_version = raw_version[0..hyphen_idx]
|
|
|
|
}
|
|
|
|
raw_ints := raw_version.split('.')
|
|
|
|
return RawVersion{
|
|
|
|
prerelease: prerelease
|
|
|
|
metadata: metadata
|
|
|
|
raw_ints: raw_ints
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (ver RawVersion) is_valid() bool {
|
|
|
|
if ver.raw_ints.len != 3 {
|
|
|
|
return false
|
|
|
|
}
|
2021-01-25 12:26:20 +03:00
|
|
|
return is_valid_number(ver.raw_ints[semver.ver_major])
|
|
|
|
&& is_valid_number(ver.raw_ints[semver.ver_minor])
|
|
|
|
&& is_valid_number(ver.raw_ints[semver.ver_patch]) && is_valid_string(ver.prerelease)
|
2021-01-23 11:33:22 +03:00
|
|
|
&& is_valid_string(ver.metadata)
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (ver RawVersion) is_missing(typ int) bool {
|
|
|
|
return typ >= ver.raw_ints.len - 1
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (raw_ver RawVersion) coerce() ?Version {
|
|
|
|
ver := raw_ver.complete()
|
2021-01-25 12:26:20 +03:00
|
|
|
if !is_valid_number(ver.raw_ints[semver.ver_major]) {
|
2022-11-15 16:53:13 +03:00
|
|
|
return error('Invalid major version: ${ver.raw_ints}[ver_major]')
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|
|
|
|
return ver.to_version()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (raw_ver RawVersion) complete() RawVersion {
|
2022-08-25 08:52:13 +03:00
|
|
|
mut raw_ints := raw_ver.raw_ints.clone()
|
2020-10-26 20:05:18 +03:00
|
|
|
for raw_ints.len < 3 {
|
|
|
|
raw_ints << '0'
|
|
|
|
}
|
|
|
|
return RawVersion{
|
|
|
|
prerelease: raw_ver.prerelease
|
|
|
|
metadata: raw_ver.metadata
|
|
|
|
raw_ints: raw_ints
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (raw_ver RawVersion) validate() ?Version {
|
|
|
|
if !raw_ver.is_valid() {
|
|
|
|
return none
|
|
|
|
}
|
|
|
|
return raw_ver.to_version()
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (raw_ver RawVersion) to_version() Version {
|
2021-01-25 12:26:20 +03:00
|
|
|
return Version{raw_ver.raw_ints[semver.ver_major].int(), raw_ver.raw_ints[semver.ver_minor].int(), raw_ver.raw_ints[semver.ver_patch].int(), raw_ver.prerelease, raw_ver.metadata}
|
2020-10-26 20:05:18 +03:00
|
|
|
}
|