ferrous-forge 1.9.6

System-wide Rust development standards enforcer
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
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
//! Rustup integration for toolchain management
//!
//! Provides commands to check, update, and manage Rust toolchains via rustup.
//! Enforces minimum/maximum versions set in locked config.
//!
//! @task T020
//! @epic T014

use crate::config::locking::HierarchicalLockManager;
use crate::rust_version::RustVersion;
use crate::rust_version::detector::{
    get_active_toolchain, get_installed_toolchains, is_rustup_available,
};
use crate::{Error, Result};
use semver::Version;
use serde::{Deserialize, Serialize};
use tracing::{debug, info};

/// Toolchain channel types
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum ToolchainChannel {
    /// Stable releases
    Stable,
    /// Beta releases
    Beta,
    /// Nightly builds
    Nightly,
    /// Specific version (e.g., "1.70.0")
    Version(String),
    /// Custom toolchain
    Custom(String),
}

impl std::fmt::Display for ToolchainChannel {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Self::Stable => write!(f, "stable"),
            Self::Beta => write!(f, "beta"),
            Self::Nightly => write!(f, "nightly"),
            Self::Version(v) => write!(f, "{}", v),
            Self::Custom(s) => write!(f, "{}", s),
        }
    }
}

impl ToolchainChannel {
    /// Parse a channel string into a `ToolchainChannel`
    pub fn parse(channel: &str) -> Self {
        match channel.to_lowercase().as_str() {
            "stable" => Self::Stable,
            "beta" => Self::Beta,
            "nightly" => Self::Nightly,
            s => {
                // Check if it looks like a version number
                if s.chars().next().is_some_and(|c| c.is_ascii_digit()) {
                    Self::Version(s.to_string())
                } else {
                    Self::Custom(s.to_string())
                }
            }
        }
    }
}

/// Information about an installed toolchain
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ToolchainInfo {
    /// Toolchain name/channel
    pub channel: ToolchainChannel,
    /// Whether this is the default toolchain
    pub is_default: bool,
    /// Whether the toolchain is installed
    pub is_installed: bool,
}

/// Version requirements from locked config
#[derive(Debug, Clone)]
pub struct VersionRequirements {
    /// Minimum required version (inclusive)
    pub minimum: Option<Version>,
    /// Maximum allowed version (inclusive)
    pub maximum: Option<Version>,
    /// Exact version requirement
    pub exact: Option<Version>,
}

impl VersionRequirements {
    /// Create empty requirements (no constraints)
    pub fn new() -> Self {
        Self {
            minimum: None,
            maximum: None,
            exact: None,
        }
    }

    /// Check if a version meets the requirements
    pub fn check(&self, version: &Version) -> bool {
        if let Some(exact) = &self.exact {
            return version == exact;
        }

        if let Some(minimum) = &self.minimum
            && version < minimum
        {
            return false;
        }

        if let Some(maximum) = &self.maximum
            && version > maximum
        {
            return false;
        }

        true
    }

    /// Get a human-readable description of the requirements
    pub fn description(&self) -> String {
        if let Some(exact) = &self.exact {
            return format!("exactly {}", exact);
        }

        match (&self.minimum, &self.maximum) {
            (Some(min), Some(max)) => format!("between {} and {}", min, max),
            (Some(min), None) => format!(">= {}", min),
            (None, Some(max)) => format!("<= {}", max),
            (None, None) => "any version".to_string(),
        }
    }
}

impl Default for VersionRequirements {
    fn default() -> Self {
        Self::new()
    }
}

/// Rustup manager for toolchain operations
pub struct RustupManager;

impl RustupManager {
    /// Create a new rustup manager
    pub fn new() -> Self {
        Self
    }

    /// Check if rustup is available on the system
    pub fn is_available(&self) -> bool {
        is_rustup_available()
    }

    /// Ensure rustup is available, returning an error if not
    fn ensure_rustup(&self) -> Result<()> {
        if !self.is_available() {
            return Err(Error::rust_not_found(
                "rustup not found. Please install rustup from https://rustup.rs",
            ));
        }
        Ok(())
    }

    /// Get the current Rust version with toolchain info
    ///
    /// # Errors
    ///
    /// Returns an error if `rustc` is not found or its version output cannot be parsed.
    pub async fn get_current_version(&self) -> Result<RustVersion> {
        crate::rust_version::detector::detect_rust_version().await
    }

    /// List all installed toolchains
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the toolchain list cannot be retrieved.
    pub async fn list_toolchains(&self) -> Result<Vec<ToolchainInfo>> {
        self.ensure_rustup()?;

        let active = get_active_toolchain().await?;
        let installed = get_installed_toolchains().await?;

        let toolchains: Vec<ToolchainInfo> = installed
            .into_iter()
            .map(|name| {
                let channel = ToolchainChannel::parse(&name);
                let is_default = name == active;

                ToolchainInfo {
                    channel,
                    is_default,
                    is_installed: true,
                }
            })
            .collect();

        Ok(toolchains)
    }

