Skip to main content

normalize_facts_core/
symbol.rs

1//! Symbol types for code facts.
2
3use serde::{Deserialize, Serialize};
4
5/// Symbol kind classification
6#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
7#[serde(rename_all = "lowercase")]
8pub enum SymbolKind {
9    /// A standalone function or procedure.
10    Function,
11    /// A method belonging to a class, struct, or impl block.
12    Method,
13    /// A class definition (OOP languages).
14    Class,
15    /// A struct definition.
16    Struct,
17    /// An enum definition.
18    Enum,
19    /// A trait definition (Rust) or abstract interface.
20    Trait,
21    /// An interface definition (Java, Go, TypeScript).
22    Interface,
23    /// A module, namespace, or package declaration.
24    Module,
25    /// A type alias or type definition.
26    Type,
27    /// A constant or compile-time value.
28    Constant,
29    /// A variable declaration.
30    Variable,
31    /// A Markdown heading (used to represent document sections as symbols).
32    Heading,
33}
34
35impl SymbolKind {
36    /// Returns the lowercase string representation of this symbol kind.
37    pub fn as_str(&self) -> &'static str {
38        match self {
39            SymbolKind::Function => "function",
40            SymbolKind::Method => "method",
41            SymbolKind::Class => "class",
42            SymbolKind::Struct => "struct",
43            SymbolKind::Enum => "enum",
44            SymbolKind::Trait => "trait",
45            SymbolKind::Interface => "interface",
46            SymbolKind::Module => "module",
47            SymbolKind::Type => "type",
48            SymbolKind::Constant => "constant",
49            SymbolKind::Variable => "variable",
50            SymbolKind::Heading => "heading",
51        }
52    }
53}
54
55/// Symbol visibility
56#[derive(Debug, Clone, Copy, PartialEq, Eq, Default, Serialize, Deserialize)]
57#[serde(rename_all = "lowercase")]
58pub enum Visibility {
59    /// Exported / accessible everywhere (default).
60    #[default]
61    Public,
62    /// Accessible only within the defining scope or file.
63    Private,
64    /// Accessible to the defining type and its subclasses.
65    Protected,
66    /// Accessible within the same package or crate but not externally.
67    Internal,
68}
69
70impl Visibility {
71    /// Returns the lowercase string representation of this visibility level.
72    pub fn as_str(&self) -> &'static str {
73        match self {
74            Visibility::Public => "public",
75            Visibility::Private => "private",
76            Visibility::Protected => "protected",
77            Visibility::Internal => "internal",
78        }
79    }
80}
81
82/// A code symbol extracted from source
83#[derive(Debug, Clone, Serialize, Deserialize)]
84pub struct Symbol {
85    /// The symbol's unqualified name.
86    pub name: String,
87    /// Classification of the symbol (function, class, heading, etc.).
88    pub kind: SymbolKind,
89    /// Full signature string (e.g., `fn foo(x: i32) -> bool`). Empty if not applicable.
90    pub signature: String,
91    /// Documentation comment or docstring attached to this symbol, if present.
92    pub docstring: Option<String>,
93    /// Language-specific decorators, annotations, or attributes (e.g., `#[derive(...)]` in Rust,
94    /// `@decorator` in Python). Each entry is the raw text of one attribute.
95    pub attributes: Vec<String>,
96    /// 1-based line number where the symbol starts.
97    pub start_line: usize,
98    /// 1-based line number where the symbol ends (inclusive).
99    pub end_line: usize,
100    /// Visibility of the symbol.
101    pub visibility: Visibility,
102    /// Nested symbols (e.g., methods inside a class). Empty for leaf symbols.
103    pub children: Vec<Symbol>,
104    /// True if this symbol implements an interface/trait (e.g., method in `impl Trait for Type`)
105    pub is_interface_impl: bool,
106    /// Parent interfaces/classes this symbol extends or implements (for semantic matching)
107    pub implements: Vec<String>,
108}
109
110/// A flattened symbol for indexing (parent reference instead of nested children)
111#[derive(Debug, Clone, Serialize, Deserialize)]
112pub struct FlatSymbol {
113    /// The symbol's unqualified name.
114    pub name: String,
115    /// Classification of the symbol.
116    pub kind: SymbolKind,
117    /// 1-based line number where the symbol starts.
118    pub start_line: usize,
119    /// 1-based line number where the symbol ends (inclusive).
120    pub end_line: usize,
121    /// Name of the enclosing symbol (e.g., the class for a method), if any.
122    pub parent: Option<String>,
123    /// Visibility of the symbol.
124    pub visibility: Visibility,
125    /// Language-specific decorators or annotations (raw text, one per entry).
126    pub attributes: Vec<String>,
127    /// True if this symbol implements an interface/trait
128    pub is_interface_impl: bool,
129    /// Parent interfaces/classes this symbol extends or implements
130    pub implements: Vec<String>,
131    /// Documentation comment or docstring attached to this symbol, with markers already
132    /// stripped by the Language trait's `extract_docstring` implementation.
133    pub docstring: Option<String>,
134}