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())
}
}