rustils/parse/
long.rs

1use error::ParseError;
2use RoundingMode;
3use RoundingMode::*;
4
5pub trait ToI64 {
6
7    fn to_i64_res(self)
8        -> ParseResultI64;
9
10    fn to_i64(self)
11        -> i64;
12}
13
14pub trait ToI64RM {
15
16    fn to_i64_rm_res(self, rm: RoundingMode)
17        -> ParseResultI64;
18
19    fn to_i64_rm(self, rm: RoundingMode)
20        -> i64;
21}
22
23/// Parse [`bool`](https://doc.rust-lang.org/std/primitive.bool.html) to
24/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.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::long::bool_to_i64_res;
37///
38/// assert_eq!(bool_to_i64_res(true), Ok(1_i64));
39/// assert_eq!(bool_to_i64_res(false), Ok(0_i64));
40/// ```
41pub fn bool_to_i64_res(a: bool)
42    -> ParseResultI64 {
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/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.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::long::bool_to_i64;
61///
62/// assert_eq!(bool_to_i64(true), 1_i64);
63/// assert_eq!(bool_to_i64(false), 0_i64);
64/// ```
65pub fn bool_to_i64(a: bool)
66    -> i64 {
67
68    if a { 1 } else { 0 }
69}
70
71pub fn f32_to_i64_res(a: f32)
72    -> ParseResultI64 {
73
74    f32_to_i64_rm_res(a, Trunc)
75}
76
77pub fn f32_to_i64(a: f32)
78    -> i64 {
79
80    f32_to_i64_rm(a, Trunc)
81}
82
83pub fn f32_to_i64_rm_res(a: f32, rm: RoundingMode)
84    -> ParseResultI64 {
85
86    let min = -16777215_f32;
87    let max = 16777215_f32;
88
89    let x = match rm {
90        Round => a.round(),
91        Ceil => a.ceil(),
92        Floor => a.floor(),
93        Trunc => a.trunc()
94    };
95
96    if x.is_nan() || x < min || x > max {
97        Err(ParseError::InvalidNumber(a.to_string()))
98    } else { Ok(x as i64) }
99}
100
101pub fn f32_to_i64_rm(a: f32, rm: RoundingMode)
102    -> i64 {
103
104    match a.to_i64_rm_res(rm) {
105        Ok(i) => i,
106        Err(err) => panic!("{}",err)
107    }
108}
109
110/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
111/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
112///
113/// # Arguments
114///
115/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
116///
117/// # Examples
118///
119/// ```
120/// use rustils::parse::long::u64_to_i64_res;
121/// use rustils::error::ParseError::InvalidNumber;
122///
123/// assert_eq!(u64_to_i64_res(0_u64), Ok(0_i64));
124/// assert_eq!(u64_to_i64_res(9223372036854775807_u64), Ok(9223372036854775807_i64));
125/// assert_eq!(
126///     u64_to_i64_res(9223372036854775808_u64),
127///     Err(InvalidNumber(String::from("9223372036854775808")))
128/// );
129/// ```
130pub fn u64_to_i64_res(a: u64)
131    -> ParseResultI64 {
132
133    let max = i64::max_value() as u64;
134
135    if a > max {
136        Err(ParseError::InvalidNumber(a.to_string()))
137    } else { Ok(a as i64) }
138}
139
140/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
141/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
142///
143/// # Panics
144///
145/// ```rust,should_panic
146/// rustils::parse::long::u64_to_i64(9223372036854775808_u64);
147/// ```
148///
149/// # Arguments
150///
151/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
152///
153/// # Examples
154///
155/// ```
156/// use rustils::parse::long::u64_to_i64;
157///
158/// assert_eq!(u64_to_i64(0_u64), 0_i64);
159/// assert_eq!(u64_to_i64(9223372036854775807_u64), 9223372036854775807_i64);
160/// ```
161pub fn u64_to_i64(a: u64)
162    -> i64 {
163
164    match u64_to_i64_res(a) {
165        Ok(i) => i,
166        Err(err) => panic!("{}",err)
167    }
168}
169
170pub fn f64_to_i64_res(a: f64)
171    -> ParseResultI64 {
172
173    f64_to_i64_rm_res(a, Trunc)
174}
175
176pub fn f64_to_i64(a: f64)
177    -> i64 {
178
179    f64_to_i64_rm(a, Trunc)
180}
181
182pub fn f64_to_i64_rm_res(a: f64, rm: RoundingMode)
183    -> ParseResultI64 {
184
185    let min = -9007199254740991_f64;
186    let max = 9007199254740991_f64;
187
188    let x = match rm {
189        Round => a.round(),
190        Ceil => a.ceil(),
191        Floor => a.floor(),
192        Trunc => a.trunc()
193    };
194
195    if x.is_nan() || x < min || x > max {
196        Err(ParseError::InvalidNumber(a.to_string()))
197    } else { Ok(x as i64) }
198}
199
200pub fn f64_to_i64_rm(a: f64, rm: RoundingMode)
201    -> i64 {
202
203    match f64_to_i64_rm_res(a, rm) {
204        Ok(i) => i,
205        Err(err) => panic!("{}",err)
206    }
207}
208
209/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
210/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
211///
212/// # Arguments
213///
214/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
215///
216/// # Examples
217///
218/// ```
219/// use rustils::parse::long::usize_to_i64_res;
220/// use rustils::error::ParseError::InvalidNumber;
221///
222/// assert_eq!(usize_to_i64_res(0_usize), Ok(0_i64));
223/// assert_eq!(usize_to_i64_res(9223372036854775807_usize), Ok(9223372036854775807_i64));
224/// assert_eq!(
225///     usize_to_i64_res(9223372036854775808_usize),
226///     Err(InvalidNumber(String::from("9223372036854775808")))
227/// );
228/// ```
229pub fn usize_to_i64_res(a: usize)
230    -> ParseResultI64 {
231
232    let max = i64::max_value() as usize;
233
234    if a > max {
235        Err(ParseError::InvalidNumber(a.to_string()))
236    } else { Ok(a as i64) }
237}
238
239/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
240/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
241///
242/// # Panics
243///
244/// ```rust,should_panic
245/// rustils::parse::long::usize_to_i64(9223372036854775808_usize);
246/// ```
247///
248/// # Arguments
249///
250/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
251///
252/// # Examples
253///
254/// ```
255/// use rustils::parse::long::usize_to_i64;
256///
257/// assert_eq!(usize_to_i64(0_usize), 0_i64);
258/// assert_eq!(usize_to_i64(9223372036854775807_usize), 9223372036854775807_i64);
259/// ```
260pub fn usize_to_i64(a: usize)
261    -> i64 {
262
263    match usize_to_i64_res(a) {
264        Ok(i) => i,
265        Err(err) => panic!("{}",err)
266    }
267}
268
269/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
270/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
271///
272/// # Arguments
273///
274/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
275///
276/// # Examples
277///
278/// ```
279/// use rustils::parse::long::string_to_i64_res;
280/// use rustils::error::ParseError::InvalidNumber;
281///
282/// assert_eq!(
283///     string_to_i64_res("-9223372036854775808".to_string()),
284///     Ok(-9223372036854775808_i64)
285/// );
286///
287/// assert_eq!(
288///     string_to_i64_res("9223372036854775807".to_string()),
289///     Ok(9223372036854775807_i64)
290/// );
291///
292/// assert_eq!(
293///     string_to_i64_res("-9223372036854775809".to_string()),
294///     Err(InvalidNumber(String::from("-9223372036854775809")))
295/// );
296///
297/// assert_eq!(
298///     string_to_i64_res("9223372036854775808".to_string()),
299///     Err(InvalidNumber(String::from("9223372036854775808")))
300/// );
301/// ```
302pub fn string_to_i64_res(a: String)
303    -> ParseResultI64 {
304
305    match a.parse::<i64>() {
306        Ok(n) => Ok(n),
307        Err(_) => Err(ParseError::InvalidNumber(a))
308    }
309}
310
311/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
312/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
313///
314/// # Panics
315///
316/// ```rust,should_panic
317/// rustils::parse::long::string_to_i64("-9223372036854775809".to_string());
318/// rustils::parse::long::string_to_i64("9223372036854775808".to_string());
319/// ```
320///
321/// # Arguments
322///
323/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
324///
325/// # Examples
326///
327/// ```
328/// use rustils::parse::long::string_to_i64;
329///
330/// assert_eq!(string_to_i64("-9223372036854775808".to_string()), -9223372036854775808_i64);
331/// assert_eq!(string_to_i64("9223372036854775807".to_string()), 9223372036854775807_i64);
332/// ```
333pub fn string_to_i64(a: String)
334    -> i64 {
335
336    match string_to_i64_res(a) {
337        Ok(i) => i,
338        Err(err) => panic!("{}",err)
339    }
340}
341
342/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
343/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
344///
345/// # Arguments
346///
347/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
348///
349/// # Examples
350///
351/// ```
352/// use rustils::parse::long::str_to_i64_res;
353/// use rustils::error::ParseError::InvalidNumber;
354///
355/// assert_eq!(str_to_i64_res("-9223372036854775808"), Ok(-9223372036854775808_i64));
356/// assert_eq!(str_to_i64_res("9223372036854775807"), Ok(9223372036854775807_i64));
357/// assert_eq!(
358///     str_to_i64_res("-9223372036854775809"),
359///     Err(InvalidNumber(String::from("-9223372036854775809")))
360/// );
361///
362/// assert_eq!(
363///     str_to_i64_res("9223372036854775808"),
364///     Err(InvalidNumber(String::from("9223372036854775808")))
365/// );
366/// ```
367pub fn str_to_i64_res(a: &str)
368    -> ParseResultI64 {
369
370    match a.parse::<i64>() {
371        Ok(n) => Ok(n),
372        Err(_) => Err(ParseError::InvalidNumber(a.to_string()))
373    }
374}
375
376/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
377/// [`i64`](https://doc.rust-lang.org/std/primitive.i64.html)
378///
379/// # Panics
380///
381/// ```rust,should_panic
382/// rustils::parse::long::str_to_i64("-9223372036854775809");
383/// rustils::parse::long::str_to_i64("9223372036854775808");
384/// ```
385///
386/// # Arguments
387///
388/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
389///
390/// # Examples
391///
392/// ```
393/// use rustils::parse::long::str_to_i64;
394///
395/// assert_eq!(str_to_i64("-9223372036854775808"), -9223372036854775808_i64);
396/// assert_eq!(str_to_i64("9223372036854775807"), 9223372036854775807_i64);
397/// ```
398pub fn str_to_i64(a: &str)
399    -> i64 {
400
401    match str_to_i64_res(a) {
402        Ok(i) => i,
403        Err(err) => panic!("{}",err)
404    }
405}
406
407pub type ParseResultI64 = Result<i64, ParseError>;