ferrous-forge 1.9.4

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
//! Configuration locking system
//!
//! Provides mandatory locking mechanism for critical configuration values.
//! Once locked, values cannot be changed without explicit unlock with justification.
//!
//! @task T015
//! @epic T014

use crate::config::hierarchy::ConfigLevel;
use crate::{Error, Result};
use chrono::{DateTime, Utc};
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::path::PathBuf;
use tokio::fs;
use tracing::{info, warn};

/// Audit logging for lock/unlock operations
pub mod audit_log;
/// Configuration change validator
pub mod validator;

pub use validator::ConfigValidator;

/// Configuration lock entry with metadata
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct LockEntry {
    /// The locked value
    pub value: String,
    /// When the lock was created
    pub locked_at: DateTime<Utc>,
    /// Who/what created the lock (user, system, etc.)
    pub locked_by: String,
    /// Reason for locking
    pub reason: String,
    /// Configuration level where lock is set
    pub level: ConfigLevel,
}

impl LockEntry {
    /// Create a new lock entry
    pub fn new(value: impl Into<String>, reason: impl Into<String>, level: ConfigLevel) -> Self {
        Self {
            value: value.into(),
            locked_at: Utc::now(),
            locked_by: whoami::username(),
            reason: reason.into(),
            level,
        }
    }
}

/// Locked configuration storage
#[derive(Debug, Clone, Serialize, Deserialize, Default)]
pub struct LockedConfig {
    /// Map of configuration keys to their lock entries
    pub locks: HashMap<String, LockEntry>,
    /// Version of the lock file format
    pub version: String,
}

impl LockedConfig {
    /// Create a new empty locked config
    pub fn new() -> Self {
        Self {
            locks: HashMap::new(),
            version: "1.0.0".to_string(),
        }
    }

    /// Load locked configuration from a specific level
    ///
    /// # Errors
    ///
    /// Returns an error if reading or parsing the lock file fails.
    pub async fn load_from_level(level: ConfigLevel) -> Result<Option<Self>> {
        let path = Self::lock_file_path_for_level(level)?;

        if !path.exists() {
            return Ok(None);
        }

        let contents = fs::read_to_string(&path).await.map_err(|e| {
            Error::config(format!(
                "Failed to read {} lock file: {}",
                level.display_name(),
                e
            ))
        })?;

        let locked: LockedConfig = toml::from_str(&contents).map_err(|e| {
            Error::config(format!(
                "Failed to parse {} lock file: {}",
                level.display_name(),
                e
            ))
        })?;

        info!(
            "Loaded {} locks from {} level",
            locked.locks.len(),
            level.display_name()
        );
        Ok(Some(locked))
    }

    /// Save locked configuration to a specific level
    ///
    /// # Errors
    ///
    /// Returns an error if serialization fails or the file cannot be written.
    pub async fn save_to_level(&self, level: ConfigLevel) -> Result<()> {
        let path = Self::lock_file_path_for_level(level)?;

        // Ensure parent directory exists
        if let Some(parent) = path.parent() {
            fs::create_dir_all(parent).await.map_err(|e| {
                Error::config(format!(
                    "Failed to create directory for {} lock file: {}",
                    level.display_name(),
                    e
                ))
            })?;
        }

        let contents = toml::to_string_pretty(self)
            .map_err(|e| Error::config(format!("Failed to serialize lock file: {}", e)))?;

        fs::write(&path, contents).await.map_err(|e| {
            Error::config(format!(
                "Failed to write {} lock file: {}",
                level.display_name(),
                e
            ))
        })?;

        info!(
            "Saved {} lock file to {}",
            level.display_name(),
            path.display()
        );
        Ok(())
    }

    /// Get the lock file path for a specific level
    ///
    /// # Errors
    ///
    /// Returns an error if the path cannot be determined.
    pub fn lock_file_path_for_level(level: ConfigLevel) -> Result<PathBuf> {
        match level {
            ConfigLevel::System => Ok(PathBuf::from("/etc/ferrous-forge/locked.toml")),
            ConfigLevel::User => {
                let config_dir = dirs::config_dir()
                    .ok_or_else(|| Error::config("Could not find config directory"))?;
                Ok(config_dir.join("ferrous-forge").join("locked.toml"))
            }
            ConfigLevel::Project => Ok(PathBuf::from(".forge/locked.toml")),
        }
    }

    /// Check if a key is locked
    pub fn is_locked(&self, key: &str) -> bool {
        self.locks.contains_key(key)
    }

