astro_notation/
decode.rs

1use std::convert::TryInto;
2use std::str;
3
4pub fn as_bool(input: &str) -> bool {
5    match input {
6        "0x00" => false,
7        "0x01" => true,
8        _ => panic!("{} is an unsupported string as bool.", input)
9    }
10}
11
12pub fn as_str(input: &str) -> String {
13    str::from_utf8(&hex(input)).unwrap().to_string()
14}
15
16pub fn as_u8(input: &str) -> u8 {
17    u8::from_be_bytes(hex(input).try_into().unwrap())
18}
19
20pub fn as_u16(input: &str) -> u16 {
21    u16::from_be_bytes(hex(input).try_into().unwrap())
22}
23
24pub fn as_u32(input: &str) -> u32 {
25    u32::from_be_bytes(hex(input).try_into().unwrap())
26}
27
28pub fn as_u64(input: &str) -> u64 {
29    u64::from_be_bytes(hex(input).try_into().unwrap())
30}
31
32pub fn as_u128(input: &str) -> u128 {
33    u128::from_be_bytes(hex(input).try_into().unwrap())
34}
35
36pub fn as_i8(input: &str) -> i8 {
37    i8::from_be_bytes(hex(input).try_into().unwrap())
38}
39
40pub fn as_i16(input: &str) -> i16 {
41    i16::from_be_bytes(hex(input).try_into().unwrap())
42}
43
44pub fn as_i32(input: &str) -> i32 {
45    i32::from_be_bytes(hex(input).try_into().unwrap())
46}
47
48pub fn as_i64(input: &str) -> i64 {
49    i64::from_be_bytes(hex(input).try_into().unwrap())
50}
51
52pub fn as_i128(input: &str) -> i128 {
53    i128::from_be_bytes(hex(input).try_into().unwrap())
54}
55
56pub fn as_bytes(input: &str) -> Vec<u8> {
57    hex(input)
58}
59
60fn hex(input: &str) -> Vec<u8> {
61
62    let mut output: Vec<u8> = Vec::new();
63    
64    (2..input.len())
65        .step_by(2)
66        .for_each(|x| output.push(u8::from_str_radix(&input[x..x + 2], 16).unwrap()));
67
68    output
69
70}