actr-cli 0.3.1

Command line tool for Actor-RTC framework projects
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
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
//! Core reusable component definitions
//!
//! Defines trait interfaces for 8 core components, supporting dependency injection and composition

pub mod cache_manager;
pub mod config_manager;
pub mod dependency_resolver;
pub mod fingerprint_validator;
pub mod network_validator;
pub mod proto_processor;
pub mod service_discovery;
pub mod user_interface;
use actr_protocol::{ActrType, discovery_response::TypeEntry};
pub use cache_manager::DefaultCacheManager;
pub use config_manager::TomlConfigManager;
pub use dependency_resolver::DefaultDependencyResolver;
pub use fingerprint_validator::DefaultFingerprintValidator;
pub use network_validator::DefaultNetworkValidator;
pub use proto_processor::DefaultProtoProcessor;
pub use service_discovery::{DiscoveryContext, NetworkServiceDiscovery};
pub use user_interface::ConsoleUI;

use actr_config::ManifestConfig;
use anyhow::Result;
use async_trait::async_trait;
use serde::{Deserialize, Serialize};
use std::path::{Path, PathBuf};
use std::time::Duration;

// ============================================================================
// Core data types
// ============================================================================

/// Dependency specification
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
pub struct DependencySpec {
    pub alias: String,
    pub name: String,
    pub actr_type: Option<ActrType>,
    pub fingerprint: Option<String>,
}

/// Resolved dependency information
#[derive(Debug, Clone)]
pub struct ResolvedDependency {
    pub spec: DependencySpec,
    pub fingerprint: String,
    pub proto_files: Vec<ProtoFile>,
}

/// Proto file information
#[derive(Debug, Clone)]
pub struct ProtoFile {
    pub name: String,
    pub path: PathBuf,
    pub content: String,
    pub services: Vec<ServiceDefinition>,
}

/// Service definition
#[derive(Debug, Clone)]
pub struct ServiceDefinition {
    pub name: String,
    pub methods: Vec<MethodDefinition>,
}

/// Method definition
#[derive(Debug, Clone)]
pub struct MethodDefinition {
    pub name: String,
    pub input_type: String,
    pub output_type: String,
}

/// Service information
#[derive(Debug, Clone)]
pub struct ServiceInfo {
    /// Service name (package name)
    pub name: String,
    pub tags: Vec<String>,
    pub fingerprint: String,
    pub actr_type: ActrType,
    pub published_at: Option<i64>,
    pub description: Option<String>,
    pub methods: Vec<MethodDefinition>,
}

/// Service details
#[derive(Debug, Clone)]
pub struct ServiceDetails {
    pub info: ServiceInfo,
    pub proto_files: Vec<ProtoFile>,
    pub dependencies: Vec<String>,
}

/// Fingerprint information
#[derive(Debug, Clone, PartialEq)]
pub struct Fingerprint {
    pub algorithm: String,
    pub value: String,
}

/// Validation report
#[derive(Debug, Clone)]
pub struct ValidationReport {
    pub is_valid: bool,
    pub config_validation: ConfigValidation,
    pub dependency_validation: Vec<DependencyValidation>,
    pub network_validation: Vec<NetworkValidation>,
    pub fingerprint_validation: Vec<FingerprintValidation>,
    pub conflicts: Vec<ConflictReport>,
}

#[derive(Debug, Clone)]
pub struct ConfigValidation {
    pub is_valid: bool,
    pub errors: Vec<String>,
    pub warnings: Vec<String>,
}

#[derive(Debug, Clone)]
pub struct DependencyValidation {
    pub dependency: String,
    pub is_available: bool,
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct NetworkValidation {
    pub is_reachable: bool,
    pub health: HealthStatus,
    pub latency_ms: Option<u64>,
    pub error: Option<String>,
    pub is_applicable: bool,
}

#[derive(Debug, Clone)]
pub struct FingerprintValidation {
    pub dependency: String,
    pub expected: Fingerprint,
    pub actual: Option<Fingerprint>,
    pub is_valid: bool,
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct ConflictReport {
    pub dependency_a: String,
    pub dependency_b: String,
    pub conflict_type: ConflictType,
    pub description: String,
}

#[derive(Debug, Clone)]
pub enum ConflictType {
    VersionConflict,
    FingerprintMismatch,
    CircularDependency,
}

impl ValidationReport {
    pub fn is_success(&self) -> bool {
        self.is_valid
            && self.config_validation.is_valid
            && self.dependency_validation.iter().all(|d| d.is_available)
            && self
                .network_validation
                .iter()
                .all(|n| !n.is_applicable || n.is_reachable)
            && self.fingerprint_validation.iter().all(|f| f.is_valid)
            && self.conflicts.is_empty()
    }
}

// ============================================================================
// 1. Configuration Management Component (ConfigManager)
// ============================================================================

/// Unified configuration management interface
#[async_trait]
pub trait ConfigManager: Send + Sync {
    /// Load configuration file
    async fn load_config(&self, path: &Path) -> Result<ManifestConfig>;

