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
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381

use std::error::Error;

pub fn encode<T, I>(iterable: I) -> Result<Vec<u8>, Box<dyn Error>>
where
    T: IntoBytes,
    I: IntoIterator<Item = T>,
{

    let mut result: Vec<u8> = Vec::new();

    for item in iterable {

        let item_buffer = item.into_bytes();

        if item_buffer.len() > 4294967295 {
            
            result.push(8);
            
            result.extend((item_buffer.len() as u64).to_le_bytes());
        
        } else if item_buffer.len() > 65535 {
            
            result.push(4);
            
            result.extend((item_buffer.len() as u32).to_le_bytes());
        
        } else if item_buffer.len() > 255 {
            
            result.push(2);

            result.extend((item_buffer.len() as u16).to_le_bytes());

        } else if item_buffer.len() > 0 {

            result.push(1);

            result.extend((item_buffer.len() as u8).to_le_bytes());

        } else {

            result.push(0);

        };

        result.extend(item_buffer)

    }

    Ok(result)

}

pub fn decode<'a, T, B>(buffer: &'a B) -> Result<Vec<T>, Box<dyn Error>>
where
    T: TryFromBytes<'a>,
    B: AsRef<[u8]>,
{

    let buffer = buffer.as_ref();

    let mut decoded_data = Vec::new();
    let mut offset = 0;

    while offset < buffer.len() {

        let length_type = buffer[offset];

        offset += 1;

        let item_len: usize = match length_type {
            0 => 0,
            1 => buffer[offset] as usize,
            2 => u16::from_le_bytes([
                buffer[offset],
                buffer[offset + 1],
            ]) as usize,
            4 => u32::from_le_bytes([
                buffer[offset],
                buffer[offset + 1],
                buffer[offset + 2],
                buffer[offset + 3],
            ]) as usize,
            8 => u64::from_le_bytes([
                buffer[offset],
                buffer[offset + 1],
                buffer[offset + 2],
                buffer[offset + 3],
                buffer[offset + 4],
                buffer[offset + 5],
                buffer[offset + 6],
                buffer[offset + 7],
            ]) as usize,
            _ => return Err("Invalid length type".into()),
        };

        offset += length_type as usize;

        if offset + item_len <= buffer.len() {
            let item_data = &buffer[offset..offset + item_len];
            offset += item_len;
            let item = T::try_from_bytes(item_data)?;
            decoded_data.push(item);
        } else {
            return Err("Buffer is too short for item length".into());
        }
    }

    Ok(decoded_data)

}

pub trait IntoBytes {
    fn into_bytes(&self) -> Vec<u8>;
}

macro_rules! impl_into_bytes_from_le_bytes {
    ($type:ty) => {
        impl IntoBytes for $type {
            fn into_bytes(&self) -> Vec<u8> {
                self.to_le_bytes().to_vec()
            }
        }
        
        impl<'a> IntoBytes for &$type {
            fn into_bytes(&self) -> Vec<u8> {
                (*self).to_le_bytes().to_vec()
            }
        }
    };
}

impl_into_bytes_from_le_bytes!(u8);
impl_into_bytes_from_le_bytes!(u16);
impl_into_bytes_from_le_bytes!(u32);
impl_into_bytes_from_le_bytes!(u64);
impl_into_bytes_from_le_bytes!(u128);
impl_into_bytes_from_le_bytes!(i8);
impl_into_bytes_from_le_bytes!(i16);
impl_into_bytes_from_le_bytes!(i32);
impl_into_bytes_from_le_bytes!(i64);
impl_into_bytes_from_le_bytes!(i128);

macro_rules! impl_into_bytes_from_as_bytes {
    ($type:ty) => {
        impl IntoBytes for $type {
            fn into_bytes(&self) -> Vec<u8> {
                self.as_bytes().to_vec()
            }
        }
        
        impl<'a> IntoBytes for &$type {
            fn into_bytes(&self) -> Vec<u8> {
                (*self).as_bytes().to_vec()
            }
        }
    };
}

impl_into_bytes_from_as_bytes!(String);
impl_into_bytes_from_as_bytes!(str);

macro_rules! impl_into_bytes_from_to_vec {
    ($type:ty) => {
        impl IntoBytes for $type {
            fn into_bytes(&self) -> Vec<u8> {
                self.to_vec()
            }
        }
        
        impl<'a> IntoBytes for &$type {
            fn into_bytes(&self) -> Vec<u8> {
                (*self).to_vec()
            }
        }
    };
}

impl_into_bytes_from_to_vec!(Vec<u8>);
impl_into_bytes_from_to_vec!([u8]);

impl IntoBytes for char {
    fn into_bytes(&self) -> Vec<u8> {
        let mut buf = [0; 4];
        self.encode_utf8(&mut buf).as_bytes().to_vec()
    }
}

impl IntoBytes for &char {
    fn into_bytes(&self) -> Vec<u8> {
        let mut buf = [0; 4];
        (*self).encode_utf8(&mut buf).as_bytes().to_vec()
    }
}



pub trait TryFromBytes<'a>: Sized {
    fn try_from_bytes(value: &'a [u8]) -> Result<Self, Box<dyn std::error::Error>>;
}

