use crate::core::search::{Confidence, Hit};
use serde::Serialize;
#[derive(Debug, Clone, Serialize)]
pub struct Citation {
pub source_id: i64,
pub path: String,
pub page: i64,
pub line_start: i64,
pub line_end: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub anchor: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub section: Option<String>,
pub reference: String,
}
#[derive(Debug, Clone, Serialize)]
pub struct Answer {
pub question: String,
pub text: String,
pub citations: Vec<Citation>,
pub not_found: bool,
pub engine: String,
}
impl Citation {
pub fn from_hit(hit: &Hit) -> Self {
let mut reference = hit.path.clone();
if let Some(anchor) = &hit.anchor {
reference.push_str(&format!(" [{anchor}]"));
}
if hit.line_start > 0 {
reference.push_str(&format!(":{}-{}", hit.line_start, hit.line_end));
} else if hit.page > 0 {
reference.push_str(&format!(":p.{}", hit.page));
}
Self {
source_id: hit.source_id,
path: hit.path.clone(),
page: hit.page,
line_start: hit.line_start,
line_end: hit.line_end,
anchor: hit.anchor.clone(),
section: hit.section.clone(),
reference,
}
}
}
impl Answer {
pub fn from_hits(question: &str, hits: &[Hit]) -> Self {
let supported: Vec<&Hit> = hits
.iter()
.filter(|hit| !matches!(hit.confidence, Confidence::Low))
.collect();
match supported.first() {
None => Self {
question: question.to_string(),
text: String::new(),
citations: vec![],
not_found: true,
engine: "local".to_string(),
},
Some(top) => Self {
question: question.to_string(),
text: top.snippet.clone(),
citations: supported
.iter()
.take(3)
.map(|h| Citation::from_hit(h))
.collect(),
not_found: false,
engine: "local".to_string(),
},
}
}
pub fn render(&self) -> String {
if self.not_found {
return "No matching document found in the local index.".to_string();
}
let mut out = self.text.clone();
out.push_str("\n\nSources:\n");
for citation in &self.citations {
out.push_str(&format!(" - {}\n", citation.reference));
}
out
}
}