use serde::{Deserialize, Serialize};
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
pub struct CodeChunk {
pub id: String,
pub file: String,
pub start_line: usize,
pub end_line: usize,
pub content: String,
#[serde(default)]
pub function_name: Option<String>,
#[serde(default)]
pub score: f32,
#[serde(default)]
pub compact_snippet: Option<String>,
#[serde(default)]
pub match_reason: String,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn chunk_minimal_json_round_trips() {
let s = r#"{
"id": "f:1:5",
"file": "f.rs",
"start_line": 1,
"end_line": 5,
"content": "fn f() {}"
}"#;
let c: CodeChunk = serde_json::from_str(s).unwrap();
assert_eq!(c.id, "f:1:5");
assert_eq!(c.content, "fn f() {}");
}
#[test]
fn chunk_tolerates_extra_fields_from_search_daemon() {
let s = r#"{
"id": "f:1:5",
"file": "f.rs",
"start_line": 1,
"end_line": 5,
"content": "fn f() {}",
"chunk_type": "Function",
"calls": ["other"],
"inherits_from": [],
"complexity_score": 3,
"chunk_depth": 0
}"#;
let c: CodeChunk = serde_json::from_str(s).unwrap();
assert_eq!(c.id, "f:1:5");
}
}