normalize-languages 0.3.1

Tree-sitter language support and dynamic grammar loading
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//! Cross-language FFI binding detection.
//!
//! This module provides trait-based FFI detection for identifying
//! cross-language bindings like PyO3, wasm-bindgen, napi-rs, ctypes, etc.

use std::path::Path;

/// A detected FFI module/crate.
#[derive(Debug, Clone)]
pub struct FfiModule {
    /// Name of the module/crate (e.g., "my-pyo3-lib")
    pub name: String,
    /// Path to the main source file (e.g., "src/lib.rs")
    pub lib_path: String,
    /// The binding type that detected this module
    pub binding_type: &'static str,
    /// Source language
    pub source_lang: &'static str,
    /// Target language
    pub target_lang: &'static str,
}

/// A cross-language reference found in source code.
#[derive(Debug, Clone)]
pub struct CrossRef {
    /// File containing the reference
    pub source_file: String,
    /// Language of the source file
    pub source_lang: &'static str,
    /// Target module/crate being referenced
    pub target_module: String,
    /// Language of the target
    pub target_lang: &'static str,
    /// Type of reference (e.g., "pyo3_import", "ctypes_usage")
    pub ref_type: &'static str,
    /// Line number of the reference
    pub line: usize,
}

/// Trait for FFI binding detection.
///
/// Each binding type (PyO3, wasm-bindgen, etc.) implements this trait
/// to describe how to detect it in a project.
pub trait FfiBinding: Send + Sync {
    /// Unique identifier for this binding type (e.g., "pyo3", "wasm-bindgen")
    fn name(&self) -> &'static str;

    /// Source language for this binding (e.g., "rust")
    fn source_lang(&self) -> &'static str;

    /// Target language for this binding (e.g., "python")
    fn target_lang(&self) -> &'static str;

    /// Check if a build file (e.g., Cargo.toml) indicates this binding is used.
    /// Returns the module name if detected.
    fn detect_in_build_file(&self, path: &Path, content: &str) -> Option<String>;

    /// File extensions that may contain imports of this binding's modules.
    fn consumer_extensions(&self) -> &[&'static str];

    /// Check if an import line references a module from this binding.
    /// `module_name` is the crate/package name (with underscores for Rust).
    fn matches_import(&self, import_module: &str, import_name: &str, known_module: &str) -> bool;
}

// ============================================================================
// Built-in FFI Binding Implementations
// ============================================================================

/// PyO3 - Rust to Python bindings
pub struct PyO3Binding;

impl FfiBinding for PyO3Binding {
    fn name(&self) -> &'static str {
        "pyo3"
    }

    fn source_lang(&self) -> &'static str {
        "rust"
    }

    fn target_lang(&self) -> &'static str {
        "python"
    }

    fn detect_in_build_file(&self, path: &Path, content: &str) -> Option<String> {
        if path.file_name()? != "Cargo.toml" {
            return None;
        }
        if !content.contains("pyo3") && !content.contains("PyO3") {
            return None;
        }
        extract_cargo_crate_name(content)
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["py"]
    }

    fn matches_import(&self, import_module: &str, import_name: &str, known_module: &str) -> bool {
        // PyO3 modules use underscores instead of hyphens
        let module_name = known_module.replace('-', "_");
        import_module == module_name
            || import_module.starts_with(&format!("{}.", module_name))
            || import_name == module_name
    }
}

/// wasm-bindgen - Rust to WebAssembly/JavaScript
pub struct WasmBindgenBinding;

impl FfiBinding for WasmBindgenBinding {
    fn name(&self) -> &'static str {
        "wasm-bindgen"
    }

    fn source_lang(&self) -> &'static str {
        "rust"
    }

    fn target_lang(&self) -> &'static str {
        "javascript"
    }

    fn detect_in_build_file(&self, path: &Path, content: &str) -> Option<String> {
        if path.file_name()? != "Cargo.toml" {
            return None;
        }
        if !content.contains("wasm-bindgen") {
            return None;
        }
        extract_cargo_crate_name(content)
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["js", "ts", "tsx", "mjs"]
    }

    fn matches_import(&self, import_module: &str, import_name: &str, known_module: &str) -> bool {
        let module_name = known_module.replace('-', "_");
        import_module == module_name
            || import_module.starts_with(&format!("{}.", module_name))
            || import_name == module_name
            || import_module.contains(&format!("/{}", module_name))
    }
}

