Skip to main content

kbolt_types/
document.rs

1use serde::{Deserialize, Serialize};
2
3#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
4pub enum Locator {
5    Path(String),
6    DocId(String),
7}
8
9impl Locator {
10    pub fn parse(raw: &str) -> Self {
11        let trimmed = raw.trim();
12        if trimmed.contains('/') {
13            return Self::Path(trimmed.to_string());
14        }
15
16        Self::DocId(trimmed.trim_start_matches('#').to_string())
17    }
18}
19
20#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
21pub struct GetRequest {
22    pub locator: Locator,
23    pub space: Option<String>,
24    pub offset: Option<usize>,
25    pub limit: Option<usize>,
26}
27
28#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
29pub struct DocumentResponse {
30    pub docid: String,
31    pub path: String,
32    pub title: String,
33    pub space: String,
34    pub collection: String,
35    pub content: String,
36    pub stale: bool,
37    pub total_lines: usize,
38    pub returned_lines: usize,
39}
40
41#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
42pub struct MultiGetRequest {
43    pub locators: Vec<Locator>,
44    pub space: Option<String>,
45    pub max_files: usize,
46    pub max_bytes: usize,
47}
48
49#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
50pub struct MultiGetResponse {
51    pub documents: Vec<DocumentResponse>,
52    pub omitted: Vec<OmittedFile>,
53    pub resolved_count: usize,
54    pub warnings: Vec<String>,
55}
56
57#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
58pub struct OmittedFile {
59    pub path: String,
60    pub docid: String,
61    pub size_bytes: usize,
62    pub reason: OmitReason,
63}
64
65#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
66pub enum OmitReason {
67    MaxFiles,
68    MaxBytes,
69}
70
71#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
72pub struct FileEntry {
73    pub path: String,
74    pub title: String,
75    pub docid: String,
76    pub active: bool,
77    pub chunk_count: usize,
78    pub embedded: bool,
79}
80
81#[cfg(test)]
82mod tests {
83    use super::Locator;
84
85    #[test]
86    fn parse_preserves_relative_paths() {
87        assert_eq!(
88            Locator::parse(" api/src/lib.rs "),
89            Locator::Path("api/src/lib.rs".to_string())
90        );
91    }
92
93    #[test]
94    fn parse_normalizes_docids() {
95        assert_eq!(
96            Locator::parse(" #abc123 "),
97            Locator::DocId("abc123".to_string())
98        );
99    }
100}