1use serde::{Deserialize, Serialize};
4use std::fmt;
5
6#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
8#[serde(rename_all = "kebab-case")]
9pub enum SymbolKind {
10 Function,
12 Method,
14 Class,
16 Struct,
18 Enum,
20 Trait,
22 Interface,
24 Module,
26 Type,
28 Value,
30 Import,
32 Package,
34 Workspace,
36 Dependency,
38 Unknown,
40}
41
42impl fmt::Display for SymbolKind {
43 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
44 formatter.write_str(match self {
45 Self::Function => "function",
46 Self::Method => "method",
47 Self::Class => "class",
48 Self::Struct => "struct",
49 Self::Enum => "enum",
50 Self::Trait => "trait",
51 Self::Interface => "interface",
52 Self::Module => "module",
53 Self::Type => "type",
54 Self::Value => "value",
55 Self::Import => "import",
56 Self::Package => "package",
57 Self::Workspace => "workspace",
58 Self::Dependency => "dependency",
59 Self::Unknown => "unknown",
60 })
61 }
62}
63
64impl SymbolKind {
65 #[must_use]
67 pub fn from_db(value: &str) -> Self {
68 match value {
69 "function" => Self::Function,
70 "method" => Self::Method,
71 "class" => Self::Class,
72 "struct" => Self::Struct,
73 "enum" => Self::Enum,
74 "trait" => Self::Trait,
75 "interface" => Self::Interface,
76 "module" => Self::Module,
77 "type" => Self::Type,
78 "value" => Self::Value,
79 "import" => Self::Import,
80 "package" => Self::Package,
81 "workspace" => Self::Workspace,
82 "dependency" => Self::Dependency,
83 _ => Self::Unknown,
84 }
85 }
86}
87
88#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
90#[serde(rename_all = "kebab-case")]
91pub enum RelationKind {
92 Contains,
94 Imports,
96 Calls,
98 DependsOn,
100}
101
102impl fmt::Display for RelationKind {
103 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
104 formatter.write_str(match self {
105 Self::Contains => "contains",
106 Self::Imports => "imports",
107 Self::Calls => "calls",
108 Self::DependsOn => "depends-on",
109 })
110 }
111}
112
113impl RelationKind {
114 #[must_use]
116 pub fn from_db(value: &str) -> Option<Self> {
117 match value {
118 "contains" => Some(Self::Contains),
119 "imports" => Some(Self::Imports),
120 "calls" => Some(Self::Calls),
121 "depends-on" => Some(Self::DependsOn),
122 _ => None,
123 }
124 }
125}
126
127#[derive(Clone, Copy, Debug, Deserialize, Eq, PartialEq, Serialize)]
129#[serde(rename_all = "kebab-case")]
130pub enum ParserKind {
131 TreeSitter,
133 Manifest,
135 Structural,
137 Fallback,
139}
140
141impl fmt::Display for ParserKind {
142 fn fmt(&self, formatter: &mut fmt::Formatter<'_>) -> fmt::Result {
143 formatter.write_str(match self {
144 Self::TreeSitter => "tree-sitter",
145 Self::Manifest => "manifest",
146 Self::Structural => "structural",
147 Self::Fallback => "fallback",
148 })
149 }
150}
151
152impl ParserKind {
153 #[must_use]
155 pub fn from_db(value: &str) -> Self {
156 match value {
157 "tree-sitter" => Self::TreeSitter,
158 "manifest" => Self::Manifest,
159 "structural" => Self::Structural,
160 _ => Self::Fallback,
161 }
162 }
163}
164
165#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
167pub struct CodeSymbol {
168 pub path: String,
170 pub language: Option<String>,
172 pub name: String,
174 pub kind: SymbolKind,
176 pub signature: String,
178 pub exported: bool,
180 pub documentation: Option<String>,
182 pub line_start: usize,
184 pub line_end: usize,
186 pub parent: Option<String>,
188 pub parser: ParserKind,
190 pub detail: Option<String>,
192}
193
194#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
196pub struct SymbolRelation {
197 pub path: String,
199 pub source_name: String,
201 pub target_name: String,
203 pub kind: RelationKind,
205 pub line: usize,
207 pub context: String,
209 pub parser: ParserKind,
211}
212
213#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
215pub struct SymbolGraph {
216 pub path: String,
218 pub language: Option<String>,
220 pub parser: ParserKind,
222 pub symbols: Vec<CodeSymbol>,
224 pub relations: Vec<SymbolRelation>,
226}
227
228#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
230pub struct SourceParseMetadata {
231 pub path: String,
233 pub language: Option<String>,
235 pub parser: ParserKind,
237 pub symbol_count: usize,
239 pub relation_count: usize,
241}
242
243impl SourceParseMetadata {
244 #[must_use]
246 pub fn from_graph(graph: &SymbolGraph) -> Self {
247 Self {
248 path: graph.path.clone(),
249 language: graph.language.clone(),
250 parser: graph.parser,
251 symbol_count: graph.symbols.len(),
252 relation_count: graph.relations.len(),
253 }
254 }
255}