ragkit_ai/
chunk.rs

1use crate::{
2  error::Error,
3  loc::Loc,
4};
5use serde::{
6  Deserialize,
7  Serialize,
8};
9use std::collections::HashMap;
10
11pub mod recursive;
12pub mod simple;
13
14pub trait Chunker<'a> {
15  type Input;
16  fn chunk(&self, input: Self::Input) -> Result<Vec<Chunk<'a>>, Error>;
17}
18
19#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
20#[serde(tag = "type", rename_all = "lowercase")]
21pub enum Chunk<'a> {
22  #[serde(borrow)]
23  Simple(SimpleChunk<'a>),
24}
25
26impl<'a> Chunk<'a> {
27  pub fn content(&'a self) -> &'a str {
28    match self {
29      Chunk::Simple(simple) => simple.content,
30    }
31  }
32
33  pub fn loc(&'a self) -> &'a Loc {
34    match self {
35      Chunk::Simple(simple) => &simple.loc,
36    }
37  }
38}
39
40#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
41pub struct SimpleChunk<'a> {
42  pub content: &'a str,
43  pub loc: Loc,
44  pub tags: HashMap<&'a str, Tag<'a>>,
45}
46
47impl<'a> SimpleChunk<'a> {
48  pub fn as_chunk(self) -> Chunk<'a> {
49    Chunk::Simple(self)
50  }
51}
52
53#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)]
54pub struct Tag<'a> {
55  pub key: &'a str,
56  pub value: &'a str,
57  pub loc: Loc,
58}