#[derive(Debug, Clone, PartialEq, Eq)]
pub struct Chunk {
pub content: String,
pub index: usize,
pub start_byte: usize,
pub end_byte: usize,
pub token_count: usize,
pub section: Option<String>,
}
impl Chunk {
#[inline]
pub fn len(&self) -> usize {
self.content.len()
}
#[inline]
pub fn is_empty(&self) -> bool {
self.content.is_empty()
}
}
impl std::fmt::Display for Chunk {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}", self.content)
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chunk_display() {
let c = Chunk {
content: "hello".into(),
index: 0,
start_byte: 0,
end_byte: 5,
token_count: 1,
section: None,
};
assert_eq!(format!("{c}"), "hello");
}
#[test]
fn chunk_len_and_is_empty() {
let c = Chunk {
content: "abc".into(),
index: 0,
start_byte: 0,
end_byte: 3,
token_count: 1,
section: None,
};
assert_eq!(c.len(), 3);
assert!(!c.is_empty());
let empty = Chunk {
content: String::new(),
index: 0,
start_byte: 0,
end_byte: 0,
token_count: 0,
section: None,
};
assert_eq!(empty.len(), 0);
assert!(empty.is_empty());
}
#[test]
fn chunk_clone_and_eq() {
let c = Chunk {
content: "test".into(),
index: 1,
start_byte: 10,
end_byte: 14,
token_count: 1,
section: Some("## Intro".into()),
};
let c2 = c.clone();
assert_eq!(c, c2);
}
#[test]
fn chunk_with_section() {
let c = Chunk {
content: "content".into(),
index: 0,
start_byte: 0,
end_byte: 7,
token_count: 1,
section: Some("# Title".into()),
};
assert_eq!(c.section.as_deref(), Some("# Title"));
}
}