Skip to main content

spire_ai/code/
symbols.rs

1//! Code symbol types.
2
3use serde::{Deserialize, Serialize};
4
5use crate::document::Doc;
6
7/// A chunk of parsed code (function, class, block, etc.)
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CodeChunk {
10    pub id: String,
11    /// The code text
12    pub code: String,
13    /// File path relative to repo root
14    pub file: String,
15    /// Language (rust, python, etc.)
16    pub language: String,
17    /// Symbol type
18    pub kind: SymbolKind,
19    /// Symbol name (if applicable)
20    pub name: Option<String>,
21    /// Start line in original file
22    pub start_line: usize,
23    /// End line in original file
24    pub end_line: usize,
25    /// Parent symbol (e.g., class for a method)
26    pub parent: Option<String>,
27    /// Doc comment / docstring
28    pub docs: Option<String>,
29    /// Function/method signature
30    pub signature: Option<String>,
31}
32
33impl Doc for CodeChunk {
34    fn id(&self) -> &str {
35        &self.id
36    }
37
38    fn embed_text(&self) -> String {
39        let mut text = String::new();
40        if let Some(docs) = &self.docs {
41            text.push_str(docs);
42            text.push_str("\n\n");
43        }
44        if let Some(sig) = &self.signature {
45            text.push_str(sig);
46            text.push_str("\n\n");
47        }
48        text.push_str(&self.code);
49        text
50    }
51}
52
53/// The type of a code symbol.
54#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
55pub enum SymbolKind {
56    Function,
57    Method,
58    Class,
59    Struct,
60    Trait,
61    Interface,
62    Enum,
63    Module,
64    Variable,
65    Constant,
66    Import,
67    Block,
68}
69
70/// A code search hit with score.
71#[derive(Debug, Clone)]
72pub struct CodeHit {
73    pub chunk: CodeChunk,
74    pub score: f32,
75}