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
use std::{error::Error, fmt::Display};

use log::{debug, warn};

#[derive(Debug, Clone)]
pub struct Frame {
    content: Vec<u8>,
}

impl Frame {
    pub fn new(content: Vec<u8>) -> Self {
        Self { content }
    }

    pub fn parse_sequence(seq: &[u8], existing: Option<Vec<u8>>) -> (Vec<Frame>, Vec<u8>) {
        let mut parsed = vec![];

        let mut byte_cache: Vec<u8> = vec![];
        let mut temp: Vec<u8> = vec![];
        if let Some(existing) = existing {
            existing.iter().for_each(|u| {
                temp.push(*u);
            });
        }
        for ch in seq {
            if *ch == 0x7e {
                if temp.len() > 0 {
                    // Submit
                    if let Ok(frame) = Self::parse(&temp) {
                        debug!(target: "atc-frame", "Parsed frame: `{}`", frame);
                        parsed.push(frame);
                    } else {
                        warn!(target: "atc-frame", "Unable to parse sequence: `{:?}`", temp);
                    }
                    temp.clear()
                }
            } else if *ch == 0x7d {
                // Put to byte cache
                byte_cache.push(0x7d);
            } else if *ch == 0x5d {
                if let Some(byte) = byte_cache.last() {
                    if *byte == 0x7d {
                        temp.push(0x7d);
                        byte_cache.clear();
                    }
                }
            } else if *ch == 0x5e {
                if let Some(byte) = byte_cache.last() {
                    if *byte == 0x7d {
                        temp.push(0x7e);
                        byte_cache.clear();
                    }
                }
            } else {
                temp.push(*ch);
            }
        }
        if temp.len() > 0 {
            if let Ok(frame) = Frame::parse(&temp) {
                debug!(target: "atc-frame", "Parsed frame: `{}`", frame);
                parsed.push(frame);
                temp.clear();
            }
        }
        (parsed, temp.iter().map(|c| *c as u8).collect::<Vec<u8>>())
    }

    fn parse(temp: &Vec<u8>) -> Result<Frame, Box<dyn Error>> {
        Ok(Frame::new(temp.clone()))
    }
}

impl From<String> for Frame {
    fn from(value: String) -> Self {
        Self {
            content: value.as_bytes().to_vec(),
        }
    }
}

impl From<&str> for Frame {
    fn from(value: &str) -> Self {
        Self {
            content: value.as_bytes().to_vec(),
        }
    }
}

impl Display for Frame {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        write!(
            f,
            "Frame<{}>",
            String::from_utf8(self.content.clone()).unwrap()
        )
    }
}

impl Into<Vec<u8>> for Frame {
    fn into(self) -> Vec<u8> {
        let mut ret: Vec<u8> = vec![0x7e];
        self.content.iter().for_each(|u| {
            if *u == 0x7e {
                ret.push(0x7d);
                ret.push(0x5e);
            } else if *u == 0x7d {
                ret.push(0x7d);
                ret.push(0x5d);
            } else {
                ret.push(*u)
            }
        });

        ret
    }
}

impl Into<String> for Frame {
    fn into(self) -> String {
        String::from_utf8(self.content).unwrap()
    }
}