vx_config/
types.rs

1//! Core types for vx configuration management
2
3use serde::{Deserialize, Serialize};
4use std::collections::HashMap;
5use std::path::PathBuf;
6
7/// Main vx configuration structure
8#[derive(Debug, Serialize, Deserialize, Default, Clone)]
9pub struct VxConfig {
10    /// Global default settings
11    pub defaults: DefaultConfig,
12    /// Tool-specific configurations
13    pub tools: HashMap<String, ToolConfig>,
14    /// Registry configurations
15    pub registries: HashMap<String, RegistryConfig>,
16}
17
18/// Default configuration settings
19#[derive(Debug, Serialize, Deserialize, Clone)]
20pub struct DefaultConfig {
21    /// Whether to automatically install missing tools
22    pub auto_install: bool,
23    /// Default cache duration for downloads
24    pub cache_duration: String,
25    /// Whether to fall back to builtin tool configurations
26    pub fallback_to_builtin: bool,
27    /// Default installation directory
28    pub install_dir: Option<String>,
29    /// Whether to use system PATH for tools
30    pub use_system_path: bool,
31}
32
33impl Default for DefaultConfig {
34    fn default() -> Self {
35        Self {
36            auto_install: true,
37            cache_duration: "7d".to_string(),
38            fallback_to_builtin: true,
39            install_dir: None,
40            use_system_path: false,
41        }
42    }
43}
44
45/// Configuration for a specific tool
46#[derive(Debug, Serialize, Deserialize, Clone)]
47pub struct ToolConfig {
48    /// Specific version to use
49    pub version: Option<String>,
50    /// Installation method (auto, manual, system)
51    pub install_method: Option<String>,
52    /// Registry to use for this tool
53    pub registry: Option<String>,
54    /// Custom download sources
55    pub custom_sources: Option<Vec<String>>,
56}
57
58/// Registry configuration
59#[derive(Debug, Serialize, Deserialize, Clone)]
60pub struct RegistryConfig {
61    /// Registry URL
62    pub url: String,
63    /// Authentication token if required
64    pub token: Option<String>,
65    /// Whether this registry is trusted
66    pub trusted: bool,
67}
68
69/// Supported project types
70#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
71pub enum ProjectType {
72    Python,  // pyproject.toml
73    Rust,    // Cargo.toml
74    Node,    // package.json
75    Go,      // go.mod
76    Mixed,   // Multiple project types
77    Unknown, // No recognized project files
78}
79
80/// Information about a detected project
81#[derive(Debug, Clone)]
82pub struct ProjectInfo {
83    /// Type of project detected
84    pub project_type: ProjectType,
85    /// Path to the primary configuration file
86    pub config_file: PathBuf,
87    /// Tool versions detected from project files
88    pub tool_versions: HashMap<String, String>,
89}
90
91/// Configuration status for diagnostics
92#[derive(Debug, Clone)]
93pub struct ConfigStatus {
94    /// Information about configuration layers
95    pub layers: Vec<LayerInfo>,
96    /// List of available tools
97    pub available_tools: Vec<String>,
98    /// Whether fallback to builtin is enabled
99    pub fallback_enabled: bool,
100    /// Project information if detected
101    pub project_info: Option<ProjectInfo>,
102}
103
104/// Information about a configuration layer
105#[derive(Debug, Clone)]
106pub struct LayerInfo {
107    /// Name of the layer (builtin, user, project, environment)
108    pub name: String,
109    /// Whether this layer is available/active
110    pub available: bool,
111    /// Priority of this layer (higher = more important)
112    pub priority: i32,
113}
114
115impl ConfigStatus {
116    /// Get a summary of the configuration status
117    pub fn summary(&self) -> String {
118        let active_layers: Vec<&str> = self
119            .layers
120            .iter()
121            .filter(|l| l.available)
122            .map(|l| l.name.as_str())
123            .collect();
124
125        format!(
126            "Configuration layers: {} | Tools: {} | Fallback: {}",
127            active_layers.join(", "),
128            self.available_tools.len(),
129            if self.fallback_enabled {
130                "enabled"
131            } else {
132                "disabled"
133            }
134        )
135    }
136
137    /// Check if the configuration is healthy
138    pub fn is_healthy(&self) -> bool {
139        // At least one layer should be available
140        self.layers.iter().any(|l| l.available) && !self.available_tools.is_empty()
141    }
142}
143
144/// Project configuration for .vx.toml files
145#[derive(Debug, Serialize, Deserialize, Default, Clone)]
146pub struct ProjectConfig {
147    /// Tool versions required for this project
148    pub tools: HashMap<String, String>,
149    /// Project-specific settings
150    pub settings: ProjectSettings,
151}
152
153/// Settings specific to a project
154#[derive(Debug, Serialize, Deserialize, Clone)]
155pub struct ProjectSettings {
156    /// Whether to auto-install missing tools
157    pub auto_install: bool,
158    /// Cache duration for this project
159    pub cache_duration: String,
160}
161
162impl Default for ProjectSettings {
163    fn default() -> Self {
164        Self {
165            auto_install: true,
166            cache_duration: "7d".to_string(),
167        }
168    }
169}