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
use serde::{Deserialize, Serialize};
fn to_string(data: &[u8]) -> String {
snailquote::escape(std::str::from_utf8(data).unwrap_or("")).to_string()
}
#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
pub struct OutputChunk {
pub index: usize,
pub data: Vec<u8>,
}
impl OutputChunk {
pub fn len(&self) -> usize {
self.data.len()
}
pub fn contains(&self, index: usize) -> bool {
index >= self.start() && index < self.end()
}
pub fn is_before(&self, index: usize) -> bool {
self.end() <= index
}
pub fn truncate_before(&mut self, index: usize) {
if index <= self.start() {
return;
}
if index >= self.end() {
self.data.clear();
return;
}
let data_index = index - self.start();
self.data.drain(0..data_index);
self.index = index;
}
pub fn start(&self) -> usize {
self.index
}
pub fn end(&self) -> usize {
self.index + self.data.len()
}
}
impl ToString for OutputChunk {
fn to_string(&self) -> String {
to_string(self.data.as_slice())
}
}
#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
pub struct InputChunk {
pub data: Vec<u8>,
}
impl InputChunk {
pub fn len(&self) -> usize {
self.data.len()
}
}
impl ToString for InputChunk {
fn to_string(&self) -> String {
to_string(self.data.as_slice())
}
}