rustils/parse/int.rs
1use error::ParseError;
2use RoundingMode;
3use RoundingMode::*;
4
5pub trait ToI32 {
6
7 fn to_i32_res(self)
8 -> ParseResultI32;
9
10 fn to_i32(self)
11 -> i32;
12}
13
14pub trait ToI32RM {
15
16 fn to_i32_rm_res(self, rm: RoundingMode)
17 -> ParseResultI32;
18
19 fn to_i32_rm(self, rm: RoundingMode)
20 -> i32;
21}
22
23/// Parse [`bool`](https://doc.rust-lang.org/std/primitive.bool.html) to
24/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
25///
26/// If `a` is `false` then returns `Ok(0)`.<br>
27/// If `a` is `true` then returns `Ok(1)`.
28///
29/// # Arguments
30///
31/// * `a` - [`bool`](https://doc.rust-lang.org/std/primitive.bool.html)
32///
33/// # Examples
34///
35/// ```
36/// use rustils::parse::int::bool_to_i32_res;
37///
38/// assert_eq!(bool_to_i32_res(true), Ok(1_i32));
39/// assert_eq!(bool_to_i32_res(false), Ok(0_i32));
40/// ```
41pub fn bool_to_i32_res(a: bool)
42 -> ParseResultI32 {
43
44 if a { Ok(1) } else { Ok(0) }
45}
46
47/// Parse [`bool`](https://doc.rust-lang.org/std/primitive.bool.html) to
48/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
49///
50/// If `a` is `false` then returns 0.<br>
51/// If `a` is `true` then returns 1.
52///
53/// # Arguments
54///
55/// * `a` - [`bool`](https://doc.rust-lang.org/std/primitive.bool.html)
56///
57/// # Examples
58///
59/// ```
60/// use rustils::parse::int::bool_to_i32;
61///
62/// assert_eq!(bool_to_i32(true), 1_i32);
63/// assert_eq!(bool_to_i32(false), 0_i32);
64/// ```
65pub fn bool_to_i32(a: bool)
66 -> i32 {
67
68 if a { 1 } else { 0 }
69}
70
71/// Parse [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) to
72/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
73///
74/// # Arguments
75///
76/// * `a` - Any [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) number
77///
78/// # Examples
79///
80/// ```
81/// use rustils::parse::int::u32_to_i32_res;
82/// use rustils::error::ParseError::InvalidNumber;
83///
84/// assert_eq!(u32_to_i32_res(0_u32), Ok(0_i32));
85/// assert_eq!(u32_to_i32_res(2147483647_u32), Ok(2147483647_i32));
86/// assert_eq!(u32_to_i32_res(2147483648_u32), Err(InvalidNumber(String::from("2147483648"))));
87/// ```
88pub fn u32_to_i32_res(a: u32)
89 -> ParseResultI32 {
90
91 let max = i32::max_value() as u32;
92
93 if a > max {
94 Err(ParseError::InvalidNumber(a.to_string()))
95 } else { Ok(a as i32) }
96}
97
98/// Parse [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) to
99/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
100///
101/// # Panics
102///
103/// ```rust,should_panic
104/// rustils::parse::int::u32_to_i32(2147483648_u32);
105/// ```
106///
107/// # Arguments
108///
109/// * `a` - Any [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) number
110///
111/// # Examples
112///
113/// ```
114/// use rustils::parse::int::u32_to_i32;
115///
116/// assert_eq!(u32_to_i32(0_u32), 0_i32);
117/// assert_eq!(u32_to_i32(2147483647_u32), 2147483647_i32);
118/// ```
119pub fn u32_to_i32(a: u32)
120 -> i32 {
121
122 match u32_to_i32_res(a) {
123 Ok(i) => i,
124 Err(err) => panic!("{}",err)
125 }
126}
127
128pub fn f32_to_i32_res(a: f32)
129 -> ParseResultI32 {
130
131 f32_to_i32_rm_res(a, Trunc)
132}
133
134pub fn f32_to_i32(a: f32)
135 -> i32 {
136
137 f32_to_i32_rm(a, Trunc)
138}
139
140pub fn f32_to_i32_rm_res(a: f32, rm: RoundingMode)
141 -> ParseResultI32 {
142
143 let min = -16777215_f32;
144 let max = 16777215_f32;
145
146 let x = match rm {
147 Round => a.round(),
148 Ceil => a.ceil(),
149 Floor => a.floor(),
150 Trunc => a.trunc()
151 };
152
153 if x.is_nan() || x < min || x > max {
154 Err(ParseError::InvalidNumber(a.to_string()))
155 } else { Ok(x as i32) }
156}
157
158pub fn f32_to_i32_rm(a: f32, rm: RoundingMode)
159 -> i32 {
160
161 match f32_to_i32_rm_res(a, rm) {
162 Ok(i) => i,
163 Err(err) => panic!("{}",err)
164 }
165}
166
167pub fn i64_to_i32_res(a: i64)
168 -> ParseResultI32 {
169
170 let min = i32::min_value() as i64;
171 let max = i32::max_value() as i64;
172
173 if a < min || a > max {
174 Err(ParseError::InvalidNumber(a.to_string()))
175 } else { Ok(a as i32) }
176}
177
178pub fn i64_to_i32(a: i64)
179 -> i32 {
180
181 match i64_to_i32_res(a) {
182 Ok(i) => i,
183 Err(err) => panic!("{}",err)
184 }
185}
186
187/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
188/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
189///
190/// # Arguments
191///
192/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
193///
194/// # Examples
195///
196/// ```
197/// use rustils::parse::int::u64_to_i32_res;
198/// use rustils::error::ParseError::InvalidNumber;
199///
200/// assert_eq!(u64_to_i32_res(0_u64), Ok(0_i32));
201/// assert_eq!(u64_to_i32_res(2147483647_u64), Ok(2147483647_i32));
202/// assert_eq!(u64_to_i32_res(2147483648_u64), Err(InvalidNumber(String::from("2147483648"))));
203/// ```
204pub fn u64_to_i32_res(a: u64)
205 -> ParseResultI32 {
206
207 let max = i32::max_value() as u64;
208
209 if a > max {
210 Err(ParseError::InvalidNumber(a.to_string()))
211 } else { Ok(a as i32) }
212}
213
214/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
215/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
216///
217/// # Panics
218///
219/// ```rust,should_panic
220/// rustils::parse::int::u64_to_i32(2147483648_u64);
221/// ```
222///
223/// # Arguments
224///
225/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
226///
227/// # Examples
228///
229/// ```
230/// use rustils::parse::int::u64_to_i32;
231///
232/// assert_eq!(u64_to_i32(0_u64), 0_i32);
233/// assert_eq!(u64_to_i32(2147483647_u64), 2147483647_i32);
234/// ```
235pub fn u64_to_i32(a: u64)
236 -> i32 {
237
238 match u64_to_i32_res(a) {
239 Ok(i) => i,
240 Err(err) => panic!("{}",err)
241 }
242}
243
244pub fn f64_to_i32_res(a: f64)
245 -> ParseResultI32 {
246
247 f64_to_i32_rm_res(a, Trunc)
248}
249
250pub fn f64_to_i32(a: f64)
251 -> i32 {
252
253 f64_to_i32_rm(a, Trunc)
254}
255
256pub fn f64_to_i32_rm_res(a: f64, rm: RoundingMode)
257 -> ParseResultI32 {
258
259 let min = i32::min_value() as f64;
260 let max = i32::max_value() as f64;
261
262 let x = match rm {
263 Round => a.round(),
264 Ceil => a.ceil(),
265 Floor => a.floor(),
266 Trunc => a.trunc()
267 };
268
269 if x.is_nan() || x < min || x > max {
270 Err(ParseError::InvalidNumber(a.to_string()))
271 } else { Ok(x as i32) }
272}
273
274pub fn f64_to_i32_rm(a: f64, rm: RoundingMode)
275 -> i32 {
276
277 match f64_to_i32_rm_res(a, rm) {
278 Ok(i) => i,
279 Err(err) => panic!("{}",err)
280 }
281}
282
283pub fn isize_to_i32_res(a: isize)
284 -> ParseResultI32 {
285
286 let min = i32::min_value() as isize;
287 let max = i32::max_value() as isize;
288
289 if a < min || a > max {
290 Err(ParseError::InvalidNumber(a.to_string()))
291 } else { Ok(a as i32) }
292}
293
294pub fn isize_to_i32(a: isize)
295 -> i32 {
296
297 match isize_to_i32_res(a) {
298 Ok(i) => i,
299 Err(err) => panic!("{}",err)
300 }
301}
302
303/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
304/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
305///
306/// # Arguments
307///
308/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
309///
310/// # Examples
311///
312/// ```
313/// use rustils::parse::int::usize_to_i32_res;
314/// use rustils::error::ParseError::InvalidNumber;
315///
316/// assert_eq!(usize_to_i32_res(0_usize), Ok(0_i32));
317/// assert_eq!(usize_to_i32_res(2147483647_usize), Ok(2147483647_i32));
318/// assert_eq!(usize_to_i32_res(2147483648_usize), Err(InvalidNumber(String::from("2147483648"))));
319/// ```
320pub fn usize_to_i32_res(a: usize)
321 -> ParseResultI32 {
322
323 let max = i32::max_value() as usize;
324
325 if a > max {
326 Err(ParseError::InvalidNumber(a.to_string()))
327 } else { Ok(a as i32) }
328}
329
330/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
331/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
332///
333/// # Panics
334///
335/// ```rust,should_panic
336/// rustils::parse::int::usize_to_i32(2147483648_usize);
337/// ```
338///
339/// # Arguments
340///
341/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
342///
343/// # Examples
344///
345/// ```
346/// use rustils::parse::int::usize_to_i32;
347///
348/// assert_eq!(usize_to_i32(0_usize), 0_i32);
349/// assert_eq!(usize_to_i32(2147483647_usize), 2147483647_i32);
350/// ```
351pub fn usize_to_i32(a: usize)
352 -> i32 {
353
354 match usize_to_i32_res(a) {
355 Ok(i) => i,
356 Err(err) => panic!("{}",err)
357 }
358}
359
360/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
361/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
362///
363/// # Arguments
364///
365/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
366///
367/// # Examples
368///
369/// ```
370/// use rustils::parse::int::string_to_i32_res;
371/// use rustils::error::ParseError::InvalidNumber;
372///
373/// assert_eq!(string_to_i32_res("-2147483648".to_string()), Ok(-2147483648_i32));
374/// assert_eq!(string_to_i32_res("2147483647".to_string()), Ok(2147483647_i32));
375/// assert_eq!(
376/// string_to_i32_res("-2147483649".to_string()),
377/// Err(InvalidNumber(String::from("-2147483649")))
378/// );
379///
380/// assert_eq!(
381/// string_to_i32_res("2147483648".to_string()),
382/// Err(InvalidNumber(String::from("2147483648")))
383/// );
384/// ```
385pub fn string_to_i32_res(a: String)
386 -> ParseResultI32 {
387
388 match a.parse::<i32>() {
389 Ok(n) => Ok(n),
390 Err(_) => Err(ParseError::InvalidNumber(a))
391 }
392}
393
394/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
395/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
396///
397/// # Panics
398///
399/// ```rust,should_panic
400/// rustils::parse::int::string_to_i32("-2147483649".to_string());
401/// rustils::parse::int::string_to_i32("2147483648".to_string());
402/// ```
403///
404/// # Arguments
405///
406/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
407///
408/// # Examples
409///
410/// ```
411/// use rustils::parse::int::string_to_i32;
412///
413/// assert_eq!(string_to_i32("-2147483648".to_string()), -2147483648_i32);
414/// assert_eq!(string_to_i32("2147483647".to_string()), 2147483647_i32);
415/// ```
416pub fn string_to_i32(a: String)
417 -> i32 {
418
419 match string_to_i32_res(a) {
420 Ok(i) => i,
421 Err(err) => panic!("{}",err)
422 }
423}
424
425/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
426/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
427///
428/// # Arguments
429///
430/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
431///
432/// # Examples
433///
434/// ```
435/// use rustils::parse::int::str_to_i32_res;
436/// use rustils::error::ParseError::InvalidNumber;
437///
438/// assert_eq!(str_to_i32_res("-2147483648"), Ok(-2147483648_i32));
439/// assert_eq!(str_to_i32_res("2147483647"), Ok(2147483647_i32));
440/// assert_eq!(str_to_i32_res("-2147483649"), Err(InvalidNumber(String::from("-2147483649"))));
441/// assert_eq!(str_to_i32_res("2147483648"), Err(InvalidNumber(String::from("2147483648"))));
442/// ```
443pub fn str_to_i32_res(a: &str)
444 -> ParseResultI32 {
445
446 match a.parse::<i32>() {
447 Ok(n) => Ok(n),
448 Err(_) => Err(ParseError::InvalidNumber(a.to_string()))
449 }
450}
451
452/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
453/// [`i32`](https://doc.rust-lang.org/std/primitive.i32.html)
454///
455/// # Panics
456///
457/// ```rust,should_panic
458/// rustils::parse::int::str_to_i32("-2147483649");
459/// rustils::parse::int::str_to_i32("2147483648");
460/// ```
461///
462/// # Arguments
463///
464/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
465///
466/// # Examples
467///
468/// ```
469/// use rustils::parse::int::str_to_i32;
470///
471/// assert_eq!(str_to_i32("-2147483648"), -2147483648_i32);
472/// assert_eq!(str_to_i32("2147483647"), 2147483647_i32);
473/// ```
474pub fn str_to_i32(a: &str)
475 -> i32 {
476
477 match str_to_i32_res(a) {
478 Ok(i) => i,
479 Err(err) => panic!("{}",err)
480 }
481}
482
483pub type ParseResultI32 = Result<i32, ParseError>;