    /// Save configuration file
    async fn save_config(&self, config: &ManifestConfig, path: &Path) -> Result<()>;

    /// Update dependency configuration
    async fn update_dependency(&self, spec: &DependencySpec) -> Result<()>;

    /// Validate configuration file
    async fn validate_config(&self) -> Result<ConfigValidation>;

    /// Get project root directory
    fn get_project_root(&self) -> &Path;

    /// Back up current configuration
    async fn backup_config(&self) -> Result<ConfigBackup>;

    /// Restore configuration backup
    async fn restore_backup(&self, backup: ConfigBackup) -> Result<()>;

    /// Remove configuration backup
    async fn remove_backup(&self, backup: ConfigBackup) -> Result<()>;
}

#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct PackageConfig {
    pub name: String,
    pub version: String,
    #[serde(rename = "type")]
    pub package_type: Option<String>,
}

/// Configuration backup
#[derive(Debug, Clone)]
pub struct ConfigBackup {
    pub original_path: PathBuf,
    pub backup_path: PathBuf,
    pub timestamp: std::time::SystemTime,
}

// ============================================================================
// 2. Dependency Resolver Component (DependencyResolver)
// ============================================================================

/// Dependency resolution and conflict detection
#[async_trait]
pub trait DependencyResolver: Send + Sync {
    /// Parse dependencies from config
    async fn resolve_spec(&self, config: &ManifestConfig) -> Result<Vec<DependencySpec>>;

    /// Resolve dependencies and fetch proto files
    async fn resolve_dependencies(
        &self,
        specs: &[DependencySpec],
        service_details: &[ServiceDetails],
    ) -> Result<Vec<ResolvedDependency>>;

    /// Check dependency conflicts
    async fn check_conflicts(&self, deps: &[ResolvedDependency]) -> Result<Vec<ConflictReport>>;

    /// Build dependency graph
    async fn build_dependency_graph(&self, deps: &[ResolvedDependency]) -> Result<DependencyGraph>;
}

#[derive(Debug, Clone)]
pub struct DependencyGraph {
    pub nodes: Vec<String>,
    pub edges: Vec<(String, String)>,
    pub has_cycles: bool,
}

// ============================================================================
// 3. Service Discovery Component (ServiceDiscovery)
// ============================================================================

/// Service discovery and network interaction
#[async_trait]
pub trait ServiceDiscovery: Send + Sync {
    /// Discover services in the network
    async fn discover_services(&self, filter: Option<&ServiceFilter>) -> Result<Vec<ServiceInfo>>;

    /// Get detailed service information
    async fn get_service_details(&self, name: &str) -> Result<ServiceDetails>;

    /// Check service availability
    async fn check_service_availability(&self, name: &str) -> Result<AvailabilityStatus>;

    /// Get service proto files
    async fn get_service_proto(&self, name: &str) -> Result<Vec<ProtoFile>>;
}

#[derive(Debug, Clone)]
pub struct ServiceFilter {
    pub name_pattern: Option<String>,
    pub version_range: Option<String>,
    pub tags: Option<Vec<String>>,
}

#[derive(Debug, Clone)]
pub struct AvailabilityStatus {
    pub is_available: bool,
    pub last_seen: Option<std::time::SystemTime>,
    pub health: HealthStatus,
}

#[derive(Debug, Clone)]
pub enum HealthStatus {
    Healthy,
    Degraded,
    Unhealthy,
    Unknown,
}

// ============================================================================
// 4. Network Validator Component (NetworkValidator)
// ============================================================================

/// Network connectivity validation
#[async_trait]
pub trait NetworkValidator: Send + Sync {
    /// Check connectivity
    async fn check_connectivity(
        &self,
        service_name: &str,
        options: &NetworkCheckOptions,
    ) -> Result<ConnectivityStatus>;

    /// Verify service health status
    async fn verify_service_health(
        &self,
        service_name: &str,
        options: &NetworkCheckOptions,
    ) -> Result<HealthStatus>;

    /// Test latency
    async fn test_latency(
        &self,
        service_name: &str,
        options: &NetworkCheckOptions,
    ) -> Result<LatencyInfo>;

    /// Batch check
    async fn batch_check(
        &self,
        service_names: &[String],
        options: &NetworkCheckOptions,
    ) -> Result<Vec<NetworkCheckResult>>;
}

#[derive(Debug, Clone)]
pub struct ConnectivityStatus {
    pub is_reachable: bool,
    pub response_time_ms: Option<u64>,
    pub error: Option<String>,
}

#[derive(Debug, Clone)]
pub struct LatencyInfo {
    pub min_ms: u64,
    pub max_ms: u64,
    pub avg_ms: u64,
    pub samples: u32,
}

#[derive(Debug, Clone)]
pub struct NetworkCheckResult {
    pub connectivity: ConnectivityStatus,
    pub health: HealthStatus,
    pub latency: Option<LatencyInfo>,
}

/// Options for network checks.
#[derive(Debug, Clone)]
pub struct NetworkCheckOptions {
    pub timeout: Duration,
}

impl NetworkCheckOptions {
    pub fn with_timeout(timeout: Duration) -> Self {
        Self { timeout }
    }

