Skip to main content

code_repo_wiki/model/
node.rs

1use petgraph::stable_graph::NodeIndex;
2use serde::{Deserialize, Serialize};
3
4/// 节点 ID(映射到 petgraph 的 NodeIndex)
5pub type NodeId = NodeIndex<u32>;
6
7/// 代码实体节点
8#[derive(Debug, Clone, Serialize, Deserialize)]
9pub struct CodeNode {
10    /// 在 petgraph 图中的索引
11    pub id: NodeId,
12    /// 实体类型
13    pub kind: NodeKind,
14    /// 实体名称
15    pub name: String,
16    /// 所属文件路径(相对项目根)
17    pub file_path: Option<String>,
18    /// 源代码行范围 [start, end]
19    pub line_range: Option<(usize, usize)>,
20    /// 文档注释
21    pub doc_comment: Option<String>,
22    /// 函数/类型签名
23    pub signature: Option<String>,
24    /// 可见性修饰符("pub"/"pub(crate)"/"private"/"internal"/"export" 等),
25    /// 由解析器按行级文本提取;缺失(默认可见性)为 None。
26    /// serde(default) 兼容旧版 insights_cache 反序列化。
27    #[serde(default)]
28    pub visibility: Option<String>,
29    /// 模块路径(用 :: 分隔)
30    pub module_path: Vec<String>,
31}
32
33/// 节点类型
34#[derive(Debug, Clone, Serialize, Deserialize, PartialEq, Eq)]
35pub enum NodeKind {
36    /// 项目根
37    Project,
38    /// 模块/目录
39    Module,
40    /// 文件
41    File,
42    /// 结构体
43    Struct,
44    /// 枚举
45    Enum,
46    /// 函数/方法
47    Function,
48    /// Trait
49    Trait,
50    /// Trait 实现
51    Impl,
52    /// 类型别名
53    Type,
54    /// 常量
55    Constant,
56    /// 变量
57    Variable,
58    /// 接口(TypeScript/Go/Java)
59    Interface,
60    /// 类
61    Class,
62    /// 宏
63    Macro,
64}
65
66impl NodeKind {
67    pub fn as_str(&self) -> &'static str {
68        match self {
69            NodeKind::Project => "project",
70            NodeKind::Module => "module",
71            NodeKind::File => "file",
72            NodeKind::Struct => "struct",
73            NodeKind::Enum => "enum",
74            NodeKind::Function => "function",
75            NodeKind::Trait => "trait",
76            NodeKind::Impl => "impl",
77            NodeKind::Type => "type",
78            NodeKind::Constant => "constant",
79            NodeKind::Variable => "variable",
80            NodeKind::Interface => "interface",
81            NodeKind::Class => "class",
82            NodeKind::Macro => "macro",
83        }
84    }
85}