codesynapse_core/ts_extract/
racket.rs1use super::{add_contains_edge, add_node_if_missing, make_file_node};
2use crate::error::Result;
3use crate::extract::{make_id, ImportNode, LanguageExtractor};
4use crate::types::{Edge, ExtractionFragment, Node};
5use std::collections::HashMap;
6use std::path::Path;
7
8pub struct TsRacketExtractor;
9
10impl TsRacketExtractor {
11 pub fn extract(source: &[u8], path: &Path) -> Result<ExtractionFragment> {
12 let (file_id, _, file_node) = make_file_node(path);
13 let mut fragment = ExtractionFragment {
14 nodes: vec![file_node],
15 edges: vec![],
16 };
17
18 let content = std::str::from_utf8(source).unwrap_or("");
19 for line in content.lines() {
20 let trimmed = line.trim();
21
22 if let Some(def) = trimmed.strip_prefix("(define ") {
23 let name = def
24 .split_whitespace()
25 .next()
26 .unwrap_or("")
27 .trim_end_matches(')');
28 if !name.is_empty() && !name.starts_with('(') {
29 let label = format!("{}()", name);
30 let id = make_id(&[&file_id, name]);
31 fragment.nodes.push(Node {
32 id: id.clone(),
33 label,
34 file_type: "function".to_string(),
35 source_file: path.to_string_lossy().to_string(),
36 source_location: None,
37 community: None,
38 rationale: None,
39 docstring: None,
40 metadata: HashMap::new(),
41 });
42 add_contains_edge(&mut fragment, &file_id, id, path);
43 }
44 }
45
46 if let Some(require) = trimmed.strip_prefix("(require ") {
47 let rest = require.trim_end_matches(')');
48 for part in rest.split_whitespace() {
49 let cleaned = part.trim().trim_matches('"');
50 if !cleaned.is_empty() && !cleaned.starts_with('(') {
51 let mod_id = make_id(&[&file_id, cleaned]);
52 let mod_node = Node {
53 id: mod_id.clone(),
54 label: cleaned.to_string(),
55 file_type: "module".to_string(),
56 source_file: path.to_string_lossy().to_string(),
57 source_location: None,
58 community: None,
59 rationale: None,
60 docstring: None,
61 metadata: HashMap::new(),
62 };
63 add_node_if_missing(&mut fragment, mod_node);
64 fragment.edges.push(Edge {
65 source: file_id.clone(),
66 target: mod_id,
67 relation: "imports".to_string(),
68 confidence: "EXTRACTED".to_string(),
69 source_file: Some(path.to_string_lossy().to_string()),
70 weight: 1.0,
71 context: None,
72 });
73 }
74 }
75 }
76
77 if let Some(provide) = trimmed.strip_prefix("(provide ") {
78 let rest = provide.trim_end_matches(')');
79 for part in rest.split_whitespace() {
80 let cleaned = part.trim();
81 if !cleaned.is_empty() && !cleaned.starts_with('(') && !cleaned.starts_with('"')
82 {
83 let id = make_id(&[&file_id, cleaned]);
84 fragment.nodes.push(Node {
85 id: id.clone(),
86 label: cleaned.to_string(),
87 file_type: "export".to_string(),
88 source_file: path.to_string_lossy().to_string(),
89 source_location: None,
90 community: None,
91 rationale: None,
92 docstring: None,
93 metadata: HashMap::new(),
94 });
95 add_contains_edge(&mut fragment, &file_id, id, path);
96 }
97 }
98 }
99 }
100
101 Ok(fragment)
102 }
103}
104
105impl LanguageExtractor for TsRacketExtractor {
106 fn file_extensions(&self) -> Vec<&'static str> {
107 vec!["rkt", "scrbl", "ss"]
108 }
109 fn extract(&self, source: &[u8], path: &Path) -> Result<ExtractionFragment> {
110 Self::extract(source, path)
111 }
112 fn resolve_imports(&self, _imports: &[ImportNode]) -> Vec<Edge> {
113 vec![]
114 }
115 fn collect_type_refs(&self, _fragment: &mut ExtractionFragment) {}
116}