Skip to main content

daimon_core/
document.rs

1//! Document types shared across the Daimon framework.
2
3use std::collections::HashMap;
4
5/// A retrieved document fragment with optional metadata and relevance score.
6#[derive(Debug, Clone)]
7pub struct Document {
8    /// The text content of the document.
9    pub content: String,
10    /// Arbitrary key-value metadata (e.g., source URL, page number, title).
11    pub metadata: HashMap<String, serde_json::Value>,
12    /// Relevance score assigned by the retrieval backend (higher = more relevant).
13    /// `None` if the backend does not provide scores.
14    pub score: Option<f64>,
15}
16
17impl Document {
18    /// Creates a document with only text content.
19    pub fn new(content: impl Into<String>) -> Self {
20        Self {
21            content: content.into(),
22            metadata: HashMap::new(),
23            score: None,
24        }
25    }
26
27    /// Adds a metadata entry.
28    pub fn with_metadata(mut self, key: impl Into<String>, value: serde_json::Value) -> Self {
29        self.metadata.insert(key.into(), value);
30        self
31    }
32
33    /// Sets the relevance score.
34    pub fn with_score(mut self, score: f64) -> Self {
35        self.score = Some(score);
36        self
37    }
38}
39
40/// A document paired with a similarity score from a vector query.
41#[derive(Debug, Clone)]
42pub struct ScoredDocument {
43    pub document: Document,
44    pub score: f64,
45}
46
47impl ScoredDocument {
48    pub fn new(document: Document, score: f64) -> Self {
49        Self { document, score }
50    }
51}
52
53#[cfg(test)]
54mod tests {
55    use super::*;
56
57    #[test]
58    fn test_document_new() {
59        let doc = Document::new("hello world");
60        assert_eq!(doc.content, "hello world");
61        assert!(doc.metadata.is_empty());
62        assert!(doc.score.is_none());
63    }
64
65    #[test]
66    fn test_document_with_metadata_and_score() {
67        let doc = Document::new("text")
68            .with_metadata("source", serde_json::json!("wiki"))
69            .with_score(0.95);
70        assert_eq!(doc.metadata["source"], "wiki");
71        assert_eq!(doc.score, Some(0.95));
72    }
73
74    #[test]
75    fn test_scored_document() {
76        let doc = Document::new("content");
77        let scored = ScoredDocument::new(doc, 0.87);
78        assert_eq!(scored.document.content, "content");
79        assert_eq!(scored.score, 0.87);
80    }
81}