1use serde::{Deserialize, Serialize};
2
3fn to_string(data: &[u8]) -> String {
4 snailquote::escape(std::str::from_utf8(data).unwrap_or("")).to_string()
5}
6
7#[derive(Serialize, Deserialize, Debug, Clone, PartialEq, Eq)]
10pub struct OutputChunk {
11 pub index: usize,
13 pub data: Vec<u8>,
15}
16
17impl OutputChunk {
18 pub fn len(&self) -> usize {
20 self.data.len()
21 }
22
23 pub fn contains(&self, index: usize) -> bool {
25 index >= self.start() && index < self.end()
26 }
27
28 pub fn is_before(&self, index: usize) -> bool {
30 self.end() <= index
31 }
32
33 pub fn truncate_before(&mut self, index: usize) {
35 if index <= self.start() {
36 return;
37 }
38
39 if index >= self.end() {
40 self.data.clear();
41 return;
42 }
43
44 let data_index = index - self.start();
45 self.data.drain(0..data_index);
46 self.index = index;
47 }
48
49 pub fn start(&self) -> usize {
51 self.index
52 }
53
54 pub fn end(&self) -> usize {
56 self.index + self.data.len()
57 }
58}
59
60impl ToString for OutputChunk {
61 fn to_string(&self) -> String {
62 to_string(self.data.as_slice())
63 }
64}
65
66#[derive(Serialize, Deserialize, Debug, Clone, Eq, PartialEq)]
69pub struct InputChunk {
70 pub data: Vec<u8>,
72}
73
74impl InputChunk {
75 pub fn len(&self) -> usize {
77 self.data.len()
78 }
79}
80
81impl ToString for InputChunk {
82 fn to_string(&self) -> String {
83 to_string(self.data.as_slice())
84 }
85}