/// napi-rs - Rust to Node.js native modules
pub struct NapiRsBinding;

impl FfiBinding for NapiRsBinding {
    fn name(&self) -> &'static str {
        "napi-rs"
    }

    fn source_lang(&self) -> &'static str {
        "rust"
    }

    fn target_lang(&self) -> &'static str {
        "javascript"
    }

    fn detect_in_build_file(&self, path: &Path, content: &str) -> Option<String> {
        if path.file_name()? != "Cargo.toml" {
            return None;
        }
        if !content.contains("napi") {
            return None;
        }
        extract_cargo_crate_name(content)
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["js", "ts", "tsx", "mjs"]
    }

    fn matches_import(&self, import_module: &str, import_name: &str, known_module: &str) -> bool {
        let module_name = known_module.replace('-', "_");
        import_module == module_name
            || import_module.starts_with(&format!("{}.", module_name))
            || import_name == module_name
    }
}

/// Generic cdylib - Rust C ABI exports
pub struct CdylibBinding;

impl FfiBinding for CdylibBinding {
    fn name(&self) -> &'static str {
        "cdylib"
    }

    fn source_lang(&self) -> &'static str {
        "rust"
    }

    fn target_lang(&self) -> &'static str {
        "c"
    }

    fn detect_in_build_file(&self, path: &Path, content: &str) -> Option<String> {
        if path.file_name()? != "Cargo.toml" {
            return None;
        }
        // Only match cdylib that isn't already caught by pyo3/wasm-bindgen/napi
        if !content.contains("cdylib") {
            return None;
        }
        if content.contains("pyo3") || content.contains("wasm-bindgen") || content.contains("napi")
        {
            return None;
        }
        extract_cargo_crate_name(content)
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["c", "cpp", "h", "hpp", "py"] // Could be called from many languages
    }

    fn matches_import(
        &self,
        _import_module: &str,
        _import_name: &str,
        _known_module: &str,
    ) -> bool {
        // Generic cdylib matching is harder - would need to look for dlopen/LoadLibrary calls
        false
    }
}

/// Python ctypes - Python calling C libraries
pub struct CtypesBinding;

impl FfiBinding for CtypesBinding {
    fn name(&self) -> &'static str {
        "ctypes"
    }

    fn source_lang(&self) -> &'static str {
        "python"
    }

    fn target_lang(&self) -> &'static str {
        "c"
    }

    fn detect_in_build_file(&self, _path: &Path, _content: &str) -> Option<String> {
        // ctypes doesn't have a build file indicator
        None
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["py"]
    }

    fn matches_import(&self, import_module: &str, import_name: &str, _known_module: &str) -> bool {
        import_module == "ctypes" || import_name == "ctypes" || import_name == "CDLL"
    }
}

/// Python cffi - Python calling C libraries
pub struct CffiBinding;

impl FfiBinding for CffiBinding {
    fn name(&self) -> &'static str {
        "cffi"
    }

    fn source_lang(&self) -> &'static str {
        "python"
    }

    fn target_lang(&self) -> &'static str {
        "c"
    }

    fn detect_in_build_file(&self, _path: &Path, _content: &str) -> Option<String> {
        None
    }

    fn consumer_extensions(&self) -> &[&'static str] {
        &["py"]
    }

    fn matches_import(&self, import_module: &str, import_name: &str, _known_module: &str) -> bool {
        import_module == "cffi" || import_name == "cffi" || import_name == "FFI"
    }
}

// ============================================================================
// FFI Detector Registry
// ============================================================================

/// Registry of all FFI binding detectors.
pub struct FfiDetector {
    bindings: Vec<Box<dyn FfiBinding>>,
}

impl Default for FfiDetector {
    fn default() -> Self {
        Self::new()
    }
}