    /// Get lock entry for a key
    pub fn get_lock(&self, key: &str) -> Option<&LockEntry> {
        self.locks.get(key)
    }

    /// Lock a configuration key
    pub fn lock(&mut self, key: impl Into<String>, entry: LockEntry) {
        let key = key.into();
        self.locks.insert(key, entry);
    }

    /// Unlock a configuration key
    pub fn unlock(&mut self, key: &str) -> Option<LockEntry> {
        self.locks.remove(key)
    }

    /// List all locked keys
    pub fn list_locks(&self) -> Vec<(&String, &LockEntry)> {
        self.locks.iter().collect()
    }
}

/// Hierarchical lock manager that respects precedence
pub struct HierarchicalLockManager {
    /// System-level locks (lowest priority)
    system: Option<LockedConfig>,
    /// User-level locks
    user: Option<LockedConfig>,
    /// Project-level locks (highest priority)
    project: Option<LockedConfig>,
}

impl HierarchicalLockManager {
    /// Load locks from all levels
    ///
    /// # Errors
    ///
    /// Returns an error if reading or parsing any lock file fails.
    pub async fn load() -> Result<Self> {
        let system = LockedConfig::load_from_level(ConfigLevel::System).await?;
        let user = LockedConfig::load_from_level(ConfigLevel::User).await?;
        let project = LockedConfig::load_from_level(ConfigLevel::Project).await?;

        Ok(Self {
            system,
            user,
            project,
        })
    }

    /// Check if a key is locked at any level
    ///
    /// Returns the most specific lock (project > user > system)
    #[allow(clippy::collapsible_if)]
    pub fn is_locked(&self, key: &str) -> Option<(ConfigLevel, &LockEntry)> {
        // Check in order of precedence (highest first)
        if let Some(project) = &self.project {
            if let Some(entry) = project.get_lock(key) {
                return Some((ConfigLevel::Project, entry));
            }
        }

        if let Some(user) = &self.user {
            if let Some(entry) = user.get_lock(key) {
                return Some((ConfigLevel::User, entry));
            }
        }

        if let Some(system) = &self.system {
            if let Some(entry) = system.get_lock(key) {
                return Some((ConfigLevel::System, entry));
            }
        }

        None
    }

    /// Check if a specific level has a lock
    pub fn is_locked_at_level(&self, key: &str, level: ConfigLevel) -> Option<&LockEntry> {
        let locks = match level {
            ConfigLevel::System => self.system.as_ref(),
            ConfigLevel::User => self.user.as_ref(),
            ConfigLevel::Project => self.project.as_ref(),
        };

        locks.and_then(|l| l.get_lock(key))
    }

    /// Get all locks merged with proper precedence
    pub fn get_effective_locks(&self) -> HashMap<String, (ConfigLevel, LockEntry)> {
        let mut effective = HashMap::new();

        // Apply in order of precedence (lowest to highest)
        if let Some(system) = &self.system {
            for (key, entry) in &system.locks {
                effective.insert(key.clone(), (ConfigLevel::System, entry.clone()));
            }
        }

        if let Some(user) = &self.user {
            for (key, entry) in &user.locks {
                effective.insert(key.clone(), (ConfigLevel::User, entry.clone()));
            }
        }

        if let Some(project) = &self.project {
            for (key, entry) in &project.locks {
                effective.insert(key.clone(), (ConfigLevel::Project, entry.clone()));
            }
        }

        effective
    }

    /// Lock a key at a specific level
    ///
    /// # Errors
    ///
    /// Returns an error if saving the lock file fails.
    #[allow(clippy::collapsible_if)]
    pub async fn lock(
        &mut self,
        key: impl Into<String>,
        value: impl Into<String>,
        reason: impl Into<String>,
        level: ConfigLevel,
    ) -> Result<()> {
        let key = key.into();
        let entry = LockEntry::new(value, reason, level);

        // Check if already locked at same or higher level
        if let Some((existing_level, _)) = self.is_locked(&key) {
            if existing_level >= level {
                warn!(
                    "Key '{}' is already locked at {} level",
                    key,
                    existing_level.display_name()
                );
                return Err(Error::config(format!(
                    "Key '{}' is already locked at {} level",
                    key,
                    existing_level.display_name()
                )));
            }
        }

        // Get or create the config for this level
        let locks = match level {
            ConfigLevel::System => &mut self.system,
            ConfigLevel::User => &mut self.user,
            ConfigLevel::Project => &mut self.project,
        };

        if locks.is_none() {
            *locks = Some(LockedConfig::new());
        }

        if let Some(config) = locks {
            config.lock(key.clone(), entry);
            config.save_to_level(level).await?;

            info!("Locked key '{}' at {} level", key, level.display_name());
        }

        Ok(())
    }