    pub fn with_timeout_secs(timeout_secs: u64) -> Self {
        Self::with_timeout(Duration::from_secs(timeout_secs))
    }
}

impl Default for NetworkCheckOptions {
    fn default() -> Self {
        Self {
            timeout: Duration::from_secs(5),
        }
    }
}

// ============================================================================
// 5. Fingerprint Validator Component (FingerprintValidator)
// ============================================================================

/// Fingerprint computation and validation
#[async_trait]
pub trait FingerprintValidator: Send + Sync {
    /// Compute service fingerprint
    async fn compute_service_fingerprint(&self, service: &ServiceInfo) -> Result<Fingerprint>;

    /// Verify fingerprint match
    async fn verify_fingerprint(
        &self,
        expected: &Fingerprint,
        actual: &Fingerprint,
    ) -> Result<bool>;

    /// Compute project fingerprint
    async fn compute_project_fingerprint(&self, project_path: &Path) -> Result<Fingerprint>;

    /// Generate lock file fingerprint
    async fn generate_lock_fingerprint(&self, deps: &[ResolvedDependency]) -> Result<Fingerprint>;
}

// ============================================================================
// 6. Proto Processor Component (ProtoProcessor)
// ============================================================================

/// Protocol Buffers file processing
#[async_trait]
pub trait ProtoProcessor: Send + Sync {
    /// Discover proto files
    async fn discover_proto_files(&self, path: &Path) -> Result<Vec<ProtoFile>>;

    /// Parse proto services
    async fn parse_proto_services(&self, files: &[ProtoFile]) -> Result<Vec<ServiceDefinition>>;

    /// Generate code
    async fn generate_code(&self, input: &Path, output: &Path) -> Result<GenerationResult>;

    /// Validate proto syntax
    async fn validate_proto_syntax(&self, files: &[ProtoFile]) -> Result<ValidationReport>;
}

#[derive(Debug, Clone)]
pub struct GenerationResult {
    pub generated_files: Vec<PathBuf>,
    pub warnings: Vec<String>,
    pub errors: Vec<String>,
}

// ============================================================================
// 7. Cache Manager Component (CacheManager)
// ============================================================================

/// Dependency cache management
#[async_trait]
pub trait CacheManager: Send + Sync {
    /// Get cached proto
    async fn get_cached_proto(&self, service_name: &str) -> Result<Option<CachedProto>>;

    /// Cache proto files
    async fn cache_proto(&self, service_name: &str, proto: &[ProtoFile]) -> Result<()>;

    /// Invalidate cache
    async fn invalidate_cache(&self, service_name: &str) -> Result<()>;

    /// Clear cache
    async fn clear_cache(&self) -> Result<()>;

    /// Get cache statistics
    async fn get_cache_stats(&self) -> Result<CacheStats>;
}

#[derive(Debug, Clone)]
pub struct CachedProto {
    pub files: Vec<ProtoFile>,
    pub fingerprint: Fingerprint,
    pub cached_at: std::time::SystemTime,
    pub expires_at: Option<std::time::SystemTime>,
}

#[derive(Debug, Clone)]
pub struct CacheStats {
    pub total_entries: usize,
    pub total_size_bytes: u64,
    pub hit_rate: f64,
    pub miss_rate: f64,
}

// ============================================================================
// 8. User Interface Component (UserInterface)
// ============================================================================

/// User interaction interface
#[async_trait]
pub trait UserInterface: Send + Sync {
    /// Prompt for input
    async fn prompt_input(&self, prompt: &str) -> Result<String>;

    /// Confirm an operation
    async fn confirm(&self, message: &str) -> Result<bool>;

    /// Select one item from a list
    async fn select_from_list(&self, items: &[String], prompt: &str) -> Result<usize>;

    /// Display a service table
    async fn display_service_table(
        &self,
        items: &[ServiceInfo],
        headers: &[&str],
        formatter: fn(&ServiceInfo) -> Vec<String>,
    );

    /// Show a progress bar
    async fn show_progress(&self, message: &str) -> Result<Box<dyn ProgressBar>>;
}

/// Progress bar interface
pub trait ProgressBar: Send + Sync {
    fn update(&self, progress: f64);
    fn set_message(&self, message: &str);
    fn finish(&self);
}

impl From<TypeEntry> for ServiceInfo {
    fn from(entry: TypeEntry) -> Self {
        let name = entry.name.clone();
        let tags = entry.tags.clone();
        let actr_type = entry.actr_type.clone();

        Self {
            name,
            actr_type,
            tags,
            published_at: entry.published_at,
            fingerprint: entry.service_fingerprint,
            description: entry.description,
            methods: Vec::new(),
        }
    }
}