Debian Codegraph @

extra/haxelib_src/src/haxelib/SemVer.hx

raw
1 /*
2 * Copyright (C)2005-2016 Haxe Foundation
3 *
4 * Permission is hereby granted, free of charge, to any person obtaining a
5 * copy of this software and associated documentation files (the "Software"),
6 * to deal in the Software without restriction, including without limitation
7 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8 * and/or sell copies of the Software, and to permit persons to whom the
9 * Software is furnished to do so, subject to the following conditions:
10 *
11 * The above copyright notice and this permission notice shall be included in
12 * all copies or substantial portions of the Software.
13 *
14 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
19 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
20 * DEALINGS IN THE SOFTWARE.
21 */
22 package haxelib;
23
24 import haxe.ds.Option;
25
26 using Std;
27
28 enum Preview {
29 ALPHA;
30 BETA;
31 RC;
32 }
33
34 abstract SemVer(String) to String {
35
36 public var major(get, never):Int;
37 public var minor(get, never):Int;
38 public var patch(get, never):Int;
39 public var preview(get, never):Null<Preview>;
40 public var previewNum(get, never):Null<Int>;
41 public var data(get, never):SemVerData;
42 public var valid(get, never):Bool;
43
44 inline function new(s) this = s;
45
46 static public function compare(a:SemVer, b:SemVer) {
47 function toArray(data:SemVerData)
48 return [
49 data.major,
50 data.minor,
51 data.patch,
52 if (data.preview == null) 100 else data.preview.getIndex(),
53 if (data.previewNum == null) -1 else data.previewNum
54 ];
55
56 var a = toArray(a.data),
57 b = toArray(b.data);
58
59 for (i in 0...a.length)
60 switch Reflect.compare(a[i], b[i]) {
61 case 0:
62 case v: return v;
63 }
64
65 return 0;
66 }
67 @:to public function toValidatable():Validator.Validatable
68 return {
69 validate:
70 function ():Option<String>
71 return
72 try {
73 get_data();
74 None;
75 }
76 catch (e:Dynamic)
77 Some(Std.string(e))
78 }
79
80 inline function get_major()
81 return data.major;
82
83 inline function get_minor()
84 return data.minor;
85
86 inline function get_patch()
87 return data.patch;
88
89 inline function get_preview()
90 return data.preview;
91
92 inline function get_previewNum()
93 return data.previewNum;
94
95 inline function get_valid()
96 return isValid(this);
97
98 @:op(a > b) static inline function gt(a:SemVer, b:SemVer)
99 return compare(a, b) == 1;
100
101 @:op(a >= b) static inline function gteq(a:SemVer, b:SemVer)
102 return compare(a, b) != -1;
103
104 @:op(a < b) static inline function lt(a:SemVer, b:SemVer)
105 return compare(a, b) == -1;
106
107 @:op(a <= b) static inline function lteq(a:SemVer, b:SemVer)
108 return compare(a, b) != 1;
109
110 @:op(a == b) static inline function eq(a:SemVer, b:SemVer)
111 return compare(a, b) == 0;
112
113 @:op(a != b) static inline function neq(a:SemVer, b:SemVer)
114 return compare(a, b) != 0;
115
116 static var FORMAT = ~/^(\d|[1-9]\d*)\.(\d|[1-9]\d*)\.(\d|[1-9]\d*)(-(alpha|beta|rc)(\.(\d|[1-9]\d*))?)?$/;
117
118 static var cache = new Map();
119
120 @:to function get_data():SemVerData {
121 if (!cache.exists(this))
122 cache[this] = getData();
123 return cache[this];
124 }
125
126 @:from static function fromData(data:SemVerData)
127 return
128 new SemVer(
129 data.major + '.' + data.minor + '.' + data.patch +
130 if (data.preview == null) ''
131 else '-' + data.preview.getName().toLowerCase() +
132 if (data.previewNum == null) '';
133 else '.' + data.previewNum
134 );
135
136 function getData():SemVerData
137 return
138 if (valid) {//RAPTORS: This query will already cause the matching.
139 major: FORMAT.matched(1).parseInt(),
140 minor: FORMAT.matched(2).parseInt(),
141 patch: FORMAT.matched(3).parseInt(),
142 preview:
143 switch FORMAT.matched(5) {
144 case 'alpha': ALPHA;
145 case 'beta': BETA;
146 case 'rc': RC;
147 case v if (v == null): null;
148 case v: throw 'unrecognized preview tag $v';
149 },
150 previewNum:
151 switch FORMAT.matched(7) {
152 case null: null;
153 case v: v.parseInt();
154 }
155 }
156 else
157 throw '$this is not a valid version string';//TODO: include some URL for reference
158
159 static public function isValid(s:String)
160 return Std.is(s, String) && FORMAT.match(s.toLowerCase());
161
162 static public function ofString(s:String) {
163 var ret = new SemVer(s);
164 ret.getData();
165 return ret;
166 }
167
168 static public var DEFAULT(default, null) = new SemVer('0.0.0');
169 }
170
171 typedef SemVerData = {
172 major:Int,
173 minor:Int,
174 patch:Int,
175 preview:Null<Preview>,
176 previewNum:Null<Int>,
177 }