deepwiki_rs/react/
context.rs

1//! 项目上下文和探索状态管理
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::path::PathBuf;
6use chrono::{DateTime, Utc};
7use crate::metadata::FileInfo;
8
9/// 项目上下文
10#[derive(Debug, Clone)]
11pub struct ProjectContext {
12    pub project_path: PathBuf,
13    pub discovered_files: HashMap<String, FileInfo>,
14    pub architecture_insights: Vec<ArchitectureInsight>,
15    pub component_relationships: Vec<ComponentRelationship>,
16    pub exploration_history: Vec<ExplorationStep>,
17    pub detected_patterns: Vec<ArchitecturePattern>,
18}
19
20/// 探索状态
21#[derive(Debug, Clone, PartialEq)]
22pub enum ExplorationState {
23    Initial,
24    DiscoveringStructure,
25    AnalyzingComponents,
26    MappingRelationships,
27    GeneratingDocumentation,
28    Completed,
29}
30
31/// 架构洞察
32#[derive(Debug, Clone, Serialize, Deserialize)]
33pub struct ArchitectureInsight {
34    pub insight_type: InsightType,
35    pub description: String,
36    pub confidence: f64,
37    pub evidence: Vec<String>,
38    pub timestamp: DateTime<Utc>,
39}
40
41/// 洞察类型
42#[derive(Debug, Clone, Serialize, Deserialize)]
43pub enum InsightType {
44    ComponentDiscovered,
45    PatternDetected,
46    RelationshipFound,
47    ArchitectureStyle,
48    TechnologyStack,
49}
50
51/// 组件关系
52#[derive(Debug, Clone, Serialize, Deserialize)]
53pub struct ComponentRelationship {
54    pub from: String,
55    pub to: String,
56    pub relationship_type: RelationshipType,
57    pub strength: f64,
58    pub evidence: Vec<String>,
59}
60
61/// 关系类型
62#[derive(Debug, Clone, Serialize, Deserialize)]
63pub enum RelationshipType {
64    Depends,
65    Uses,
66    Implements,
67    Extends,
68    Configures,
69    Calls,
70}
71
72/// 架构模式
73#[derive(Debug, Clone, Serialize, Deserialize, PartialEq)]
74pub enum ArchitecturePattern {
75    MVC,
76    MVP,
77    MVVM,
78    Layered,
79    Microservice,
80    EventDriven,
81    Pipeline,
82    Repository,
83    Factory,
84    Singleton,
85}
86
87/// 探索步骤
88#[derive(Debug, Clone)]
89pub struct ExplorationStep {
90    pub timestamp: DateTime<Utc>,
91    pub action: String,
92    pub state: ExplorationState,
93    pub tools_used: Vec<String>,
94    pub insights_gained: Vec<String>,
95}
96
97/// 项目分析结果
98#[derive(Debug, Clone)]
99pub struct ProjectAnalysis {
100    pub project_path: PathBuf,
101    pub summary: String,
102    pub discovered_components: HashMap<String, FileInfo>,
103    pub architecture_patterns: Vec<ArchitectureInsight>,
104    pub relationships: Vec<ComponentRelationship>,
105    pub exploration_history: Vec<ExplorationStep>,
106    pub c4_documentation: C4Documentation,
107}
108
109/// C4文档结构
110#[derive(Debug, Clone)]
111pub struct C4Documentation {
112    pub system_context: String,
113    pub container_diagram: String,
114    pub component_diagram: String,
115    pub code_diagram: String,
116}
117
118impl ProjectContext {
119    pub fn new(project_path: PathBuf) -> Self {
120        Self {
121            project_path,
122            discovered_files: HashMap::new(),
123            architecture_insights: Vec::new(),
124            component_relationships: Vec::new(),
125            exploration_history: Vec::new(),
126            detected_patterns: Vec::new(),
127        }
128    }
129
130    pub fn add_file(&mut self, path: String, file_info: FileInfo) {
131        self.discovered_files.insert(path, file_info);
132    }
133
134    pub fn add_insight(&mut self, insight: ArchitectureInsight) {
135        self.architecture_insights.push(insight);
136    }
137
138    pub fn add_relationship(&mut self, relationship: ComponentRelationship) {
139        self.component_relationships.push(relationship);
140    }
141
142    pub fn add_exploration_step(&mut self, step: ExplorationStep) {
143        self.exploration_history.push(step);
144    }
145
146    pub fn summarize(&self) -> String {
147        format!(
148            "已发现 {} 个文件,{} 个架构洞察,{} 个组件关系,完成 {} 个探索步骤",
149            self.discovered_files.len(),
150            self.architecture_insights.len(),
151            self.component_relationships.len(),
152            self.exploration_history.len()
153        )
154    }
155
156    pub fn get_file_types_summary(&self) -> HashMap<String, usize> {
157        let mut type_count = HashMap::new();
158        for file_info in self.discovered_files.values() {
159            if let Some(ext) = file_info.path.extension().and_then(|e| e.to_str()) {
160                *type_count.entry(ext.to_string()).or_insert(0) += 1;
161            }
162        }
163        type_count
164    }
165}
166
167impl ExplorationState {
168    pub fn next(&self) -> Self {
169        match self {
170            ExplorationState::Initial => ExplorationState::DiscoveringStructure,
171            ExplorationState::DiscoveringStructure => ExplorationState::AnalyzingComponents,
172            ExplorationState::AnalyzingComponents => ExplorationState::MappingRelationships,
173            ExplorationState::MappingRelationships => ExplorationState::GeneratingDocumentation,
174            ExplorationState::GeneratingDocumentation => ExplorationState::Completed,
175            ExplorationState::Completed => ExplorationState::Completed,
176        }
177    }
178
179    pub fn description(&self) -> &'static str {
180        match self {
181            ExplorationState::Initial => "初始化",
182            ExplorationState::DiscoveringStructure => "发现项目结构",
183            ExplorationState::AnalyzingComponents => "分析组件",
184            ExplorationState::MappingRelationships => "映射关系",
185            ExplorationState::GeneratingDocumentation => "生成文档",
186            ExplorationState::Completed => "完成",
187        }
188    }
189}