1use std::collections::HashMap;
4
5#[derive(Debug, Clone)]
7pub struct Document {
8 pub content: String,
10 pub metadata: HashMap<String, serde_json::Value>,
12 pub score: Option<f64>,
15}
16
17impl Document {
18 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 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 pub fn with_score(mut self, score: f64) -> Self {
35 self.score = Some(score);
36 self
37 }
38}
39
40#[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}