rustils/parse/
ubyte.rs

1use error::ParseError;
2use RoundingMode;
3use RoundingMode::*;
4
5pub trait ToU8 {
6
7    fn to_u8_res(self)
8        -> ParseResultU8;
9
10    fn to_u8(self)
11        -> u8;
12}
13
14pub trait ToU8RM {
15
16    fn to_u8_rm_res(self, rm: RoundingMode)
17        -> ParseResultU8;
18
19    fn to_u8_rm(self, rm: RoundingMode)
20        -> u8;
21}
22
23/// Parse [`bool`](https://doc.rust-lang.org/std/primitive.bool.html) to
24/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.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::ubyte::bool_to_u8_res;
37///
38/// assert_eq!(bool_to_u8_res(true), Ok(1_u8));
39/// assert_eq!(bool_to_u8_res(false), Ok(0_u8));
40/// ```
41pub fn bool_to_u8_res(a: bool)
42    -> ParseResultU8 {
43
44    if a { Ok(1) } else { Ok(0) }
45}
46
47
48/// Parse [`bool`](https://doc.rust-lang.org/std/primitive.bool.html) to
49/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
50///
51/// If `a` is `false` then returns 0.<br>
52/// If `a` is `true` then returns 1.
53///
54/// # Arguments
55///
56/// * `a` - [`bool`](https://doc.rust-lang.org/std/primitive.bool.html)
57///
58/// # Examples
59///
60/// ```
61/// use rustils::parse::ubyte::bool_to_u8;
62///
63/// assert_eq!(bool_to_u8(true), 1_u8);
64/// assert_eq!(bool_to_u8(false), 0_u8);
65/// ```
66pub fn bool_to_u8(a: bool)
67    -> u8 {
68
69    if a { 1 } else { 0 }
70}
71
72
73/// Parse [`i8`](https://doc.rust-lang.org/std/primitive.i8.html) to
74/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
75///
76/// # Arguments
77///
78/// * `a` - Any [`i8`](https://doc.rust-lang.org/std/primitive.i8.html) number
79///
80/// # Examples
81///
82/// ```
83/// use rustils::parse::ubyte::i8_to_u8_res;
84/// use rustils::error::ParseError::InvalidNumber;
85///
86/// assert_eq!(i8_to_u8_res(0_i8), Ok(0_u8));
87/// assert_eq!(i8_to_u8_res(127_i8), Ok(127_u8));
88/// assert_eq!(i8_to_u8_res(-1_i8), Err(InvalidNumber(String::from("-1"))));
89/// ```
90pub fn i8_to_u8_res(a: i8)
91    -> ParseResultU8 {
92
93    if a < 0 {
94        Err(ParseError::InvalidNumber(a.to_string()))
95    } else { Ok(a as u8) }
96}
97
98
99/// Parse [`i8`](https://doc.rust-lang.org/std/primitive.i8.html) to
100/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
101///
102/// # Panics
103///
104/// ```rust,should_panic
105/// rustils::parse::ubyte::i8_to_u8(-1_i8);
106/// ```
107///
108/// # Arguments
109///
110/// * `a` - Any [`i8`](https://doc.rust-lang.org/std/primitive.i8.html) number
111///
112/// # Examples
113///
114/// ```
115/// use rustils::parse::ubyte::i8_to_u8;
116///
117/// assert_eq!(i8_to_u8(0_i8), 0_u8);
118/// assert_eq!(i8_to_u8(127_i8), 127_u8);
119/// ```
120pub fn i8_to_u8(a: i8)
121    -> u8 {
122
123    match i8_to_u8_res(a) {
124        Ok(i) => i,
125        Err(err) => panic!("{}",err)
126    }
127}
128
129/// Parse [`i16`](https://doc.rust-lang.org/std/primitive.i16.html) to
130/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
131///
132/// # Arguments
133///
134/// * `a` - Any [`i16`](https://doc.rust-lang.org/std/primitive.i16.html) number
135///
136/// # Examples
137///
138/// ```
139/// use rustils::parse::ubyte::i16_to_u8_res;
140/// use rustils::error::ParseError::InvalidNumber;
141///
142/// assert_eq!(i16_to_u8_res(0_i16), Ok(0_u8));
143/// assert_eq!(i16_to_u8_res(255_i16), Ok(255_u8));
144/// assert_eq!(i16_to_u8_res(-1_i16), Err(InvalidNumber(String::from("-1"))));
145/// assert_eq!(i16_to_u8_res(256_i16), Err(InvalidNumber(String::from("256"))));
146/// ```
147pub fn i16_to_u8_res(a: i16)
148    -> ParseResultU8 {
149
150    let max = u8::max_value() as i16;
151
152    if a < 0 || a > max {
153        Err(ParseError::InvalidNumber(a.to_string()))
154    } else { Ok(a as u8) }
155}
156
157/// Parse [`i16`](https://doc.rust-lang.org/std/primitive.i16.html) to
158/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
159///
160/// # Panics
161///
162/// ```rust,should_panic
163/// rustils::parse::ubyte::i16_to_u8(-1_i16);
164/// rustils::parse::ubyte::i16_to_u8(256_i16);
165/// ```
166///
167/// # Arguments
168///
169/// * `a` - Any [`i16`](https://doc.rust-lang.org/std/primitive.i16.html) number
170///
171/// # Examples
172///
173/// ```
174/// use rustils::parse::ubyte::i16_to_u8;
175///
176/// assert_eq!(i16_to_u8(0_i16), 0_u8);
177/// assert_eq!(i16_to_u8(255_i16), 255_u8);
178/// ```
179pub fn i16_to_u8(a: i16)
180    -> u8 {
181
182    match i16_to_u8_res(a) {
183        Ok(i) => i,
184        Err(err) => panic!("{}",err)
185    }
186}
187
188/// Parse [`u16`](https://doc.rust-lang.org/std/primitive.u16.html) to
189/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
190///
191/// # Arguments
192///
193/// * `a` - Any [`u16`](https://doc.rust-lang.org/std/primitive.u16.html) number
194///
195/// # Examples
196///
197/// ```
198/// use rustils::parse::ubyte::u16_to_u8_res;
199/// use rustils::error::ParseError::InvalidNumber;
200///
201/// assert_eq!(u16_to_u8_res(0_u16), Ok(0_u8));
202/// assert_eq!(u16_to_u8_res(255_u16), Ok(255_u8));
203/// assert_eq!(u16_to_u8_res(256_u16), Err(InvalidNumber(String::from("256"))));
204/// ```
205pub fn u16_to_u8_res(a: u16)
206    -> ParseResultU8 {
207
208    let max = u8::max_value() as u16;
209
210    if a > max {
211        Err(ParseError::InvalidNumber(a.to_string()))
212    } else { Ok(a as u8) }
213}
214
215/// Parse [`u16`](https://doc.rust-lang.org/std/primitive.u16.html) to
216/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
217///
218/// # Panics
219///
220/// ```rust,should_panic
221/// rustils::parse::ubyte::u16_to_u8(256_u16);
222/// ```
223///
224/// # Arguments
225///
226/// * `a` - Any [`u16`](https://doc.rust-lang.org/std/primitive.u16.html) number
227///
228/// # Examples
229///
230/// ```
231/// use rustils::parse::ubyte::u16_to_u8;
232///
233/// assert_eq!(u16_to_u8(0_u16), 0_u8);
234/// assert_eq!(u16_to_u8(255_u16), 255_u8);
235/// ```
236pub fn u16_to_u8(a: u16)
237    -> u8 {
238
239    match u16_to_u8_res(a) {
240        Ok(i) => i,
241        Err(err) => panic!("{}",err)
242    }
243}
244
245/// Parse [`i32`](https://doc.rust-lang.org/std/primitive.i32.html) to
246/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
247///
248/// # Arguments
249///
250/// * `a` - Any [`i32`](https://doc.rust-lang.org/std/primitive.i32.html) number
251///
252/// # Examples
253///
254/// ```
255/// use rustils::parse::ubyte::i32_to_u8_res;
256/// use rustils::error::ParseError::InvalidNumber;
257///
258/// assert_eq!(i32_to_u8_res(0_i32), Ok(0_u8));
259/// assert_eq!(i32_to_u8_res(255_i32), Ok(255_u8));
260/// assert_eq!(i32_to_u8_res(-1_i32), Err(InvalidNumber(String::from("-1"))));
261/// assert_eq!(i32_to_u8_res(256_i32), Err(InvalidNumber(String::from("256"))));
262/// ```
263pub fn i32_to_u8_res(a: i32)
264    -> ParseResultU8 {
265
266    let max = u8::max_value() as i32;
267
268    if a < 0 || a > max {
269        Err(ParseError::InvalidNumber(a.to_string()))
270    } else { Ok(a as u8) }
271}
272
273/// Parse [`i32`](https://doc.rust-lang.org/std/primitive.i32.html) to
274/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
275///
276/// # Panics
277///
278/// ```rust,should_panic
279/// rustils::parse::ubyte::i32_to_u8(-1_i32);
280/// rustils::parse::ubyte::i32_to_u8(256_i32);
281/// ```
282///
283/// # Arguments
284///
285/// * `a` - Any [`i32`](https://doc.rust-lang.org/std/primitive.i32.html) number
286///
287/// # Examples
288///
289/// ```
290/// use rustils::parse::ubyte::i32_to_u8;
291///
292/// assert_eq!(i32_to_u8(0_i32), 0_u8);
293/// assert_eq!(i32_to_u8(255_i32), 255_u8);
294/// ```
295pub fn i32_to_u8(a: i32)
296    -> u8 {
297
298    match i32_to_u8_res(a) {
299        Ok(i) => i,
300        Err(err) => panic!("{}",err)
301    }
302}
303
304/// Parse [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) to
305/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
306///
307/// # Arguments
308///
309/// * `a` - Any [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) number
310///
311/// # Examples
312///
313/// ```
314/// use rustils::parse::ubyte::u32_to_u8_res;
315/// use rustils::error::ParseError::InvalidNumber;
316///
317/// assert_eq!(u32_to_u8_res(0_u32), Ok(0_u8));
318/// assert_eq!(u32_to_u8_res(255_u32), Ok(255_u8));
319/// assert_eq!(u32_to_u8_res(256_u32), Err(InvalidNumber(String::from("256"))));
320/// ```
321pub fn u32_to_u8_res(a: u32)
322    -> ParseResultU8 {
323
324    let max = u8::max_value() as u32;
325
326    if a > max {
327        Err(ParseError::InvalidNumber(a.to_string()))
328    } else { Ok(a as u8) }
329}
330
331
332/// Parse [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) to
333/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
334///
335/// # Panics
336///
337/// ```rust,should_panic
338/// rustils::parse::ubyte::u32_to_u8(256_u32);
339/// ```
340///
341/// # Arguments
342///
343/// * `a` - Any [`u32`](https://doc.rust-lang.org/std/primitive.u32.html) number
344///
345/// # Examples
346///
347/// ```
348/// use rustils::parse::ubyte::u32_to_u8;
349///
350/// assert_eq!(u32_to_u8(0_u32), 0_u8);
351/// assert_eq!(u32_to_u8(255_u32), 255_u8);
352/// ```
353pub fn u32_to_u8(a: u32)
354    -> u8 {
355
356    match u32_to_u8_res(a) {
357        Ok(i) => i,
358        Err(err) => panic!("{}",err)
359    }
360}
361
362pub fn f32_to_u8_res(a: f32)
363    -> ParseResultU8{
364
365    f32_to_u8_rm_res(a, Trunc)
366}
367
368pub fn f32_to_u8(a: f32)
369    -> u8 {
370
371    f32_to_u8_rm(a, Trunc)
372}
373
374pub fn f32_to_u8_rm_res(a: f32, rm: RoundingMode)
375    -> ParseResultU8 {
376
377    let max = u8::max_value() as f32;
378
379    let x = match rm {
380        Round => a.round(),
381        Ceil => a.ceil(),
382        Floor => a.floor(),
383        Trunc => a.trunc()
384    };
385
386    if x.is_nan() || x < 0.0 || x > max {
387        Err(ParseError::InvalidNumber(a.to_string()))
388    } else { Ok(x as u8) }
389}
390
391pub fn f32_to_u8_rm(a: f32, rm: RoundingMode)
392    -> u8 {
393
394    match f32_to_u8_rm_res(a, rm) {
395        Ok(i) => i,
396        Err(err) => panic!("{}",err)
397    }
398}
399
400/// Parse [`i64`](https://doc.rust-lang.org/std/primitive.i64.html) to
401/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
402///
403/// # Arguments
404///
405/// * `a` - Any [`i64`](https://doc.rust-lang.org/std/primitive.i64.html) number
406///
407/// # Examples
408///
409/// ```
410/// use rustils::parse::ubyte::i64_to_u8_res;
411/// use rustils::error::ParseError::InvalidNumber;
412///
413/// assert_eq!(i64_to_u8_res(0_i64), Ok(0_u8));
414/// assert_eq!(i64_to_u8_res(255_i64), Ok(255_u8));
415/// assert_eq!(i64_to_u8_res(-1_i64), Err(InvalidNumber(String::from("-1"))));
416/// assert_eq!(i64_to_u8_res(256_i64), Err(InvalidNumber(String::from("256"))));
417/// ```
418pub fn i64_to_u8_res(a: i64)
419    -> ParseResultU8 {
420
421    let max = u8::max_value() as i64;
422
423    if a < 0 || a > max {
424        Err(ParseError::InvalidNumber(a.to_string()))
425    } else { Ok(a as u8) }
426}
427
428/// Parse [`i64`](https://doc.rust-lang.org/std/primitive.i64.html) to
429/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
430///
431/// # Panics
432///
433/// ```rust,should_panic
434/// rustils::parse::ubyte::i64_to_u8(-1_i64);
435/// rustils::parse::ubyte::i64_to_u8(256_i64);
436/// ```
437///
438/// # Arguments
439///
440/// * `a` - Any [`i64`](https://doc.rust-lang.org/std/primitive.i64.html) number
441///
442/// # Examples
443///
444/// ```
445/// use rustils::parse::ubyte::i64_to_u8;
446///
447/// assert_eq!(i64_to_u8(0_i64), 0_u8);
448/// assert_eq!(i64_to_u8(255_i64), 255_u8);
449/// ```
450pub fn i64_to_u8(a: i64)
451    -> u8 {
452
453    match i64_to_u8_res(a) {
454        Ok(i) => i,
455        Err(err) => panic!("{}",err)
456    }
457}
458
459/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
460/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
461///
462/// # Arguments
463///
464/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
465///
466/// # Examples
467///
468/// ```
469/// use rustils::parse::ubyte::u64_to_u8_res;
470/// use rustils::error::ParseError::InvalidNumber;
471///
472/// assert_eq!(u64_to_u8_res(0_u64), Ok(0_u8));
473/// assert_eq!(u64_to_u8_res(255_u64), Ok(255_u8));
474/// assert_eq!(u64_to_u8_res(256_u64), Err(InvalidNumber(String::from("256"))));
475/// ```
476pub fn u64_to_u8_res(a: u64)
477    -> ParseResultU8 {
478
479    let max = u8::max_value() as u64;
480
481    if a > max {
482        Err(ParseError::InvalidNumber(a.to_string()))
483    } else { Ok(a as u8) }
484}
485
486/// Parse [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) to
487/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
488///
489/// # Panics
490///
491/// ```rust,should_panic
492/// rustils::parse::ubyte::u64_to_u8(256_u64);
493/// ```
494///
495/// # Arguments
496///
497/// * `a` - Any [`u64`](https://doc.rust-lang.org/std/primitive.u64.html) number
498///
499/// # Examples
500///
501/// ```
502/// use rustils::parse::ubyte::u64_to_u8;
503///
504/// assert_eq!(u64_to_u8(0_u64), 0_u8);
505/// assert_eq!(u64_to_u8(255_u64), 255_u8);
506/// ```
507pub fn u64_to_u8(a: u64)
508    -> u8 {
509
510    match u64_to_u8_res(a) {
511        Ok(i) => i,
512        Err(err) => panic!("{}",err)
513    }
514}
515
516pub fn f64_to_u8_res(a: f64)
517    -> ParseResultU8{
518
519    f64_to_u8_rm_res(a, Trunc)
520}
521
522pub fn f64_to_u8(a: f64)
523    -> u8 {
524
525    f64_to_u8_rm(a, Trunc)
526}
527
528pub fn f64_to_u8_rm_res(a: f64, rm: RoundingMode)
529    -> ParseResultU8 {
530
531    let max = u8::max_value() as f64;
532
533    let x = match rm {
534        Round => a.round(),
535        Ceil => a.ceil(),
536        Floor => a.floor(),
537        Trunc => a.trunc()
538    };
539
540    if x.is_nan() || x < 0.0 || x > max {
541        Err(ParseError::InvalidNumber(a.to_string()))
542    } else { Ok(x as u8) }
543}
544
545pub fn f64_to_u8_rm(a: f64, rm: RoundingMode)
546    -> u8 {
547
548    match f64_to_u8_rm_res(a, rm) {
549        Ok(i) => i,
550        Err(err) => panic!("{}",err)
551    }
552}
553
554/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
555/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
556///
557/// # Arguments
558///
559/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
560///
561/// # Examples
562///
563/// ```
564/// use rustils::parse::ubyte::usize_to_u8_res;
565/// use rustils::error::ParseError::InvalidNumber;
566///
567/// assert_eq!(usize_to_u8_res(0_usize), Ok(0_u8));
568/// assert_eq!(usize_to_u8_res(255_usize), Ok(255_u8));
569/// assert_eq!(usize_to_u8_res(256_usize), Err(InvalidNumber(String::from("256"))));
570/// ```
571pub fn usize_to_u8_res(a: usize)
572    -> ParseResultU8 {
573
574    let max = u8::max_value() as usize;
575
576    if a > max {
577        Err(ParseError::InvalidNumber(a.to_string()))
578    } else { Ok(a as u8) }
579}
580
581/// Parse [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) to
582/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
583///
584/// # Panics
585///
586/// ```rust,should_panic
587/// rustils::parse::ubyte::usize_to_u8(256_usize);
588/// ```
589///
590/// # Arguments
591///
592/// * `a` - Any [`usize`](https://doc.rust-lang.org/std/primitive.usize.html) number
593///
594/// # Examples
595///
596/// ```
597/// use rustils::parse::ubyte::usize_to_u8;
598///
599/// assert_eq!(usize_to_u8(0_usize), 0_u8);
600/// assert_eq!(usize_to_u8(255_usize), 255_u8);
601/// ```
602pub fn usize_to_u8(a: usize)
603    -> u8 {
604
605    match usize_to_u8_res(a) {
606        Ok(i) => i,
607        Err(err) => panic!("{}",err)
608    }
609}
610
611/// Parse [`isize`](https://doc.rust-lang.org/std/primitive.isize.html) to
612/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
613///
614/// # Arguments
615///
616/// * `a` - Any [`isize`](https://doc.rust-lang.org/std/primitive.isize.html) number
617///
618/// # Examples
619///
620/// ```
621/// use rustils::parse::ubyte::isize_to_u8_res;
622/// use rustils::error::ParseError::InvalidNumber;
623///
624/// assert_eq!(isize_to_u8_res(0_isize), Ok(0_u8));
625/// assert_eq!(isize_to_u8_res(255_isize), Ok(255_u8));
626/// assert_eq!(isize_to_u8_res(-1_isize), Err(InvalidNumber(String::from("-1"))));
627/// assert_eq!(isize_to_u8_res(256_isize), Err(InvalidNumber(String::from("256"))));
628/// ```
629pub fn isize_to_u8_res(a: isize)
630    -> ParseResultU8 {
631
632    let max = u8::max_value() as isize;
633
634    if a < 0 || a > max {
635        Err(ParseError::InvalidNumber(a.to_string()))
636    } else { Ok(a as u8) }
637}
638
639/// Parse [`isize`](https://doc.rust-lang.org/std/primitive.isize.html) to
640/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
641///
642/// # Panics
643///
644/// ```rust,should_panic
645/// rustils::parse::ubyte::isize_to_u8(-1_isize);
646/// rustils::parse::ubyte::isize_to_u8(256_isize);
647/// ```
648///
649/// # Arguments
650///
651/// * `a` - Any [`isize`](https://doc.rust-lang.org/std/primitive.isize.html) number
652///
653/// # Examples
654///
655/// ```
656/// use rustils::parse::ubyte::isize_to_u8;
657///
658/// assert_eq!(isize_to_u8(0_isize), 0_u8);
659/// assert_eq!(isize_to_u8(255_isize), 255_u8);
660/// ```
661pub fn isize_to_u8(a: isize)
662    -> u8 {
663
664    match isize_to_u8_res(a) {
665        Ok(i) => i,
666        Err(err) => panic!("{}",err)
667    }
668}
669
670/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
671/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
672///
673/// # Arguments
674///
675/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
676///
677/// # Examples
678///
679/// ```
680/// use rustils::parse::ubyte::string_to_u8_res;
681/// use rustils::error::ParseError::InvalidNumber;
682///
683/// assert_eq!(string_to_u8_res("0".to_string()), Ok(0_u8));
684/// assert_eq!(string_to_u8_res("255".to_string()), Ok(255_u8));
685/// assert_eq!(string_to_u8_res("-1".to_string()), Err(InvalidNumber(String::from("-1"))));
686/// assert_eq!(string_to_u8_res("256".to_string()), Err(InvalidNumber(String::from("256"))));
687/// ```
688pub fn string_to_u8_res(a: String)
689    -> ParseResultU8 {
690
691    match a.parse::<u8>() {
692        Ok(n) => Ok(n),
693        Err(_) => Err(ParseError::InvalidNumber(a))
694    }
695}
696
697/// Parse [`String`](https://doc.rust-lang.org/std/string/struct.String.html) to
698/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
699///
700/// # Panics
701///
702/// ```rust,should_panic
703/// rustils::parse::ubyte::string_to_u8("-1".to_string());
704/// rustils::parse::ubyte::string_to_u8("256".to_string());
705/// ```
706///
707/// # Arguments
708///
709/// * `a` - Any [`String`](https://doc.rust-lang.org/std/string/struct.String.html)
710///
711/// # Examples
712///
713/// ```
714/// use rustils::parse::ubyte::string_to_u8;
715///
716/// assert_eq!(string_to_u8("0".to_string()), 0_u8);
717/// assert_eq!(string_to_u8("255".to_string()), 255_u8);
718/// ```
719pub fn string_to_u8(a: String)
720    -> u8 {
721
722    match string_to_u8_res(a) {
723        Ok(i) => i,
724        Err(err) => panic!("{}",err)
725    }
726}
727
728/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
729/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
730///
731/// # Arguments
732///
733/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
734///
735/// # Examples
736///
737/// ```
738/// use rustils::parse::ubyte::str_to_u8_res;
739/// use rustils::error::ParseError::InvalidNumber;
740///
741/// assert_eq!(str_to_u8_res("0"), Ok(0_u8));
742/// assert_eq!(str_to_u8_res("255"), Ok(255_u8));
743/// assert_eq!(str_to_u8_res("-1"), Err(InvalidNumber(String::from("-1"))));
744/// assert_eq!(str_to_u8_res("256"), Err(InvalidNumber(String::from("256"))));
745/// ```
746pub fn str_to_u8_res(a: &str)
747    -> ParseResultU8 {
748
749    match a.parse::<u8>() {
750        Ok(n) => Ok(n),
751        Err(_) => Err(ParseError::InvalidNumber(a.to_string()))
752    }
753}
754
755/// Parse [`&str`](https://doc.rust-lang.org/std/primitive.str.html) to
756/// [`u8`](https://doc.rust-lang.org/std/primitive.u8.html)
757///
758/// # Panics
759///
760/// ```rust,should_panic
761/// rustils::parse::ubyte::str_to_u8("-1");
762/// rustils::parse::ubyte::str_to_u8("256");
763/// ```
764///
765/// # Arguments
766///
767/// * `a` - Any [`&str`](https://doc.rust-lang.org/std/primitive.str.html)
768///
769/// # Examples
770///
771/// ```
772/// use rustils::parse::ubyte::str_to_u8;
773///
774/// assert_eq!(str_to_u8("0"), 0_u8);
775/// assert_eq!(str_to_u8("255"), 255_u8);
776/// ```
777pub fn str_to_u8(a: &str)
778    -> u8 {
779
780    match str_to_u8_res(a) {
781        Ok(i) => i,
782        Err(err) => panic!("{}",err)
783    }
784}
785
786pub type ParseResultU8 = Result<u8, ParseError>;