1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3
4#[derive(Debug, Clone, Serialize, Deserialize)]
5#[serde(rename_all = "camelCase")]
6pub struct SemanticEntity {
7 pub id: String,
8 pub file_path: String,
9 pub entity_type: String,
10 pub name: String,
11 #[serde(skip_serializing_if = "Option::is_none")]
12 pub parent_id: Option<String>,
13 pub content: String,
14 pub content_hash: String,
15 #[serde(skip_serializing_if = "Option::is_none")]
19 pub structural_hash: Option<String>,
20 pub start_line: usize,
21 pub end_line: usize,
22 #[serde(skip_serializing_if = "Option::is_none")]
23 pub metadata: Option<HashMap<String, String>>,
24}
25
26pub fn build_entity_id(
27 file_path: &str,
28 entity_type: &str,
29 name: &str,
30 parent_id: Option<&str>,
31) -> String {
32 match parent_id {
33 Some(pid) => format!("{pid}::{name}"),
34 None => format!("{file_path}::{entity_type}::{name}"),
35 }
36}
37
38pub fn build_entity_id_disambiguated(
40 file_path: &str,
41 entity_type: &str,
42 name: &str,
43 parent_id: Option<&str>,
44 line: usize,
45) -> String {
46 let base = build_entity_id(file_path, entity_type, name, parent_id);
47 format!("{base}@L{line}")
48}
49
50#[cfg(test)]
51mod tests {
52 use super::*;
53
54 #[test]
55 fn test_build_entity_id_no_parent() {
56 assert_eq!(
57 build_entity_id("src/main.ts", "function", "hello", None),
58 "src/main.ts::function::hello"
59 );
60 }
61
62 #[test]
63 fn test_build_entity_id_with_parent() {
64 let id = build_entity_id(
65 "src/main.ts",
66 "method",
67 "greet",
68 Some("src/main.ts::class::MyClass"),
69 );
70 assert_eq!(id, "src/main.ts::class::MyClass::greet");
71 }
72}