    /// Install a specific toolchain
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the installation fails.
    pub async fn install_toolchain(&self, channel: &ToolchainChannel) -> Result<()> {
        self.ensure_rustup()?;

        let channel_str = channel.to_string();
        info!("Installing toolchain: {}", channel_str);

        let output = tokio::process::Command::new("rustup")
            .args(["toolchain", "install", &channel_str, "--no-self-update"])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to install toolchain '{}': {}",
                channel_str, stderr
            )));
        }

        info!("Successfully installed toolchain: {}", channel_str);
        Ok(())
    }

    /// Uninstall a specific toolchain
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the uninstallation fails.
    pub async fn uninstall_toolchain(&self, channel: &ToolchainChannel) -> Result<()> {
        self.ensure_rustup()?;

        let channel_str = channel.to_string();
        info!("Uninstalling toolchain: {}", channel_str);

        let output = tokio::process::Command::new("rustup")
            .args(["toolchain", "uninstall", &channel_str])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to uninstall toolchain '{}': {}",
                channel_str, stderr
            )));
        }

        info!("Successfully uninstalled toolchain: {}", channel_str);
        Ok(())
    }

    /// Switch to a different toolchain (set as default)
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the switch fails.
    pub async fn switch_toolchain(&self, channel: &ToolchainChannel) -> Result<()> {
        self.ensure_rustup()?;

        let channel_str = channel.to_string();
        info!("Switching to toolchain: {}", channel_str);

        let output = tokio::process::Command::new("rustup")
            .args(["default", &channel_str])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to switch to toolchain '{}': {}",
                channel_str, stderr
            )));
        }

        info!("Successfully switched to toolchain: {}", channel_str);
        Ok(())
    }

    /// Update all installed toolchains
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the update fails.
    pub async fn update_toolchains(&self) -> Result<UpdateResult> {
        self.ensure_rustup()?;

        info!("Updating toolchains...");

        let output = tokio::process::Command::new("rustup")
            .args(["update", "--no-self-update"])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        let stdout = String::from_utf8_lossy(&output.stdout);
        let stderr = String::from_utf8_lossy(&output.stderr);

        if !output.status.success() {
            return Err(Error::command(format!(
                "Failed to update toolchains: {}",
                stderr
            )));
        }

        // Parse output to determine what was updated
        let updated = stdout
            .lines()
            .chain(stderr.lines())
            .filter(|line| line.contains("updated") || line.contains("installed"))
            .map(|s| s.to_string())
            .collect();

        info!("Toolchain update completed");

        Ok(UpdateResult {
            success: true,
            updated,
        })
    }

    /// Install a toolchain component (e.g., clippy, rustfmt)
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the component installation fails.
    pub async fn install_component(&self, component: &str, toolchain: Option<&str>) -> Result<()> {
        self.ensure_rustup()?;

        let mut args = vec!["component", "add", component];
        if let Some(tc) = toolchain {
            args.push("--toolchain");
            args.push(tc);
        }

        info!("Installing component '{}'", component);

        let output = tokio::process::Command::new("rustup")
            .args(&args)
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to install component '{}': {}",
                component, stderr
            )));
        }

        info!("Successfully installed component '{}'", component);
        Ok(())
    }

    /// Get version requirements from locked configuration
    ///
    /// # Errors
    ///
    /// Returns an error if loading the lock configuration fails.
    pub async fn get_version_requirements(&self) -> Result<VersionRequirements> {
        let lock_manager = HierarchicalLockManager::load().await?;
        let mut requirements = VersionRequirements::new();

        // Check for locked rust-version
        if let Some((_, entry)) = lock_manager.is_locked("rust-version") {
            debug!("Found locked rust-version: {}", entry.value);
            if let Ok(version) = Version::parse(&entry.value) {
                // For now, treat locked version as minimum requirement
                // This could be extended to support range syntax like ">=1.70.0, <1.80.0"
                requirements.minimum = Some(version);
            }
        }

        // Check for locked maximum version (if defined)
        if let Some((_, entry)) = lock_manager.is_locked("max-rust-version") {
            debug!("Found locked max-rust-version: {}", entry.value);
            if let Ok(version) = Version::parse(&entry.value) {
                requirements.maximum = Some(version);
            }
        }

        Ok(requirements)
    }

    /// Check if current Rust version meets locked requirements
    ///
    /// # Errors
    ///
    /// Returns an error if the current version cannot be determined or the lock configuration cannot be loaded.
    pub async fn check_version_requirements(&self) -> Result<VersionCheckResult> {
        let current = self.get_current_version().await?;
        let requirements = self.get_version_requirements().await?;

        let meets_requirements = requirements.check(&current.version);

        Ok(VersionCheckResult {
            current: current.version,
            requirements,
            meets_requirements,
        })
    }

    /// Run rustup self-update
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the self-update fails.
    pub async fn self_update(&self) -> Result<()> {
        self.ensure_rustup()?;

        info!("Running rustup self-update...");

        let output = tokio::process::Command::new("rustup")
            .args(["self", "update"])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to self-update rustup: {}",
                stderr
            )));
        }

        info!("Rustup self-update completed");
        Ok(())
    }

    /// Show active toolchain information
    ///
    /// # Errors
    ///
    /// Returns an error if rustup is not available or the active toolchain cannot be determined.
    pub async fn show_active_toolchain(&self) -> Result<String> {
        self.ensure_rustup()?;

        let output = tokio::process::Command::new("rustup")
            .args(["show", "active-toolchain"])
            .output()
            .await
            .map_err(|e| Error::command(format!("Failed to run rustup: {}", e)))?;

        if !output.status.success() {
            let stderr = String::from_utf8_lossy(&output.stderr);
            return Err(Error::command(format!(
                "Failed to show active toolchain: {}",
                stderr
            )));
        }

        Ok(String::from_utf8_lossy(&output.stdout).trim().to_string())
    }
}

