/
CommonParsers.kt
306 lines (266 loc) · 7.59 KB
/
CommonParsers.kt
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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
package parsix.core
import parsix.fp.result.Failure
import parsix.fp.result.Ok
/**
* The most basic parse, it will always succeed with [result]
*/
fun <I, O> succeed(result: O): Parse<I, O> =
{ _ -> Ok(result) }
/**
* Enhance [parse] so that it can handle a nullable input.
* The final parser will return [RequiredError] if the input is null.
*/
inline fun <I : Any, O> notNullable(crossinline parse: Parse<I, O>): Parse<I?, O> =
{ inp ->
if (inp == null)
Failure(RequiredError)
else
parse(inp)
}
/**
* Enhance [parse] so that it can handle a nullable input.
* If the input is null, it will use [default] as value.
*/
inline fun <I : Any, O : Any> nullable(
default: O,
crossinline parse: Parse<I, O>
): Parse<I?, O> =
{ inp ->
if (inp == null)
Ok(default)
else
parse(inp)
}
/**
* Enhance [parse] so that it can handle a nullable input.
* If the input is null, that will be the result.
*/
inline fun <I : Any, O : Any> nullable(crossinline parse: Parse<I, O>): Parse<I?, O?> =
{ inp ->
if (inp == null)
Ok(null)
else
parse(inp)
}
/**
* Parse [Any] into [String].
* @return [StringError] in case of failure.
*/
fun parseString(inp: Any): Parsed<String> =
when (inp) {
is String ->
Ok(inp)
is Number ->
Ok(inp.toString())
is Char ->
Ok(inp.toString())
else ->
Failure(StringError(inp))
}
/**
* Parse [Any] into [Boolean].
* @return [BoolError] in case of failure.
*/
fun parseBool(inp: Any): Parsed<Boolean> =
parseTyped(inp, ::BoolError)
/**
* Generic parser, it can be used to easily convert from [Any] to a specific type [T]
*/
inline fun <reified T> parseTyped(
inp: Any,
crossinline mkErr: (Any) -> TerminalError
): Parsed<T> =
if (inp is T)
Ok(inp)
else
Failure(mkErr(inp))
/**
* Make a `parse` that ensures a [Comparable] is greater than or equal to [min]
* and returns [MinError] otherwise
*
* A common case is to use it with numbers, for example:
* ```
* parseMin(10)(4) // => MinError(10)
* parseMin(10.5)(11.0) // => Ok(10.5)
* ```
*
* @see parseBetween if you need a range
*/
fun <T : Comparable<T>> parseMin(min: T): Parse<T, T> = { inp ->
if (inp < min)
Failure(MinError(inp, min))
else
Ok(inp)
}
/**
* Make a `parse` that ensures a [Comparable] is less a than or equal to [max]
* and returns [MaxError] otherwise
*
* A common case is to use it with numbers, for example:
* ```
* parseMax(10)(4) // => MaxError(10)
* parseMin(10.5)(11.0) // => Ok(10.5)
* ```
*
* @see parseBetween if you need a range
*/
fun <T : Comparable<T>> parseMax(max: T): Parse<T, T> = { inp ->
if (inp > max)
Failure(MaxError(inp, max))
else
Ok(inp)
}
/**
* Make a `parse` that ensures [Comparable] is between [min] and [max], inclusive,
* and returns [BetweenError] otherwise.
*
* @see parseMin
* @see parseMax
*/
fun <T : Comparable<T>> parseBetween(min: T, max: T): Parse<T, T> = { inp ->
when {
inp < min ->
Failure(BetweenError(inp, min, max))
inp > max ->
Failure(BetweenError(inp, min, max))
else ->
Ok(inp)
}
}
/**
* Parse an [Any] into a [Int].
* It supports the following types:
* - [Int], returns it
* - [UInt], value must be no greater than [Int.MAX_VALUE], [MaxError] otherwise
* - [Long], value must be between [Int.MIN_VALUE] and [Int.MAX_VALUE], [BetweenError] otherwise
* - [Double], value must be between [Int.MIN_VALUE] and [Int.MAX_VALUE], [BetweenError] otherwise
* - [String], must be a valid int, [IntError] otherwise
*
* Anything else will fail with [IntError]
*/
fun parseInt(inp: Any): Parsed<Int> =
when (inp) {
is String ->
try {
Ok(inp.toInt())
} catch (ex: NumberFormatException) {
Failure(IntError(inp))
}
is Int ->
Ok(inp)
is UInt ->
if (inp > Int.MAX_VALUE.toUInt())
Failure(MaxError(inp, Int.MAX_VALUE))
else
Ok(inp.toInt())
is Long ->
if (Int.MIN_VALUE > inp || Int.MAX_VALUE < inp)
Failure(BetweenError(inp, Int.MIN_VALUE, Int.MAX_VALUE))
else
Ok(inp.toInt())
is Double ->
if (Int.MIN_VALUE > inp || Int.MAX_VALUE < inp)
Failure(BetweenError(inp, Int.MIN_VALUE, Int.MAX_VALUE))
else
Ok(inp.toInt())
else ->
Failure(IntError(inp))
}
/**
* Parse an [Any] into a [UInt].
* It supports the following types:
* - [UInt], returns it
* - [Int], value cannot be less than 0, [MinError] otherwise
* - [Long], value must be between 0 and [UInt.MAX_VALUE], [BetweenError] otherwise
* - [String], must be a valid unsigned int, [UIntError] otherwise
*
* Anything else will fail with [UIntError]
*/
fun parseUInt(inp: Any): Parsed<UInt> =
when (inp) {
is String ->
try {
Ok(inp.toUInt())
} catch (ex: NumberFormatException) {
Failure(UIntError(inp))
}
is UInt ->
Ok(inp)
is Int ->
if (inp < 0)
Failure(MinError(inp, 0))
else
Ok(inp.toUInt())
is Long ->
if (inp < 0 || inp > UInt.MAX_VALUE.toLong())
Failure(BetweenError(inp, UInt.MIN_VALUE, UInt.MAX_VALUE))
else
Ok(inp.toUInt())
is Double ->
if (inp < UInt.MIN_VALUE.toDouble() || inp > UInt.MAX_VALUE.toDouble())
Failure(BetweenError(inp, UInt.MIN_VALUE, UInt.MAX_VALUE))
else
Ok(inp.toUInt())
else ->
Failure(UIntError(inp))
}
/**
* Parse an [Any] into a [Long].
* It supports the following types:
* - [Long], [Int], [UInt]
* - [Double], must be between [Long.MIN_VALUE] and [Long.MAX_VALUE], [BetweenError] otherwise
* - [String], must be a valid long int, [LongError] otherwise
*
* Anything else will fail with [LongError]
*/
fun parseLong(inp: Any): Parsed<Long> =
when (inp) {
is String ->
try {
Ok(inp.toLong())
} catch (ex: NumberFormatException) {
Failure(LongError(inp))
}
is Long ->
Ok(inp)
is UInt ->
Ok(inp.toLong())
is Int ->
Ok(inp.toLong())
is Double ->
if (inp < Long.MIN_VALUE.toDouble() || inp > Long.MAX_VALUE.toDouble())
Failure(BetweenError(inp, Long.MIN_VALUE, Long.MAX_VALUE))
else
Ok(inp.toLong())
else ->
Failure(LongError(inp))
}
/**
* Parse an [Any] into a [Double].
* It supports the following types:
* - [Double], [Float], [Int], [UInt], [Long]
* - [String], must be a valid double, [DoubleError] otherwise
*
* Anything else will fail with [DoubleError]
*/
fun parseDouble(inp: Any): Parsed<Double> =
when (inp) {
is String ->
try {
Ok(inp.toDouble())
} catch (ex: NumberFormatException) {
Failure(LongError(inp))
}
is Double ->
Ok(inp)
is Float ->
Ok(inp.toDouble())
is Long ->
Ok(inp.toDouble())
is UInt ->
Ok(inp.toDouble())
is Int ->
Ok(inp.toDouble())
else ->
Failure(DoubleError(inp))
}