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
use std::fmt;
use std::slice::Iter;
#[cfg(feature = "serde-config")]
use serde::{Deserialize, Serialize};
use crate::error::*;
use crate::serialize::binary::*;
#[cfg_attr(feature = "serde-config", derive(Deserialize, Serialize))]
#[derive(Debug, PartialEq, Eq, Hash, Clone)]
pub struct TXT {
txt_data: Box<[Box<[u8]>]>,
}
impl TXT {
pub fn new(txt_data: Vec<String>) -> Self {
Self {
txt_data: txt_data
.into_iter()
.map(|s| s.into_bytes().into_boxed_slice())
.collect::<Vec<_>>()
.into_boxed_slice(),
}
}
pub fn from_bytes(txt_data: Vec<&[u8]>) -> Self {
Self {
txt_data: txt_data
.into_iter()
.map(|s| s.to_vec().into_boxed_slice())
.collect::<Vec<_>>()
.into_boxed_slice(),
}
}
pub fn txt_data(&self) -> &[Box<[u8]>] {
&self.txt_data
}
pub fn iter(&self) -> Iter<'_, Box<[u8]>> {
self.txt_data.iter()
}
}
pub fn read(decoder: &mut BinDecoder<'_>, rdata_length: Restrict<u16>) -> ProtoResult<TXT> {
let data_len = decoder.len();
let mut strings = Vec::with_capacity(1);
let rdata_length =
rdata_length.map(|u| u as usize).unverified();
while data_len - decoder.len() < rdata_length {
let string =
decoder.read_character_data()?.unverified();
strings.push(string.to_vec().into_boxed_slice());
}
Ok(TXT {
txt_data: strings.into_boxed_slice(),
})
}
pub fn emit(encoder: &mut BinEncoder<'_>, txt: &TXT) -> ProtoResult<()> {
for s in txt.txt_data() {
encoder.emit_character_data(s)?;
}
Ok(())
}
impl fmt::Display for TXT {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> Result<(), fmt::Error> {
for txt in self.txt_data.iter() {
f.write_str(&String::from_utf8_lossy(txt))?;
}
Ok(())
}
}
#[cfg(test)]
mod tests {
#![allow(clippy::dbg_macro, clippy::print_stdout)]
use super::*;
#[test]
fn test() {
let rdata = TXT::new(vec!["Test me some".to_string(), "more please".to_string()]);
let mut bytes = Vec::new();
let mut encoder: BinEncoder<'_> = BinEncoder::new(&mut bytes);
assert!(emit(&mut encoder, &rdata).is_ok());
let bytes = encoder.into_bytes();
println!("bytes: {:?}", bytes);
let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata = read(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
#[test]
fn publish_binary_txt_record() {
let bin_data = vec![0, 1, 2, 3, 4, 5, 6, 7, 8];
let rdata = TXT::from_bytes(vec![b"Test me some", &bin_data]);
let mut bytes = Vec::new();
let mut encoder: BinEncoder<'_> = BinEncoder::new(&mut bytes);
assert!(emit(&mut encoder, &rdata).is_ok());
let bytes = encoder.into_bytes();
println!("bytes: {:?}", bytes);
let mut decoder: BinDecoder<'_> = BinDecoder::new(bytes);
let restrict = Restrict::new(bytes.len() as u16);
let read_rdata = read(&mut decoder, restrict).expect("Decoding error");
assert_eq!(rdata, read_rdata);
}
}