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

use std::error::Error;
use std::fmt;
use std::str;

#[derive(Debug)]
struct CustomError(String);

impl fmt::Display for CustomError {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "Stellar Notation Error: {}", self.0)
    }
}

impl Error for CustomError {}

pub fn object(bytes: &Vec<u8>) -> Result<(String, String), Box<dyn Error>> {

    let key_length: usize = u8::from_le_bytes([bytes[0]; 1]) as usize;

    let key_string: String = str::from_utf8(&bytes[1..key_length]).unwrap().to_string();

    let vls: usize = u8::from_le_bytes([bytes[1 + key_length]; 1]) as usize;

    match vls {
        
        1 => {
            let vs = str::from_utf8(&bytes[3 + key_length..bytes.len()])?.to_string();
            Ok((key_string, vs))
        },

        2 => {
            let vs = str::from_utf8(&bytes[4 + key_length..bytes.len()])?.to_string();
            Ok((key_string, vs))
        },

        4 => {
            let vs = str::from_utf8(&bytes[6 + key_length..bytes.len()])?.to_string();
            Ok((key_string, vs))
        },

        8 => {
            let vs = str::from_utf8(&bytes[10 + key_length..bytes.len()])?.to_string();
            Ok((key_string, vs))
        },

        _ => {
            Err(Box::new(CustomError("value length size unknown".into())))
        }

    }

}

pub fn group(bytes: &Vec<u8>) -> Result<Vec<(String, String)>, Box<dyn Error>> {

    let mut objects: Vec<(String, String)> = Vec::new();

    let mut i = 0;

    while i < bytes.len() {

        let key_length: usize = u8::from_le_bytes([bytes[i]; 1]) as usize;
        i += 1;

        let key_string: String = str::from_utf8(&bytes[i..i + key_length]).unwrap().to_string();
        i += key_length;

        let value_length_size: usize = u8::from_le_bytes([bytes[i]; 1]) as usize;
        i += 1;

        match value_length_size {

            1 => {

                let value_length: usize = u8::from_le_bytes([bytes[i]; 1]) as usize;
                i += 1;

                let value_string: String = str::from_utf8(&bytes[i..i + value_length])?.to_string();
                i += value_length;

                objects.push((key_string, value_string))

            },

            2 => {

                let value_length: usize = u16::from_le_bytes([bytes[i], bytes[i + 1]]) as usize;
                i += 2;

                let value_string: String = str::from_utf8(&bytes[i..i + value_length])?.to_string();
                i += value_length;

                objects.push((key_string, value_string))

            },

            4 => {

                let value_length: usize = u32::from_le_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3]]) as usize;
                i += 4;

                let value_string: String = str::from_utf8(&bytes[i..i + value_length])?.to_string();
                i += value_length;

                objects.push((key_string, value_string))

            },

            8 => {

                let value_length: usize = u64::from_le_bytes([bytes[i], bytes[i + 1], bytes[i + 2], bytes[i + 3], bytes[i + 4], bytes[i + 5], bytes[i + 6], bytes[i + 7]]) as usize;
                i += 8;

                let value_string: String = str::from_utf8(&bytes[i..i + value_length])?.to_string();
                i += value_length;

                objects.push((key_string, value_string))

            },

            _ => {
                Err(Box::new(CustomError("value length size unknown".into())))?
            }

        }

    }

    Ok(objects)

}