actr_cli/core/
components.rs

1//! 核心复用组件定义
2//!
3//! 定义了8个核心组件的trait接口,支持依赖注入和组合使用
4
5pub mod config_manager;
6pub mod dependency_resolver;
7pub mod service_discovery;
8pub use actr_config::Config;
9pub use config_manager::TomlConfigManager;
10pub use dependency_resolver::DefaultDependencyResolver;
11pub use service_discovery::NetworkServiceDiscovery;
12
13use anyhow::Result;
14use async_trait::async_trait;
15use serde::{Deserialize, Serialize};
16use std::path::{Path, PathBuf};
17
18// ============================================================================
19// 核心数据类型
20// ============================================================================
21
22/// 依赖规范
23#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
24pub struct DependencySpec {
25    pub name: String,
26    pub uri: String,
27    pub version: Option<String>,
28    pub fingerprint: Option<String>,
29}
30
31/// 解析后的依赖信息
32#[derive(Debug, Clone)]
33pub struct ResolvedDependency {
34    pub spec: DependencySpec,
35    pub uri: String,
36    pub resolved_version: String,
37    pub fingerprint: String,
38    pub proto_files: Vec<ProtoFile>,
39}
40
41/// Proto文件信息
42#[derive(Debug, Clone)]
43pub struct ProtoFile {
44    pub name: String,
45    pub path: PathBuf,
46    pub content: String,
47    pub services: Vec<ServiceDefinition>,
48}
49
50/// 服务定义
51#[derive(Debug, Clone)]
52pub struct ServiceDefinition {
53    pub name: String,
54    pub methods: Vec<MethodDefinition>,
55}
56
57/// 方法定义
58#[derive(Debug, Clone)]
59pub struct MethodDefinition {
60    pub name: String,
61    pub input_type: String,
62    pub output_type: String,
63}
64
65/// 服务信息
66#[derive(Debug, Clone)]
67pub struct ServiceInfo {
68    pub name: String,
69    pub uri: String,
70    pub version: String,
71    pub fingerprint: String,
72    pub description: Option<String>,
73    pub methods: Vec<MethodDefinition>,
74}
75
76/// 服务详情
77#[derive(Debug, Clone)]
78pub struct ServiceDetails {
79    pub info: ServiceInfo,
80    pub proto_files: Vec<ProtoFile>,
81    pub dependencies: Vec<String>,
82}
83
84/// 指纹信息
85#[derive(Debug, Clone, PartialEq)]
86pub struct Fingerprint {
87    pub algorithm: String,
88    pub value: String,
89}
90
91/// 验证报告
92#[derive(Debug, Clone)]
93pub struct ValidationReport {
94    pub is_valid: bool,
95    pub config_validation: ConfigValidation,
96    pub dependency_validation: Vec<DependencyValidation>,
97    pub network_validation: Vec<NetworkValidation>,
98    pub fingerprint_validation: Vec<FingerprintValidation>,
99    pub conflicts: Vec<ConflictReport>,
100}
101
102#[derive(Debug, Clone)]
103pub struct ConfigValidation {
104    pub is_valid: bool,
105    pub errors: Vec<String>,
106    pub warnings: Vec<String>,
107}
108
109#[derive(Debug, Clone)]
110pub struct DependencyValidation {
111    pub dependency: String,
112    pub is_available: bool,
113    pub resolved_uri: Option<String>,
114    pub error: Option<String>,
115}
116
117#[derive(Debug, Clone)]
118pub struct NetworkValidation {
119    pub uri: String,
120    pub is_reachable: bool,
121    pub latency_ms: Option<u64>,
122    pub error: Option<String>,
123}
124
125#[derive(Debug, Clone)]
126pub struct FingerprintValidation {
127    pub dependency: String,
128    pub expected: Fingerprint,
129    pub actual: Option<Fingerprint>,
130    pub is_valid: bool,
131    pub error: Option<String>,
132}
133
134#[derive(Debug, Clone)]
135pub struct ConflictReport {
136    pub dependency_a: String,
137    pub dependency_b: String,
138    pub conflict_type: ConflictType,
139    pub description: String,
140}
141
142#[derive(Debug, Clone)]
143pub enum ConflictType {
144    VersionConflict,
145    FingerprintMismatch,
146    CircularDependency,
147}
148
149impl ValidationReport {
150    pub fn is_success(&self) -> bool {
151        self.is_valid
152            && self.config_validation.is_valid
153            && self.dependency_validation.iter().all(|d| d.is_available)
154            && self.network_validation.iter().all(|n| n.is_reachable)
155            && self.fingerprint_validation.iter().all(|f| f.is_valid)
156            && self.conflicts.is_empty()
157    }
158}
159
160// ============================================================================
161// 1. 配置管理组件 (ConfigManager)
162// ============================================================================
163
164/// 统一的配置管理接口
165#[async_trait]
166pub trait ConfigManager: Send + Sync {
167    /// 加载配置文件
168    async fn load_config(&self, path: &Path) -> Result<Config>;
169
170    /// 保存配置文件
171    async fn save_config(&self, config: &Config, path: &Path) -> Result<()>;
172
173    /// 更新依赖配置
174    async fn update_dependency(&self, spec: &DependencySpec) -> Result<()>;
175
176    /// 验证配置文件
177    async fn validate_config(&self) -> Result<ConfigValidation>;
178
179    /// 获取项目根目录
180    fn get_project_root(&self) -> &Path;
181
182    /// 备份当前配置
183    async fn backup_config(&self) -> Result<ConfigBackup>;
184
185    /// 恢复配置备份
186    async fn restore_backup(&self, backup: ConfigBackup) -> Result<()>;
187
188    /// 删除配置备份
189    async fn remove_backup(&self, backup: ConfigBackup) -> Result<()>;
190}
191
192#[derive(Debug, Clone, Serialize, Deserialize)]
193pub struct PackageConfig {
194    pub name: String,
195    pub version: String,
196    #[serde(rename = "type")]
197    pub package_type: Option<String>,
198}
199
200/// 配置备份
201#[derive(Debug, Clone)]
202pub struct ConfigBackup {
203    pub original_path: PathBuf,
204    pub backup_path: PathBuf,
205    pub timestamp: std::time::SystemTime,
206}
207
208// ============================================================================
209// 2. 依赖解析组件 (DependencyResolver)
210// ============================================================================
211
212/// 依赖解析和冲突检测
213#[async_trait]
214pub trait DependencyResolver: Send + Sync {
215    /// 解析依赖规范字符串
216    async fn resolve_spec(&self, spec: &str) -> Result<DependencySpec>;
217
218    /// 解析多个依赖
219    async fn resolve_dependencies(
220        &self,
221        specs: &[DependencySpec],
222    ) -> Result<Vec<ResolvedDependency>>;
223
224    /// 检查依赖冲突
225    async fn check_conflicts(&self, deps: &[ResolvedDependency]) -> Result<Vec<ConflictReport>>;
226
227    /// 构建依赖图
228    async fn build_dependency_graph(&self, deps: &[ResolvedDependency]) -> Result<DependencyGraph>;
229}
230
231#[derive(Debug, Clone)]
232pub struct DependencyGraph {
233    pub nodes: Vec<String>,
234    pub edges: Vec<(String, String)>,
235    pub has_cycles: bool,
236}
237
238// ============================================================================
239// 3. 服务发现组件 (ServiceDiscovery)
240// ============================================================================
241
242/// 服务发现和网络交互
243#[async_trait]
244pub trait ServiceDiscovery: Send + Sync {
245    /// 发现网络中的服务
246    async fn discover_services(&self, filter: Option<&ServiceFilter>) -> Result<Vec<ServiceInfo>>;
247
248    /// 获取服务详细信息
249    async fn get_service_details(&self, uri: &str) -> Result<ServiceDetails>;
250
251    /// 检查服务可用性
252    async fn check_service_availability(&self, uri: &str) -> Result<AvailabilityStatus>;
253
254    /// 获取服务Proto文件
255    async fn get_service_proto(&self, uri: &str) -> Result<Vec<ProtoFile>>;
256}
257
258#[derive(Debug, Clone)]
259pub struct ServiceFilter {
260    pub name_pattern: Option<String>,
261    pub version_range: Option<String>,
262    pub tags: Option<Vec<String>>,
263}
264
265#[derive(Debug, Clone)]
266pub struct AvailabilityStatus {
267    pub is_available: bool,
268    pub last_seen: Option<std::time::SystemTime>,
269    pub health: HealthStatus,
270}
271
272#[derive(Debug, Clone)]
273pub enum HealthStatus {
274    Healthy,
275    Degraded,
276    Unhealthy,
277    Unknown,
278}
279
280// ============================================================================
281// 4. 网络验证组件 (NetworkValidator)
282// ============================================================================
283
284/// 网络连通性验证
285#[async_trait]
286pub trait NetworkValidator: Send + Sync {
287    /// 检查连通性
288    async fn check_connectivity(&self, uri: &str) -> Result<ConnectivityStatus>;
289
290    /// 验证服务健康状态
291    async fn verify_service_health(&self, uri: &str) -> Result<HealthStatus>;
292
293    /// 测试延迟
294    async fn test_latency(&self, uri: &str) -> Result<LatencyInfo>;
295
296    /// 批量检查
297    async fn batch_check(&self, uris: &[String]) -> Result<Vec<NetworkCheckResult>>;
298}
299
300#[derive(Debug, Clone)]
301pub struct ConnectivityStatus {
302    pub is_reachable: bool,
303    pub response_time_ms: Option<u64>,
304    pub error: Option<String>,
305}
306
307#[derive(Debug, Clone)]
308pub struct LatencyInfo {
309    pub min_ms: u64,
310    pub max_ms: u64,
311    pub avg_ms: u64,
312    pub samples: u32,
313}
314
315#[derive(Debug, Clone)]
316pub struct NetworkCheckResult {
317    pub uri: String,
318    pub connectivity: ConnectivityStatus,
319    pub health: HealthStatus,
320    pub latency: Option<LatencyInfo>,
321}
322
323// ============================================================================
324// 5. 指纹验证组件 (FingerprintValidator)
325// ============================================================================
326
327/// 指纹计算和验证
328#[async_trait]
329pub trait FingerprintValidator: Send + Sync {
330    /// 计算服务指纹
331    async fn compute_service_fingerprint(&self, service: &ServiceInfo) -> Result<Fingerprint>;
332
333    /// 验证指纹匹配
334    async fn verify_fingerprint(
335        &self,
336        expected: &Fingerprint,
337        actual: &Fingerprint,
338    ) -> Result<bool>;
339
340    /// 计算项目指纹
341    async fn compute_project_fingerprint(&self, project_path: &Path) -> Result<Fingerprint>;
342
343    /// 生成锁文件指纹
344    async fn generate_lock_fingerprint(&self, deps: &[ResolvedDependency]) -> Result<Fingerprint>;
345}
346
347// ============================================================================
348// 6. Proto处理组件 (ProtoProcessor)
349// ============================================================================
350
351/// Protocol Buffers 文件处理
352#[async_trait]
353pub trait ProtoProcessor: Send + Sync {
354    /// 发现Proto文件
355    async fn discover_proto_files(&self, path: &Path) -> Result<Vec<ProtoFile>>;
356
357    /// 解析Proto服务
358    async fn parse_proto_services(&self, files: &[ProtoFile]) -> Result<Vec<ServiceDefinition>>;
359
360    /// 生成代码
361    async fn generate_code(&self, input: &Path, output: &Path) -> Result<GenerationResult>;
362
363    /// 验证Proto语法
364    async fn validate_proto_syntax(&self, files: &[ProtoFile]) -> Result<ValidationReport>;
365}
366
367#[derive(Debug, Clone)]
368pub struct GenerationResult {
369    pub generated_files: Vec<PathBuf>,
370    pub warnings: Vec<String>,
371    pub errors: Vec<String>,
372}
373
374// ============================================================================
375// 7. 缓存管理组件 (CacheManager)
376// ============================================================================
377
378/// 依赖缓存管理
379#[async_trait]
380pub trait CacheManager: Send + Sync {
381    /// 获取缓存的Proto
382    async fn get_cached_proto(&self, uri: &str) -> Result<Option<CachedProto>>;
383
384    /// 缓存Proto文件
385    async fn cache_proto(&self, uri: &str, proto: &[ProtoFile]) -> Result<()>;
386
387    /// 失效缓存
388    async fn invalidate_cache(&self, uri: &str) -> Result<()>;
389
390    /// 清理缓存
391    async fn clear_cache(&self) -> Result<()>;
392
393    /// 获取缓存统计
394    async fn get_cache_stats(&self) -> Result<CacheStats>;
395}
396
397#[derive(Debug, Clone)]
398pub struct CachedProto {
399    pub uri: String,
400    pub files: Vec<ProtoFile>,
401    pub fingerprint: Fingerprint,
402    pub cached_at: std::time::SystemTime,
403    pub expires_at: Option<std::time::SystemTime>,
404}
405
406#[derive(Debug, Clone)]
407pub struct CacheStats {
408    pub total_entries: usize,
409    pub total_size_bytes: u64,
410    pub hit_rate: f64,
411    pub miss_rate: f64,
412}
413
414// ============================================================================
415// 8. 用户交互组件 (UserInterface)
416// ============================================================================
417
418/// 用户交互界面
419#[async_trait]
420pub trait UserInterface: Send + Sync {
421    /// 提示输入
422    async fn prompt_input(&self, prompt: &str) -> Result<String>;
423
424    /// 确认操作
425    async fn confirm(&self, message: &str) -> Result<bool>;
426
427    /// 从服务列表中选择
428    async fn select_service_from_list(
429        &self,
430        items: &[ServiceInfo],
431        formatter: fn(&ServiceInfo) -> String,
432    ) -> Result<usize>;
433
434    /// 从字符串列表中选择
435    async fn select_string_from_list(
436        &self,
437        items: &[String],
438        formatter: fn(&String) -> String,
439    ) -> Result<usize>;
440
441    /// 显示服务表格
442    async fn display_service_table(
443        &self,
444        items: &[ServiceInfo],
445        headers: &[&str],
446        formatter: fn(&ServiceInfo) -> Vec<String>,
447    );
448
449    /// 显示进度条
450    async fn show_progress(&self, message: &str) -> Result<Box<dyn ProgressBar>>;
451}
452
453/// 进度条接口
454pub trait ProgressBar: Send + Sync {
455    fn update(&self, progress: f64);
456    fn set_message(&self, message: &str);
457    fn finish(&self);
458}