impl FfiDetector {
    /// Create a new detector with all built-in bindings.
    pub fn new() -> Self {
        Self {
            bindings: vec![
                Box::new(PyO3Binding),
                Box::new(WasmBindgenBinding),
                Box::new(NapiRsBinding),
                Box::new(CdylibBinding),
                Box::new(CtypesBinding),
                Box::new(CffiBinding),
            ],
        }
    }

    /// Add a custom binding detector.
    pub fn add_binding(&mut self, binding: Box<dyn FfiBinding>) {
        self.bindings.push(binding);
    }

    /// Get all registered bindings.
    pub fn bindings(&self) -> &[Box<dyn FfiBinding>] {
        &self.bindings
    }

    /// Detect FFI modules from a build file.
    pub fn detect_modules(&self, path: &Path, content: &str) -> Vec<FfiModule> {
        let mut modules = Vec::new();
        let parent = path.parent().unwrap_or(Path::new(""));
        let lib_path = parent.join("src").join("lib.rs");

        for binding in &self.bindings {
            if let Some(name) = binding.detect_in_build_file(path, content) {
                modules.push(FfiModule {
                    name,
                    lib_path: lib_path.to_string_lossy().to_string(),
                    binding_type: binding.name(),
                    source_lang: binding.source_lang(),
                    target_lang: binding.target_lang(),
                });
            }
        }

        modules
    }

    /// Check if an import matches any known FFI module.
    pub fn match_import<'a>(
        &self,
        import_module: &str,
        import_name: &str,
        known_modules: &'a [FfiModule],
    ) -> Option<(&'a FfiModule, &'static str)> {
        for module in known_modules {
            for binding in &self.bindings {
                if binding.name() == module.binding_type
                    && binding.matches_import(import_module, import_name, &module.name)
                {
                    return Some((module, binding.name()));
                }
            }
        }
        None
    }

    /// Check if a file extension can consume FFI modules.
    pub fn is_consumer_extension(&self, ext: &str) -> bool {
        for binding in &self.bindings {
            if binding.consumer_extensions().contains(&ext) {
                return true;
            }
        }
        false
    }
}

// ============================================================================
// Helpers
// ============================================================================

/// Extract crate name from Cargo.toml content.
fn extract_cargo_crate_name(content: &str) -> Option<String> {
    let mut in_package = false;
    for line in content.lines() {
        let trimmed = line.trim();
        if trimmed == "[package]" {
            in_package = true;
            continue;
        }
        if trimmed.starts_with('[') {
            in_package = false;
            continue;
        }
        if in_package
            && trimmed.starts_with("name")
            && let Some(eq_pos) = trimmed.find('=')
        {
            let value = trimmed[eq_pos + 1..].trim();
            let value = value.trim_matches('"').trim_matches('\'');
            return Some(value.to_string());
        }
    }
    None
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_pyo3_detection() {
        let binding = PyO3Binding;
        let content = r#"
[package]
name = "my-lib"
version = "0.1.0"

[dependencies]
pyo3 = "0.20"
"#;
        let result = binding.detect_in_build_file(Path::new("Cargo.toml"), content);
        assert_eq!(result, Some("my-lib".to_string()));
    }

    #[test]
    fn test_pyo3_import_matching() {
        let binding = PyO3Binding;
        assert!(binding.matches_import("my_lib", "", "my-lib"));
        assert!(binding.matches_import("my_lib.submodule", "", "my-lib"));
        assert!(!binding.matches_import("other_lib", "", "my-lib"));
    }

    #[test]
    fn test_detector_registry() {
        let detector = FfiDetector::new();
        assert!(detector.bindings().len() >= 6);

        let content = r#"
[package]
name = "wasm-app"

[dependencies]
wasm-bindgen = "0.2"
"#;
        let modules = detector.detect_modules(Path::new("Cargo.toml"), content);
        assert_eq!(modules.len(), 1);
        assert_eq!(modules[0].name, "wasm-app");
        assert_eq!(modules[0].binding_type, "wasm-bindgen");
    }
}