@@ -2039,12 +2039,81 @@ def _parse_rust(self, filepath: str, content: str) -> ModuleInfo:
20392039 constants : List [str ] = []
20402040 exports : List [str ] = []
20412041
2042+ def _get_or_create_class (name : str ) -> ClassInfo :
2043+ for c in classes :
2044+ if c .name == name :
2045+ return c
2046+ c = ClassInfo (name = name )
2047+ classes .append (c )
2048+ return c
2049+
2050+ def _find_matching_brace (src : str , start_idx : int ) -> int :
2051+ depth = 0
2052+ in_str = False
2053+ str_ch = ''
2054+ i = start_idx
2055+ while i < len (src ):
2056+ ch = src [i ]
2057+ if in_str :
2058+ if ch == '\\ ' and i + 1 < len (src ):
2059+ i += 2
2060+ continue
2061+ if ch == str_ch :
2062+ in_str = False
2063+ str_ch = ''
2064+ i += 1
2065+ continue
2066+ if ch in ('\" ' , "'" ):
2067+ in_str = True
2068+ str_ch = ch
2069+ i += 1
2070+ continue
2071+ if ch == '{' :
2072+ depth += 1
2073+ elif ch == '}' :
2074+ depth -= 1
2075+ if depth == 0 :
2076+ return i
2077+ i += 1
2078+ return - 1
2079+
2080+ def _parse_rust_fn (match : re .Match , * , is_method : bool ) -> FunctionInfo :
2081+ name = match .group ('name' )
2082+ params_raw = (match .group ('params' ) or '' ).strip ()
2083+ ret = (match .group ('ret' ) or '' ).strip ()
2084+ sig = (match .group (0 ) or '' )
2085+
2086+ params = [p .strip () for p in params_raw .split (',' ) if p .strip ()]
2087+ params = params [:8 ]
2088+
2089+ is_pub = bool (re .search (r'\bpub\b' , sig ))
2090+ return FunctionInfo (
2091+ name = name ,
2092+ params = params ,
2093+ return_type = ret ,
2094+ docstring = None ,
2095+ docstring_full = None ,
2096+ calls = [],
2097+ raises = [],
2098+ decorators = [],
2099+ complexity = 1 ,
2100+ lines = 1 ,
2101+ is_async = False ,
2102+ is_static = False ,
2103+ is_classmethod = False ,
2104+ is_property = False ,
2105+ intent = self .intent_gen .generate (name ),
2106+ start_line = 0 ,
2107+ end_line = 0 ,
2108+ is_private = not is_pub ,
2109+ )
2110+
20422111 for m in re .finditer (r'^use\s+([^;]+);' , content , re .MULTILINE ):
20432112 imports .append (m .group (1 ).strip ())
20442113
20452114 for m in re .finditer (r'^(?:pub\s+)?struct\s+(\w+)' , content , re .MULTILINE ):
20462115 name = m .group (1 )
2047- classes . append ( ClassInfo ( name = name ) )
2116+ _get_or_create_class ( name )
20482117 types .append (TypeInfo (name = name , kind = 'struct' , definition = '' ))
20492118 exports .append (name )
20502119
@@ -2058,35 +2127,64 @@ def _parse_rust(self, filepath: str, content: str) -> ModuleInfo:
20582127 types .append (TypeInfo (name = name , kind = 'trait' , definition = '' ))
20592128 exports .append (name )
20602129
2061- for m in re .finditer (r'^(?:pub\s+)?fn \s+(\w+)\s*\(([^)]*)\)\s*(?:->\s*([^\s{]+))? ' , content , re .MULTILINE ):
2130+ for m in re .finditer (r'^(?:pub\s+)?type \s+(\w+)\s*= ' , content , re .MULTILINE ):
20622131 name = m .group (1 )
2063- params = [p .strip () for p in (m .group (2 ) or '' ).split (',' ) if p .strip ()][:8 ]
2064- ret = (m .group (3 ) or '' ).strip ()
2065- functions .append (FunctionInfo (
2066- name = name ,
2067- params = params ,
2068- return_type = ret ,
2069- docstring = None ,
2070- docstring_full = None ,
2071- calls = [],
2072- raises = [],
2073- decorators = [],
2074- complexity = 1 ,
2075- lines = 1 ,
2076- is_async = 'async' in m .group (0 ),
2077- is_static = False ,
2078- is_classmethod = False ,
2079- is_property = False ,
2080- intent = self .intent_gen .generate (name ),
2081- start_line = 0 ,
2082- end_line = 0 ,
2083- is_private = False ,
2084- ))
2132+ types .append (TypeInfo (name = name , kind = 'type' , definition = '' ))
2133+ exports .append (name )
2134+
2135+ for m in re .finditer (r'^(?:pub\s+)?mod\s+(\w+)\s*;' , content , re .MULTILINE ):
2136+ name = m .group (1 )
2137+ types .append (TypeInfo (name = name , kind = 'module' , definition = '' ))
20852138 exports .append (name )
20862139
2140+ fn_pat = re .compile (
2141+ r'^\s*(?P<sig>(?:pub(?:\([^)]*\))?\s+)?fn)\s+(?P<name>\w+)\s*\((?P<params>[^)]*)\)\s*(?:->\s*(?P<ret>[^\s{]+))?' ,
2142+ re .MULTILINE ,
2143+ )
2144+
2145+ for m in fn_pat .finditer (content ):
2146+ functions .append (_parse_rust_fn (m , is_method = False ))
2147+ exports .append (m .group ('name' ))
2148+
2149+ impl_pat = re .compile (
2150+ r'^impl\b[^\{]*\{' ,
2151+ re .MULTILINE ,
2152+ )
2153+
2154+ for m in impl_pat .finditer (content ):
2155+ hdr = content [m .start ():m .end ()]
2156+ type_name = ''
2157+ m_for = re .search (r'\bfor\s+(\w+)\b' , hdr )
2158+ if m_for :
2159+ type_name = m_for .group (1 )
2160+ else :
2161+ m_type = re .search (r'^impl\b[^\{]*?\b(\w+)\b\s*\{' , hdr )
2162+ if m_type :
2163+ type_name = m_type .group (1 )
2164+
2165+ if not type_name :
2166+ continue
2167+
2168+ open_idx = content .find ('{' , m .start (), m .end ())
2169+ if open_idx < 0 :
2170+ continue
2171+ close_idx = _find_matching_brace (content , open_idx )
2172+ if close_idx < 0 :
2173+ continue
2174+
2175+ body = content [open_idx + 1 :close_idx ]
2176+ cls = _get_or_create_class (type_name )
2177+ for fm in fn_pat .finditer (body ):
2178+ cls .methods .append (_parse_rust_fn (fm , is_method = True ))
2179+
2180+ exports .append (type_name )
2181+
20872182 for m in re .finditer (r'^(?:pub\s+)?const\s+([A-Z][A-Z0-9_]*)\b' , content , re .MULTILINE ):
20882183 constants .append (m .group (1 ))
20892184
2185+ for m in re .finditer (r'^(?:pub\s+)?static\s+(?:mut\s+)?([A-Z][A-Z0-9_]*)\b' , content , re .MULTILINE ):
2186+ constants .append (m .group (1 ))
2187+
20902188 lines = content .split ('\n ' )
20912189 return ModuleInfo (
20922190 path = filepath ,
0 commit comments