raz-override 0.2.4

Override management system for raz with stable key generation
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
use crate::{OverrideEntry, OverrideError, OverrideSystem, ResolutionStrategy, Result};
use colored::*;
use std::path::Path;

/// Inspector for debugging and examining overrides
pub struct OverrideInspector {
    system: OverrideSystem,
}

impl OverrideInspector {
    /// Create a new inspector for a workspace
    pub fn new(workspace_path: &Path) -> Result<Self> {
        let system = OverrideSystem::new(workspace_path)?;
        Ok(Self { system })
    }

    /// List all overrides with detailed information
    pub fn list_all_detailed(&self) -> Result<()> {
        let hierarchical_overrides = self.system.storage.list_all_hierarchical()?;

        if hierarchical_overrides.is_empty() {
            println!("{}", "No overrides found.".yellow());
            return Ok(());
        }

        let total_count: usize = hierarchical_overrides
            .iter()
            .map(|(_, entries)| entries.len())
            .sum();

        println!(
            "{}",
            format!("Found {total_count} overrides across all configuration levels:").bold()
        );
        println!();

        let mut idx = 1;
        for (level, entries) in hierarchical_overrides {
            if !entries.is_empty() {
                println!(
                    "{}",
                    format!("{:?} Level ({} overrides):", level, entries.len())
                        .bold()
                        .blue()
                );
                println!();

                for entry in entries {
                    self.print_override_entry(idx, &entry);
                    idx += 1;
                }
                println!();
            }
        }

        Ok(())
    }

    /// List overrides for a specific file
    pub fn list_by_file(&self, file_path: &Path) -> Result<()> {
        let overrides = self.system.list_by_file(file_path)?;

        if overrides.is_empty() {
            println!(
                "{}",
                format!("No overrides found for file: {}", file_path.display()).yellow()
            );
            return Ok(());
        }

        println!(
            "{}",
            format!(
                "Found {} overrides for {}:",
                overrides.len(),
                file_path.display()
            )
            .bold()
        );
        println!();

        for (idx, entry) in overrides.iter().enumerate() {
            self.print_override_entry(idx + 1, entry);
        }

        Ok(())
    }

    /// Show detailed information about a specific override
    pub fn inspect_override(&self, key: &str) -> Result<()> {
        let overrides = self.system.list_all()?;

        let entry = overrides
            .iter()
            .find(|e| e.key.primary == key || e.key.fallbacks.contains(&key.to_string()))
            .ok_or_else(|| OverrideError::OverrideNotFound(key.to_string()))?;

        println!("{}", "Override Details:".bold().blue());
        println!();

        // Key information
        println!("{}", "Keys:".bold());
        println!("  Primary: {}", entry.key.primary.green());
        if !entry.key.fallbacks.is_empty() {
            println!("  Fallbacks:");
            for fallback in &entry.key.fallbacks {
                println!("    - {}", fallback.cyan());
            }
        }
        println!();

        // Metadata
        println!("{}", "Metadata:".bold());
        println!(
            "  File: {}",
            entry.metadata.file_path.display().to_string().blue()
        );
        if let Some(func_name) = &entry.metadata.function_name {
            println!("  Function: {}", func_name.yellow());
        }
        if let Some(line) = entry.metadata.original_line {
            println!("  Original Line: {}", line.to_string().magenta());
        }
        println!(
            "  Created: {}",
            entry.metadata.created_at.format("%Y-%m-%d %H:%M:%S UTC")
        );
        println!(
            "  Modified: {}",
            entry.metadata.modified_at.format("%Y-%m-%d %H:%M:%S UTC")
        );
        if let Some(notes) = &entry.metadata.notes {
            println!("  Notes: {notes}");
        }
        println!();

        // Override configuration
        println!("{}", "Override Configuration:".bold());
        println!("  Key: {}", entry.override_config.key.green());

        if !entry.override_config.env.is_empty() {
            println!("  Environment Variables:");
            for (key, value) in &entry.override_config.env {
                println!("    {} = {}", key.cyan(), value);
            }
        }

        if !entry.override_config.cargo_options.is_empty() {
            println!("  Cargo Options:");
            for opt in &entry.override_config.cargo_options {
                println!("    {opt}");
            }
        }

        if !entry.override_config.rustc_options.is_empty() {
            println!("  Rustc Options:");
            for opt in &entry.override_config.rustc_options {
                println!("    {opt}");
            }
        }

        if !entry.override_config.args.is_empty() {
            println!("  Additional Arguments:");
            for arg in &entry.override_config.args {
                println!("    {arg}");
            }
        }

        Ok(())
    }

    /// Debug resolution process for a given context
    pub fn debug_resolution(
        &mut self,
        file_path: &Path,
        line: usize,
        column: Option<usize>,
    ) -> Result<()> {
        let context = self.system.get_function_context(file_path, line, column)?;

        println!("{}", "Resolution Debug Information:".bold().blue());
        println!();

        // Context information
        println!("{}", "Context:".bold());
        println!("  File: {}", context.file_path.display().to_string().blue());
        println!("  Line: {}", line.to_string().magenta());
        if let Some(col) = column {
            println!("  Column: {}", col.to_string().magenta());
        }
        if let Some(func_name) = &context.function_name {
            println!("  Detected Function: {}", func_name.yellow());
        } else {
            println!("  Detected Function: {}", "None".red());
        }
        println!();

        // Resolution candidates
        let candidates = self.system.debug_resolution(&context)?;

        if candidates.is_empty() {
            println!("{}", "No resolution candidates found.".yellow());
            return Ok(());
        }

        println!(
            "{}",
            format!("Found {} resolution candidates:", candidates.len()).bold()
        );
        println!();

        for (idx, (entry, strategy, score)) in candidates.iter().enumerate() {
            println!("{}. {} (score: {:.2})", idx + 1, entry.key.primary, score);
            println!("   Strategy: {}", self.format_strategy(strategy));
            println!(
                "   Function: {}",
                entry.metadata.function_name.as_deref().unwrap_or("N/A")
            );
            if let Some(line) = entry.metadata.original_line {
                println!("   Original Line: {line}");
            }
            println!();
        }

        // Final resolution result
        if let Some((override_config, strategy)) = self.system.resolve_with_strategy(&context)? {
            println!("{}", "Final Resolution:".bold().green());
            println!("  Override Key: {}", override_config.key);
            println!("  Strategy: {}", self.format_strategy(&strategy));
        } else {
            println!("{}", "No override resolved.".red());
        }

        Ok(())
    }

