1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
|
// * Documentation: https://docs.npmjs.com/misc/semver
module semver
// * Structures.
// `Version` represents a semantic version in semver format.
pub struct Version {
pub:
major int
minor int
patch int
prerelease string
metadata string
}
// Increment represents the different types of version increments.
pub enum Increment {
major
minor
patch
}
struct EmptyInputError {
msg string = 'Empty input'
code int
}
struct InvalidVersionFormatError {
msg string
code int
}
// * Constructor.
// from returns a `Version` structure parsed from `input` `string`.
pub fn from(input string) ?Version {
if input.len == 0 {
return IError(&EmptyInputError{})
}
raw_version := parse(input)
version := raw_version.validate() or {
return IError(&InvalidVersionFormatError{
msg: 'Invalid version format for input "$input"'
})
}
return version
}
// build returns a `Version` structure with given `major`, `minor` and `patch` versions.
pub fn build(major int, minor int, patch int) Version {
// TODO Check if versions are greater than zero.
return Version{major, minor, patch, '', ''}
}
// * Transformation.
// increment returns a `Version` structure with incremented values.
pub fn (ver Version) increment(typ Increment) Version {
return increment_version(ver, typ)
}
// * Comparison.
// satisfies returns `true` if the `input` expression can be validated to `true`
// when run against this `Version`.
// Example: assert semver.build(1,0,0).satisfies('<=2.0.0') == true
// Example: assert semver.build(1,0,0).satisfies('>=2.0.0') == false
pub fn (ver Version) satisfies(input string) bool {
return version_satisfies(ver, input)
}
// eq returns `true` if `v1` is equal to `v2`.
pub fn (v1 Version) eq(v2 Version) bool {
return compare_eq(v1, v2)
}
// gt returns `true` if `v1` is greater than `v2`.
pub fn (v1 Version) gt(v2 Version) bool {
return compare_gt(v1, v2)
}
// lt returns `true` if `v1` is less than `v2`.
pub fn (v1 Version) lt(v2 Version) bool {
return compare_lt(v1, v2)
}
// ge returns `true` if `v1` is greater than or equal to `v2`.
pub fn (v1 Version) ge(v2 Version) bool {
return compare_ge(v1, v2)
}
// le returns `true` if `v1` is less than or equal to `v2`.
pub fn (v1 Version) le(v2 Version) bool {
return compare_le(v1, v2)
}
// * Utilites.
// coerce converts the `input` version to a `Version` struct.
// coerce will strip any contents *after* the parsed version string:
/*
Example:
import semver
v := semver.coerce('1.3-RC1-b2') or { semver.Version{} }
assert v.satisfies('>1.0 <2.0') == true // 1.3.0
*/
pub fn coerce(input string) ?Version {
return coerce_version(input)
}
// is_valid returns `true` if the `input` `string` can be converted to
// a (semantic) `Version` struct.
pub fn is_valid(input string) bool {
return is_version_valid(input)
}
|