normalize_facts_core/import.rs
1//! Import and export types for code facts.
2
3use serde::{Deserialize, Serialize};
4
5use crate::SymbolKind;
6
7/// An import statement
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct Import {
10 /// The module specifier as written in source (e.g., `"./foo"` or `std::collections`).
11 pub module: String,
12 /// Specific names imported from the module (e.g., `["HashMap", "HashSet"]`).
13 pub names: Vec<String>,
14 /// Local alias for the import (e.g., `import numpy as np` → `"np"`).
15 pub alias: Option<String>,
16 /// True for wildcard imports (`import *` / `use *`).
17 pub is_wildcard: bool,
18 /// True for relative imports (e.g., `./foo`, `../bar`).
19 pub is_relative: bool,
20 /// Source line number where this import appears.
21 pub line: usize,
22}
23
24impl Import {
25 /// Format as a readable summary (module + names)
26 pub fn format_summary(&self) -> String {
27 if self.is_wildcard {
28 format!("{}::*", self.module)
29 } else if self.names.is_empty() {
30 self.module.clone()
31 } else if self.names.len() == 1 {
32 format!("{}::{}", self.module, self.names[0])
33 } else {
34 format!("{}::{{{}}}", self.module, self.names.join(", "))
35 }
36 }
37}
38
39/// An export declaration
40#[derive(Debug, Clone, Serialize, Deserialize)]
41pub struct Export {
42 /// The exported name as it appears in source.
43 pub name: String,
44 /// The symbol kind being exported (function, class, variable, etc.).
45 pub kind: SymbolKind,
46 /// Source line number where this export appears.
47 pub line: usize,
48}
49
50/// A flattened import for indexing (one entry per imported name)
51#[derive(Debug, Clone, Serialize, Deserialize)]
52pub struct FlatImport {
53 /// The module being imported from (None for "import X")
54 pub module: Option<String>,
55 /// The name being imported
56 pub name: String,
57 /// Alias if present (from X import Y as Z -> alias = Z)
58 pub alias: Option<String>,
59 /// Line number
60 pub line: usize,
61 /// True when this is a re-export that makes the symbol available to other files.
62 /// In Rust: `pub use path::Item;`. In TypeScript/JS: `export { X } from './y'`.
63 /// Re-exports are both an import (bringing the symbol in) and a publication of it.
64 #[serde(default)]
65 pub is_reexport: bool,
66}