    /// Export overrides for inspection
    pub fn export_overrides(&self, output_path: Option<&Path>) -> Result<()> {
        let export_data = self.system.export()?;

        if let Some(path) = output_path {
            std::fs::write(path, &export_data)?;
            println!(
                "{}",
                format!("Exported overrides to: {}", path.display()).green()
            );
        } else {
            println!("{}", "Exported Override Data:".bold());
            println!("{export_data}");
        }

        Ok(())
    }

    /// Show statistics about overrides
    pub fn show_statistics(&self) -> Result<()> {
        let overrides = self.system.list_all()?;

        println!("{}", "Override Statistics:".bold().blue());
        println!();

        println!("Total Overrides: {}", overrides.len());

        // Count by file
        let mut file_counts = std::collections::HashMap::new();
        for entry in &overrides {
            *file_counts
                .entry(entry.metadata.file_path.display().to_string())
                .or_insert(0) += 1;
        }

        println!();
        println!("{}", "Overrides by File:".bold());
        for (file, count) in file_counts.iter() {
            println!("  {}: {}", file.blue(), count);
        }

        // Count by function
        let function_count = overrides
            .iter()
            .filter(|e| e.metadata.function_name.is_some())
            .count();
        let file_level_count = overrides.len() - function_count;

        println!();
        println!("{}", "Override Types:".bold());
        println!("  Function-level: {function_count}");
        println!("  File-level: {file_level_count}");

        // Environment variables usage
        let mut env_vars = std::collections::HashSet::new();
        for entry in &overrides {
            for key in entry.override_config.env.keys() {
                env_vars.insert(key.clone());
            }
        }

        if !env_vars.is_empty() {
            println!();
            println!("{}", "Environment Variables Used:".bold());
            for var in env_vars {
                println!("  - {}", var.cyan());
            }
        }

        Ok(())
    }

    // Helper functions

    fn print_override_entry(&self, idx: usize, entry: &OverrideEntry) {
        println!("{}. {}", idx, entry.key.primary.green());
        println!(
            "   File: {}",
            entry.metadata.file_path.display().to_string().blue()
        );
        if let Some(func_name) = &entry.metadata.function_name {
            println!("   Function: {}", func_name.yellow());
        }
        if let Some(line) = entry.metadata.original_line {
            println!("   Line: {}", line.to_string().magenta());
        }
        println!("   Override: {}", entry.override_config.key);
        println!();
    }

    fn format_strategy(&self, strategy: &ResolutionStrategy) -> colored::ColoredString {
        match strategy {
            ResolutionStrategy::Exact => "Exact Key Match".green(),
            ResolutionStrategy::Fallback => "Fallback Key".cyan(),
            ResolutionStrategy::FuzzyFunction => "Fuzzy Function Match".yellow(),
            ResolutionStrategy::LineProximity => "Line Proximity".magenta(),
            ResolutionStrategy::HashFallback => "Hash Fallback".red(),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::{CommandOverride, FunctionContext};
    use tempfile::TempDir;

    #[test]
    fn test_inspector_list_all() {
        let temp_dir = TempDir::new().unwrap();
        let mut system = OverrideSystem::new(temp_dir.path()).unwrap();

        // Add some test overrides
        let context1 = FunctionContext {
            file_path: "src/main.rs".into(),
            function_name: Some("main".to_string()),
            line_number: 10,
            context: None,
        };

        let key1 = system.generate_key(&context1).unwrap();
        let mut override1 = CommandOverride::new("test1".to_string());
        override1.env.insert("DEBUG".to_string(), "1".to_string());

        system.save_override(key1, override1, &context1).unwrap();

        // Create inspector and test
        let inspector = OverrideInspector::new(temp_dir.path()).unwrap();

        // Should not panic
        inspector.list_all_detailed().unwrap();
    }

    #[test]
    fn test_inspector_debug_resolution() {
        let temp_dir = TempDir::new().unwrap();
        let mut system = OverrideSystem::new(temp_dir.path()).unwrap();

        // Create a test file
        let test_file = temp_dir.path().join("test.rs");
        std::fs::write(
            &test_file,
            r#"
fn process_data(input: &str) -> String {
    input.to_uppercase()
}

fn main() {
    let result = process_data("hello");
    println!("{}", result);
}
"#,
        )
        .unwrap();

        // Add override for process_data
        let context = FunctionContext {
            file_path: test_file.clone(),
            function_name: Some("process_data".to_string()),
            line_number: 2,
            context: None,
        };

        let key = system.generate_key(&context).unwrap();
        let override_config = CommandOverride::new("debug-test".to_string());
        system
            .save_override(key, override_config, &context)
            .unwrap();

        // Create inspector and debug resolution
        let mut inspector = OverrideInspector::new(temp_dir.path()).unwrap();

        // Should not panic
        inspector.debug_resolution(&test_file, 2, None).unwrap();
    }
}