impl Default for RustupManager {
    fn default() -> Self {
        Self::new()
    }
}

/// Result of an update operation
#[derive(Debug, Clone)]
pub struct UpdateResult {
    /// Whether the update was successful
    pub success: bool,
    /// List of updated items
    pub updated: Vec<String>,
}

/// Result of a version check
#[derive(Debug, Clone)]
pub struct VersionCheckResult {
    /// Current installed version
    pub current: Version,
    /// Required version constraints
    pub requirements: VersionRequirements,
    /// Whether the current version meets requirements
    pub meets_requirements: bool,
}

impl VersionCheckResult {
    /// Format a human-readable status message
    pub fn status_message(&self) -> String {
        if self.meets_requirements {
            format!(
                "✅ Current version {} meets requirements ({})",
                self.current,
                self.requirements.description()
            )
        } else {
            format!(
                "❌ Current version {} does NOT meet requirements ({})",
                self.current,
                self.requirements.description()
            )
        }
    }
}

#[cfg(test)]
#[allow(clippy::unwrap_used, clippy::expect_used, clippy::panic)]
mod tests {
    use super::*;

    #[test]
    fn test_toolchain_channel_display() {
        assert_eq!(ToolchainChannel::Stable.to_string(), "stable");
        assert_eq!(ToolchainChannel::Beta.to_string(), "beta");
        assert_eq!(ToolchainChannel::Nightly.to_string(), "nightly");
        assert_eq!(
            ToolchainChannel::Version("1.70.0".to_string()).to_string(),
            "1.70.0"
        );
        assert_eq!(
            ToolchainChannel::Custom("my-toolchain".to_string()).to_string(),
            "my-toolchain"
        );
    }

    #[test]
    fn test_toolchain_channel_parse() {
        assert!(matches!(
            ToolchainChannel::parse("stable"),
            ToolchainChannel::Stable
        ));
        assert!(matches!(
            ToolchainChannel::parse("beta"),
            ToolchainChannel::Beta
        ));
        assert!(matches!(
            ToolchainChannel::parse("nightly"),
            ToolchainChannel::Nightly
        ));
        assert!(matches!(
            ToolchainChannel::parse("1.70.0"),
            ToolchainChannel::Version(_)
        ));
        assert!(matches!(
            ToolchainChannel::parse("custom-toolchain"),
            ToolchainChannel::Custom(_)
        ));
    }

    #[test]
    fn test_version_requirements_check() {
        let mut req = VersionRequirements::new();
        let v170 = Version::new(1, 70, 0);
        let v180 = Version::new(1, 80, 0);
        let v190 = Version::new(1, 90, 0);

        // No constraints
        assert!(req.check(&v170));

        // Minimum version
        req.minimum = Some(v180.clone());
        assert!(!req.check(&v170));
        assert!(req.check(&v180));
        assert!(req.check(&v190));

        // Maximum version
        req = VersionRequirements::new();
        req.maximum = Some(v180.clone());
        assert!(req.check(&v170));
        assert!(req.check(&v180));
        assert!(!req.check(&v190));

        // Exact version
        req = VersionRequirements::new();
        req.exact = Some(v180.clone());
        assert!(!req.check(&v170));
        assert!(req.check(&v180));
        assert!(!req.check(&v190));
    }

    #[test]
    fn test_version_requirements_description() {
        let mut req = VersionRequirements::new();
        assert_eq!(req.description(), "any version");

        req.minimum = Some(Version::new(1, 70, 0));
        assert_eq!(req.description(), ">= 1.70.0");

        req.maximum = Some(Version::new(1, 80, 0));
        assert_eq!(req.description(), "between 1.70.0 and 1.80.0");

        req = VersionRequirements::new();
        req.exact = Some(Version::new(1, 75, 0));
        assert_eq!(req.description(), "exactly 1.75.0");
    }
}