Skip to main content

aft/
symbols.rs

1use serde::Serialize;
2
3/// The kind of a discovered symbol.
4#[derive(Debug, Clone, PartialEq, Eq, Serialize)]
5#[serde(rename_all = "snake_case")]
6pub enum SymbolKind {
7    Function,
8    Class,
9    Method,
10    Struct,
11    Interface,
12    Enum,
13    TypeAlias,
14    /// Top-level const/let variable declaration
15    Variable,
16    /// Markdown heading (h1, h2, h3, etc.)
17    Heading,
18}
19
20/// Location range within a source file (line/column, 0-indexed internally).
21///
22/// **Serialization**: JSON output is 1-based (matches editor/git conventions).
23/// All internal Rust code uses 0-indexed values. The custom `Serialize` impl
24/// adds +1 to all fields during serialization.
25#[derive(Debug, Clone, PartialEq, Eq)]
26pub struct Range {
27    pub start_line: u32,
28    pub start_col: u32,
29    pub end_line: u32,
30    pub end_col: u32,
31}
32
33impl serde::Serialize for Range {
34    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
35        use serde::ser::SerializeStruct;
36        let mut s = serializer.serialize_struct("Range", 4)?;
37        s.serialize_field("start_line", &(self.start_line + 1))?;
38        s.serialize_field("start_col", &(self.start_col + 1))?;
39        s.serialize_field("end_line", &(self.end_line + 1))?;
40        s.serialize_field("end_col", &(self.end_col + 1))?;
41        s.end()
42    }
43}
44
45/// A symbol discovered in a source file.
46#[derive(Debug, Clone, Serialize)]
47pub struct Symbol {
48    pub name: String,
49    pub kind: SymbolKind,
50    pub range: Range,
51    /// Function/method signature, e.g. `fn foo(x: i32) -> bool`
52    #[serde(skip_serializing_if = "Option::is_none")]
53    pub signature: Option<String>,
54    /// Scope chain from outermost to innermost parent, e.g. `["ClassName"]` for a method.
55    pub scope_chain: Vec<String>,
56    /// Whether this symbol is exported (relevant for TS/JS).
57    pub exported: bool,
58    /// The direct parent symbol name, if any.
59    #[serde(skip_serializing_if = "Option::is_none")]
60    pub parent: Option<String>,
61}
62
63/// A resolved symbol match — a `Symbol` plus the file it was found in.
64#[derive(Debug, Clone, Serialize)]
65pub struct SymbolMatch {
66    pub symbol: Symbol,
67    pub file: String,
68}