impl TryFromBytes<'_> for String {
    fn try_from_bytes(value: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
        match String::from_utf8(value.to_vec()) {
            Ok(s) => Ok(s),
            Err(e) => Err(Box::new(e) as Box<dyn std::error::Error>),
        }
    }
}

impl TryFromBytes<'_> for Vec<u8> {
    fn try_from_bytes(value: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
        Ok(value.to_vec())
    }
}

impl<'a> TryFromBytes<'a> for &'a [u8] {
    fn try_from_bytes(value: &'a [u8]) -> Result<&'a [u8], Box<dyn std::error::Error>> {
        Ok(value)
    }
}



macro_rules! impl_try_from_bytes_for_numeric {
    ($type:ty) => {
        impl TryFromBytes<'_> for $type {
            fn try_from_bytes(value: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
                if value.len() != std::mem::size_of::<Self>() {
                    return Err("Invalid byte size".into());
                }
                let mut array = [0u8; std::mem::size_of::<Self>()];
                array.copy_from_slice(&value);
                Ok(Self::from_le_bytes(array))
            }
        }
    };
}

impl_try_from_bytes_for_numeric!(u8);
impl_try_from_bytes_for_numeric!(u16);
impl_try_from_bytes_for_numeric!(u32);
impl_try_from_bytes_for_numeric!(u64);
impl_try_from_bytes_for_numeric!(u128);
impl_try_from_bytes_for_numeric!(i8);
impl_try_from_bytes_for_numeric!(i16);
impl_try_from_bytes_for_numeric!(i32);
impl_try_from_bytes_for_numeric!(i64);
impl_try_from_bytes_for_numeric!(i128);

impl TryFromBytes<'_> for char {
    fn try_from_bytes(value: &[u8]) -> Result<Self, Box<dyn std::error::Error>> {
        let str_slice = std::str::from_utf8(value)
            .map_err(|e| Box::new(e) as Box<dyn std::error::Error>)?;

        let mut char_iter = str_slice.chars();
        if let (Some(ch), None) = (char_iter.next(), char_iter.next()) {
            Ok(ch)
        } else {
            Err("Invalid byte slice for char".into())
        }
    }
}


#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn list_of_one() {

        let list = vec![vec![1, 2, 3]];

        let encoded = encode(&list).unwrap();

        let decoded: Vec<Vec<u8>> = decode(&encoded).unwrap();

        assert_eq!(list, decoded);

    }

    #[test]
    fn three_arrays() {

        let list: Vec<&[u8]> = vec![&[1,2,3], &[4,5,6], &[7,8,9]];

        let encoded = encode(list.clone()).unwrap();

        let exp_encoded = vec![1,3,1,2,3,1,3,4,5,6,1,3,7,8,9];

        assert_eq!(exp_encoded, encoded);

        let decoded: Vec<&[u8]> = decode(&encoded).unwrap();

        assert_eq!(list, decoded);

    }

    #[test]
    fn list_of_none() {

        let list: Vec<u8> = vec![];

        let encoded = encode(&list).unwrap();

        assert_eq!(list, decode(&encoded).unwrap());

    }

    #[test]
    fn list_of_one_empty() {

        let list: Vec<Vec<u8>> = vec![vec![]];

        let encoded = encode(&list).unwrap();

        let decoded: Vec<Vec<u8>> = decode(&encoded).unwrap();

        assert_eq!(list, decoded);

    }

    #[test]
    fn list_of_three_empty() {

        let list: Vec<&[u8]> = vec![&[], &[], &[]];
    
        let encoded = encode(list.clone()).unwrap();

        let decoded: Vec<&[u8]> = decode(&encoded).unwrap();
    
        assert_eq!(list, decoded);

    }

    #[test]
    fn list_of_two_and_one_empty() {

        let list: Vec<&[u8]> = vec![&[1,2,3], &[], &[7,8,9]];

        let encoded = encode(list.clone()).unwrap();

        let decoded: Vec<&[u8]> = decode(&encoded).unwrap();
    
        assert_eq!(list, decoded);

    }

    #[test]
    fn test_char_into_bytes() {
        let value = 'a'; // 'a' in UTF-8 is 97
        assert_eq!(value.into_bytes(), vec![97]);

        let value = 'ñ'; // A multi-byte character
        assert_eq!(value.into_bytes(), vec![195, 177]);
    }

    #[test]
    fn test_char_try_from_bytes_valid() {
        let bytes = [97]; // 'a' in UTF-8
        assert_eq!(char::try_from_bytes(&bytes).unwrap(), 'a');

        let bytes = [195, 177]; // 'ñ' in UTF-8
        assert_eq!(char::try_from_bytes(&bytes).unwrap(), 'ñ');
    }

    #[test]
    fn test_char_try_from_bytes_invalid() {
        let bytes = []; // Empty slice
        assert!(char::try_from_bytes(&bytes).is_err());

        let bytes = [97, 98]; // Represents 'ab', more than one char
        assert!(char::try_from_bytes(&bytes).is_err());

        let bytes = [255]; // Invalid UTF-8
        assert!(char::try_from_bytes(&bytes).is_err());
    }


}