Skip to main content

sem_core/model/
entity.rs

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    /// AST-based hash that strips comments and normalizes whitespace.
16    /// Two entities with the same structural_hash are logically identical
17    /// even if formatting/comments differ. Inspired by Unison's content-addressed model.
18    #[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!("{file_path}::{pid}::{name}"),
34        None => format!("{file_path}::{entity_type}::{name}"),
35    }
36}
37
38#[cfg(test)]
39mod tests {
40    use super::*;
41
42    #[test]
43    fn test_build_entity_id_no_parent() {
44        assert_eq!(
45            build_entity_id("src/main.ts", "function", "hello", None),
46            "src/main.ts::function::hello"
47        );
48    }
49
50    #[test]
51    fn test_build_entity_id_with_parent() {
52        let id = build_entity_id("src/main.ts", "method", "greet", Some("MyClass"));
53        assert_eq!(id, "src/main.ts::MyClass::greet");
54    }
55}