    /// Unlock a key at a specific level
    ///
    /// # Errors
    ///
    /// Returns an error if the key is not locked at this level or if saving fails.
    pub async fn unlock(
        &mut self,
        key: &str,
        level: ConfigLevel,
        reason: &str,
    ) -> Result<LockEntry> {
        // Verify the lock exists at this exact level
        let locks = match level {
            ConfigLevel::System => &mut self.system,
            ConfigLevel::User => &mut self.user,
            ConfigLevel::Project => &mut self.project,
        };

        let config = locks.as_mut().ok_or_else(|| {
            Error::config(format!(
                "No locks defined at {} level",
                level.display_name()
            ))
        })?;

        let entry = config.unlock(key).ok_or_else(|| {
            Error::config(format!(
                "Key '{}' is not locked at {} level",
                key,
                level.display_name()
            ))
        })?;

        // Save the updated locks
        config.save_to_level(level).await?;

        info!(
            "Unlocked key '{}' at {} level. Reason: {}",
            key,
            level.display_name(),
            reason
        );

        // Audit log the unlock operation
        audit_log::log_unlock(key, &entry, level, reason).await?;

        Ok(entry)
    }

    /// Get lock status report
    pub fn status_report(&self) -> String {
        let mut report = String::from("Configuration Lock Status:\n\n");

        let effective = self.get_effective_locks();

        if effective.is_empty() {
            report.push_str("No configuration values are currently locked.\n");
            return report;
        }

        report.push_str(&format!("Total locked keys: {}\n\n", effective.len()));

        for (key, (level, entry)) in effective {
            report.push_str(&format!(
                "  {}: {} (locked at {} level)\n",
                key,
                entry.value,
                level.display_name()
            ));
            report.push_str(&format!(
                "    Locked by: {} at {}\n",
                entry.locked_by, entry.locked_at
            ));
            report.push_str(&format!("    Reason: {}\n\n", entry.reason));
        }

        report
    }
}

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

    #[test]
    fn test_lock_entry_creation() {
        let entry = LockEntry::new("2024", "Required for project", ConfigLevel::Project);
        assert_eq!(entry.value, "2024");
        assert_eq!(entry.reason, "Required for project");
        assert_eq!(entry.level, ConfigLevel::Project);
    }

    #[test]
    fn test_locked_config_lock_unlock() {
        let mut config = LockedConfig::new();
        assert!(!config.is_locked("edition"));

        let entry = LockEntry::new("2024", "Required", ConfigLevel::User);
        config.lock("edition", entry.clone());
        assert!(config.is_locked("edition"));
        assert_eq!(config.get_lock("edition").unwrap().value, "2024");

        let removed = config.unlock("edition");
        assert!(removed.is_some());
        assert!(!config.is_locked("edition"));
    }

    #[test]
    fn test_locked_config_list_locks() {
        let mut config = LockedConfig::new();
        let entry1 = LockEntry::new("2024", "Required", ConfigLevel::User);
        let entry2 = LockEntry::new("1.85", "Required", ConfigLevel::User);

        config.lock("edition", entry1);
        config.lock("rust-version", entry2);

        let locks = config.list_locks();
        assert_eq!(locks.len(), 2);
    }

    #[test]
    fn test_hierarchical_lock_precedence() {
        let mut system = LockedConfig::new();
        let mut user = LockedConfig::new();
        let mut project = LockedConfig::new();

        system.lock(
            "edition",
            LockEntry::new("2021", "System default", ConfigLevel::System),
        );
        user.lock(
            "edition",
            LockEntry::new("2024", "User preference", ConfigLevel::User),
        );
        project.lock(
            "rust-version",
            LockEntry::new("1.88", "Project requirement", ConfigLevel::Project),
        );

        let manager = HierarchicalLockManager {
            system: Some(system),
            user: Some(user),
            project: Some(project),
        };

        // Project-level should override system/user for edition
        let result = manager.is_locked("edition");
        assert!(result.is_some());
        let (level, entry) = result.unwrap();
        assert_eq!(level, ConfigLevel::User);
        assert_eq!(entry.value, "2024");

        // Project should take precedence for rust-version
        let result = manager.is_locked("rust-version");
        assert!(result.is_some());
        let (level, entry) = result.unwrap();
        assert_eq!(level, ConfigLevel::Project);
        assert_eq!(entry.value, "1.88");
    }
}