1use serde::{Deserialize, Serialize};
2use std::collections::HashMap;
3use uuid::Uuid;
4use chrono::{DateTime, Utc};
5
6#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
8pub enum EntityType {
9 Function,
10 Class,
11 Module,
12 Variable,
13 Import,
14 Export,
15 Interface,
16 Type,
17 Constant,
18}
19
20impl EntityType {
21 pub fn as_str(&self) -> &'static str {
22 match self {
23 EntityType::Function => "function",
24 EntityType::Class => "class",
25 EntityType::Module => "module",
26 EntityType::Variable => "variable",
27 EntityType::Import => "import",
28 EntityType::Export => "export",
29 EntityType::Interface => "interface",
30 EntityType::Type => "type",
31 EntityType::Constant => "constant",
32 }
33 }
34
35 pub fn from_str(s: &str) -> Option<Self> {
36 match s {
37 "function" => Some(EntityType::Function),
38 "class" => Some(EntityType::Class),
39 "module" => Some(EntityType::Module),
40 "variable" => Some(EntityType::Variable),
41 "import" => Some(EntityType::Import),
42 "export" => Some(EntityType::Export),
43 "interface" => Some(EntityType::Interface),
44 "type" => Some(EntityType::Type),
45 "constant" => Some(EntityType::Constant),
46 _ => None,
47 }
48 }
49}
50
51#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct CodeEntity {
54 pub id: String,
55 pub name: String,
56 pub entity_type: EntityType,
57 pub file_path: String,
58 pub line_start: u32,
59 pub line_end: u32,
60 pub column_start: u32,
61 pub column_end: u32,
62 pub metadata: HashMap<String, String>,
63 pub created_at: DateTime<Utc>,
64 pub updated_at: DateTime<Utc>,
65}
66
67impl CodeEntity {
68 pub fn new(
69 name: String,
70 entity_type: EntityType,
71 file_path: String,
72 line_start: u32,
73 line_end: u32,
74 column_start: u32,
75 column_end: u32,
76 ) -> Self {
77 let now = Utc::now();
78 Self {
79 id: Uuid::new_v4().to_string(),
80 name,
81 entity_type,
82 file_path,
83 line_start,
84 line_end,
85 column_start,
86 column_end,
87 metadata: HashMap::new(),
88 created_at: now,
89 updated_at: now,
90 }
91 }
92
93 #[allow(dead_code)]
94 pub fn with_metadata(mut self, key: String, value: String) -> Self {
95 self.metadata.insert(key, value);
96 self
97 }
98
99 #[allow(dead_code)]
100 pub fn update_position(&mut self, line_start: u32, line_end: u32, column_start: u32, column_end: u32) {
101 self.line_start = line_start;
102 self.line_end = line_end;
103 self.column_start = column_start;
104 self.column_end = column_end;
105 self.updated_at = Utc::now();
106 }
107
108 #[allow(dead_code)]
109 pub fn get_signature(&self) -> String {
110 format!("{}:{}:{}:{}", self.file_path, self.entity_type.as_str(), self.name, self.line_start)
111 }
112}
113
114#[cfg(test)]
115mod tests {
116 use super::*;
117
118 #[test]
119 fn test_entity_creation() {
120 let entity = CodeEntity::new(
121 "test_function".to_string(),
122 EntityType::Function,
123 "src/main.rs".to_string(),
124 10,
125 20,
126 0,
127 10,
128 );
129
130 assert_eq!(entity.name, "test_function");
131 assert_eq!(entity.entity_type, EntityType::Function);
132 assert_eq!(entity.file_path, "src/main.rs");
133 assert_eq!(entity.line_start, 10);
134 assert_eq!(entity.line_end, 20);
135 }
136
137 #[test]
138 fn test_entity_type_conversion() {
139 assert_eq!(EntityType::Function.as_str(), "function");
140 assert_eq!(EntityType::from_str("function"), Some(EntityType::Function));
141 assert_eq!(EntityType::from_str("invalid"), None);
142 }
143}