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
use std::fmt;

use serde::{Deserialize, Serialize};

use crate::{
    io::Source,
    parse::{Decode, Encode},
};

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct DynString<T: Numeric> {
    capacity: T,
    raw: String,
}

impl<T> DynString<T>
where
    T: Numeric,
{
    pub fn new(raw: &str) -> Self {
        DynString {
            capacity: T::from_usize(raw.len()),
            raw: raw.to_string(),
        }
    }

    pub fn with_capacity(capacity: T, raw: &str) -> Self {
        if capacity.to_usize() < raw.len() {
            panic!("Content is over capacity!")
        }
        DynString {
            capacity,
            raw: raw.to_string(),
        }
    }

    pub fn capacity(&self) -> T {
        self.capacity
    }

    pub fn content(&self) -> &String {
        &self.raw
    }

    pub fn set_content(&mut self, content: &str) {
        self.raw = content.to_string();
    }
}

impl<T> Encode for DynString<T>
where
    T: Numeric,
{
    fn to_le_vec(&self) -> Vec<u8> {
        let mut vec = Vec::<u8>::new();
        let mut prefix = self.capacity.to_le_vec();
        let content = self.raw.clone().into_bytes();
        let mut container = vec![0; self.capacity.to_usize()];
        for (index, _) in content.iter().enumerate() {
            container[index] = content[index];
        }
        vec.append(&mut prefix);
        vec.append(&mut container);
        vec
    }
}

impl Decode for DynString<u16> {
    fn from_le_vec(source: &mut Source) -> Self {
        let capacity = u16::from_le_vec(source);
        let raw = String::from_utf8_lossy(&source.get_vec(capacity as usize)[..]).to_string();

        DynString::with_capacity(capacity, &raw)
    }
}

impl Decode for DynString<u32> {
    fn from_le_vec(source: &mut Source) -> Self {
        let capacity = u32::from_le_vec(source);
        let raw = String::from_utf8_lossy(&source.get_vec(capacity as usize)[..]).to_string();

        DynString::with_capacity(capacity, &raw)
    }
}

pub trait Numeric: Copy + Encode {
    fn to_usize(&self) -> usize;
    fn from_usize(num: usize) -> Self;
}

impl Numeric for u16 {
    fn to_usize(&self) -> usize {
        *self as usize
    }

    fn from_usize(num: usize) -> Self {
        num as u16
    }
}
impl Numeric for u32 {
    fn to_usize(&self) -> usize {
        *self as usize
    }

    fn from_usize(num: usize) -> Self {
        num as u32
    }
}

#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct C4 {
    raw: String,
}

impl C4 {
    pub fn new(content: &str) -> Self {
        if content.len() > 4 {
            panic!("Out of the fixed capacity!")
        }

        C4 {
            raw: content.to_string(),
        }
    }

    pub fn content(&self) -> &String {
        &self.raw
    }

    pub fn set_content(&mut self, content: &str) {
        if content.len() > 4 {
            panic!("Out of the fixed capacity!")
        }
        self.raw = content.to_string();
    }
}

impl Decode for C4 {
    fn from_le_vec(source: &mut Source) -> Self {
        let raw = String::from_utf8_lossy(&source.get_vec(4_usize)[..]).to_string();

        C4::new(&raw)
    }
}

impl Encode for C4 {
    fn to_le_vec(&self) -> Vec<u8> {
        let content = self.raw.clone().into_bytes();
        let mut container = vec![0; 4];
        for (index, _) in content.iter().enumerate() {
            container[index] = content[index];
        }
        container
    }
}

#[derive(Clone, Serialize, Deserialize)]
pub struct C256 {
    raw: String,
}

impl C256 {
    pub fn new(content: &str) -> Self {
        if content.len() > 256 {
            panic!("Out of the fixed capacity!")
        }

        C256 {
            raw: content.to_string(),
        }
    }

    pub fn content(&self) -> &String {
        &self.raw
    }

    pub fn set_content(&mut self, content: &str) {
        if content.len() > 256 {
            panic!("Out of the fixed capacity!")
        }
        self.raw = content.to_string();
    }
}

impl Decode for C256 {
    fn from_le_vec(source: &mut Source) -> Self {
        let raw = String::from_utf8_lossy(&source.get_vec(256_usize)[..]).to_string();
        C256::new(&raw)
    }
}

impl Encode for C256 {
    fn to_le_vec(&self) -> Vec<u8> {
        let content = self.raw.clone().into_bytes();
        let mut container = vec![0; 256];
        for (index, _) in content.iter().enumerate() {
            container[index] = content[index];
        }
        container
    }
}

impl fmt::Debug for C256 {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.raw.trim_matches(char::from(0)))
    }
}