rch-common 1.0.26

Shared types and utilities for Remote Compilation Helper
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
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
//! CompilationProgress - Cargo build status visualization.
//!
//! Parses cargo output and renders compilation progress with:
//! - Crate count tracking (X/Y crates)
//! - Build phase indication (Compiling, Linking, Running tests)
//! - Rate calculation (crates/sec)
//! - Warning accumulation
//! - Optional memory usage display

use crate::ui::{Icons, OutputContext, ProgressContext};
use std::time::{Duration, Instant};

#[cfg(all(feature = "rich-ui", unix))]
use crate::ui::RchTheme;
#[cfg(all(feature = "rich-ui", unix))]
use rich_rust::prelude::{BarStyle, ProgressBar, Style};

const DEFAULT_BAR_WIDTH: usize = 28;
const RATE_SAMPLE_WINDOW: usize = 10;

/// Build phases during compilation.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BuildPhase {
    /// Initial state before any output.
    #[default]
    Starting,
    /// Compiling crates.
    Compiling,
    /// Running build scripts (build.rs).
    BuildScript,
    /// Linking final binary.
    Linking,
    /// Running tests.
    Testing,
    /// Running documentation tests.
    DocTest,
    /// Finished (success or failure).
    Finished,
}

impl BuildPhase {
    fn label(self) -> &'static str {
        match self {
            Self::Starting => "Starting",
            Self::Compiling => "Compiling",
            Self::BuildScript => "Build script",
            Self::Linking => "Linking",
            Self::Testing => "Testing",
            Self::DocTest => "Doc tests",
            Self::Finished => "Finished",
        }
    }

    fn icon(self, ctx: OutputContext) -> &'static str {
        match self {
            Self::Starting => Icons::hourglass(ctx),
            Self::Compiling => Icons::gear(ctx),
            Self::BuildScript => Icons::gear(ctx),
            Self::Linking => Icons::transfer(ctx),
            Self::Testing => Icons::clock(ctx),
            Self::DocTest => Icons::clock(ctx),
            Self::Finished => Icons::check(ctx),
        }
    }
}

/// Parsed information from a single cargo output line.
#[derive(Debug, Clone)]
pub struct CrateInfo {
    /// Crate name.
    pub name: String,
    /// Crate version (if available).
    pub version: Option<String>,
}

/// Build configuration detected from cargo output.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Default)]
pub enum BuildProfile {
    #[default]
    Debug,
    Release,
}

impl BuildProfile {
    #[allow(dead_code)] // Part of public API, may be used by consumers
    fn label(self) -> &'static str {
        match self {
            Self::Debug => "debug",
            Self::Release => "release",
        }
    }
}

/// Smoothed rate calculation for crates/sec.
#[derive(Debug, Default)]
struct RateSmoother {
    samples: Vec<f64>,
}

impl RateSmoother {
    fn push(&mut self, value: f64) {
        if value <= 0.0 || !value.is_finite() {
            return;
        }
        self.samples.push(value);
        if self.samples.len() > RATE_SAMPLE_WINDOW {
            self.samples.remove(0);
        }
    }

    fn average(&self) -> Option<f64> {
        if self.samples.is_empty() {
            return None;
        }
        let sum: f64 = self.samples.iter().sum();
        Some(sum / self.samples.len() as f64)
    }
}

/// Progress display for cargo compilation.
///
/// Parses cargo output lines and renders a compact progress display showing:
/// - Crate count (X/Y crates)
/// - Current crate being compiled
/// - Elapsed time and compilation rate
/// - Build phase (Compiling, Linking, Testing)
/// - Warning count accumulator
///
/// # Example
///
/// ```ignore
/// use rch_common::ui::{OutputContext, CompilationProgress};
///
/// let ctx = OutputContext::detect();
/// let mut progress = CompilationProgress::new(ctx, "worker1", false);
///
/// // Feed cargo output lines
/// progress.update_from_line("   Compiling serde v1.0.193");
/// progress.update_from_line("   Compiling serde_json v1.0.108");
/// progress.update_from_line("    Finished `release` profile [optimized] target(s) in 45.23s");
///
/// progress.finish();
/// ```
#[derive(Debug)]
pub struct CompilationProgress {
    ctx: OutputContext,
    worker: String,
    enabled: bool,
    progress: Option<ProgressContext>,
    start: Instant,
    phase: BuildPhase,
    profile: BuildProfile,
    crates_compiled: u32,
    crates_total: Option<u32>,
    current_crate: Option<CrateInfo>,
    warnings: u32,
    rate: RateSmoother,
    memory_mb: Option<u32>,
    last_crate_time: Instant,
    linking_start: Option<Instant>,
}

impl CompilationProgress {
    /// Create a new compilation progress display.
    pub fn new(ctx: OutputContext, worker: impl Into<String>, quiet: bool) -> Self {
        let enabled = !quiet && !ctx.is_machine();
        let progress = if enabled && matches!(ctx, OutputContext::Interactive) {
            Some(ProgressContext::new(ctx))
        } else {
            None
        };

        let now = Instant::now();
        Self {
            ctx,
            worker: worker.into(),
            enabled,
            progress,
            start: now,
            phase: BuildPhase::Starting,
            profile: BuildProfile::Debug,
            crates_compiled: 0,
            crates_total: None,
            current_crate: None,
            warnings: 0,
            rate: RateSmoother::default(),
            memory_mb: None,
            last_crate_time: now,
            linking_start: None,
        }
    }

    /// Update progress from a raw cargo output line.
    pub fn update_from_line(&mut self, line: &str) {
        let trimmed = line.trim();
        if trimmed.is_empty() {
            return;
        }

        // Parse the line and update state
        self.parse_line(trimmed);
        self.render();
    }

    /// Set the total number of crates to compile.
    ///
    /// If known ahead of time (e.g., from cargo metadata),
    /// this enables percentage display.
    pub fn set_total_crates(&mut self, total: u32) {
        self.crates_total = Some(total);
    }

    /// Set memory usage (in MB) for display.
    pub fn set_memory_mb(&mut self, mb: u32) {
        self.memory_mb = Some(mb);
    }

    /// Finish the progress display and print a summary line.
    pub fn finish(&mut self) {
        if let Some(progress) = &self.progress {
            progress.clear();
        }

        if !self.enabled {
            return;
        }

        let duration = self.start.elapsed();
        let icon = Icons::check(self.ctx);
        let crates = self.crates_compiled;
        let duration_str = format_duration(duration);
        let rate = self.rate.average().unwrap_or(0.0);
        let rate_str = if rate > 0.0 {
            format!("{rate:.1} crates/sec")
        } else {
            "--".to_string()
        };

        let warnings_str = if self.warnings > 0 {
            format!(
                ", {} warning{}",
                self.warnings,
                if self.warnings == 1 { "" } else { "s" }
            )
        } else {
            String::new()
        };

        eprintln!(
            "{icon} Build complete: {crates} crates in {duration_str} ({rate_str}{warnings_str})"
        );
    }

    /// Finish with a failure message.
    pub fn finish_error(&mut self, message: &str) {
        if let Some(progress) = &self.progress {
            progress.clear();
        }

        if !self.enabled {
            return;
        }

        let icon = Icons::cross(self.ctx);
        let duration = self.start.elapsed();
        let duration_str = format_duration(duration);

        eprintln!("{icon} Build failed after {duration_str}: {message}");
    }

    /// Get the current build phase.
    #[must_use]
    pub fn phase(&self) -> BuildPhase {
        self.phase
    }

    /// Get the number of crates compiled so far.
    #[must_use]
    pub fn crates_compiled(&self) -> u32 {
        self.crates_compiled
    }

    /// Get the warning count.
    #[must_use]
    pub fn warnings(&self) -> u32 {
        self.warnings
    }

    fn parse_line(&mut self, line: &str) {
        // Check for summary warning count FIRST (before individual warning check)
        if let Some(rest) = line.strip_prefix("warning: ") {
            if let Some(count_str) = rest.strip_suffix(" warnings emitted") {
                if let Ok(count) = count_str.parse::<u32>() {
                    self.warnings = count;
                    return;
                }
            } else if rest.ends_with(" warning emitted") {
                self.warnings = 1;
                return;
            }
            // Not a summary line, fall through to individual warning check
        }

        // Check for individual warning lines
        if line.contains("warning:") && !line.starts_with("warning:") {
            // Inline warning in crate output (e.g., "src/lib.rs:10: warning: ..."), don't count
        } else if line.starts_with("warning:") || line.contains(": warning") {
            self.warnings += 1;
            return;
        }

        // Detect "Compiling crate v1.2.3"
        if let Some(rest) = line.strip_prefix("Compiling ") {
            self.phase = BuildPhase::Compiling;
            self.parse_crate_info(rest);
            self.record_crate_compiled();
            return;
        }

        // Detect build script running
        if line.starts_with("Running `") && line.contains("build-script") {
            self.phase = BuildPhase::BuildScript;
            return;
        }

        // Detect linking phase
        if line.starts_with("Linking ") || line.contains("Linking ") {
            self.phase = BuildPhase::Linking;
            self.linking_start = Some(Instant::now());
            return;
        }

        // Detect test running
        if line.starts_with("Running ") && (line.contains("tests") || line.contains("test")) {
            self.phase = BuildPhase::Testing;
            return;
        }

        // Detect doc tests
        if line.contains("Doc-tests") {
            self.phase = BuildPhase::DocTest;
            return;
        }

        // Detect finished
        if line.starts_with("Finished ") {
            self.phase = BuildPhase::Finished;
            if line.contains("`release`") || line.contains("release") {
                self.profile = BuildProfile::Release;
            }
            return;
        }

        // Detect profile from early output
        if line.contains("--release") || line.contains("`release`") {
            self.profile = BuildProfile::Release;
        }

        // Parse fresh/dirty crate messages (cargo check/clippy)
        if let Some(rest) = line.strip_prefix("Checking ") {
            self.phase = BuildPhase::Compiling;
            self.parse_crate_info(rest);
            self.record_crate_compiled();
        }
    }

    fn parse_crate_info(&mut self, rest: &str) {
        // Format: "crate_name v1.2.3" or "crate_name v1.2.3 (path+...)"
        let parts: Vec<&str> = rest.split_whitespace().collect();
        if parts.is_empty() {
            return;
        }

        let name = parts[0].to_string();
        let version = parts.get(1).map(|v| {
            if v.starts_with('v') || v.starts_with('V') {
                v[1..].to_string()
            } else {
                (*v).to_string()
            }
        });

        self.current_crate = Some(CrateInfo { name, version });
    }

    fn record_crate_compiled(&mut self) {
        self.crates_compiled += 1;

        // Calculate rate
        let now = Instant::now();
        let elapsed_since_last = now.duration_since(self.last_crate_time).as_secs_f64();
        if elapsed_since_last > 0.0 {
            let rate = 1.0 / elapsed_since_last;
            self.rate.push(rate);
        }
        self.last_crate_time = now;
    }

    fn render(&mut self) {
        if !self.enabled {
            return;
        }

        let phase_icon = self.phase.icon(self.ctx);
        let worker = &self.worker;
        let elapsed = format_duration(self.start.elapsed());

        // Build progress bar
        let (bar, percent_str) = if let Some(total) = self.crates_total {
            let percent = if total > 0 {
                (self.crates_compiled as f64 / total as f64).clamp(0.0, 1.0)
            } else {
                0.0
            };
            let bar = render_bar(
                self.ctx,
                self.crates_compiled as u64,
                Some(total as u64),
                Some(percent),
                DEFAULT_BAR_WIDTH,
            );
            let pct = (percent * 100.0).round() as u32;
            (bar, format!("{pct}%"))
        } else {
            // Unknown total - show indeterminate progress
            let bar = render_bar(
                self.ctx,
                self.crates_compiled as u64,
                None,
                None,
                DEFAULT_BAR_WIDTH,
            );
            (bar, "??%".to_string())
        };

        // Crate counts
        let crates_str = if let Some(total) = self.crates_total {
            format!("{}/{} crates", self.crates_compiled, total)
        } else {
            format!("{} crates", self.crates_compiled)
        };

        // Rate
        let rate = self.rate.average().unwrap_or(0.0);
        let rate_str = if rate > 0.0 {
            format!("{rate:.1} crates/sec")
        } else {
            "--/sec".to_string()
        };

        // Current crate
        let current = self
            .current_crate
            .as_ref()
            .map(|c| {
                if let Some(v) = &c.version {
                    format!("{} v{v}", c.name)
                } else {
                    c.name.clone()
                }
            })
            .unwrap_or_else(|| "--".to_string());

        // Phase label
        let phase_label = self.phase.label();

        // Warning indicator
        let warnings_str = if self.warnings > 0 {
            let icon = Icons::warning(self.ctx);
            format!(" {icon} {}", self.warnings)
        } else {
            String::new()
        };

        // Memory indicator (reserved for future multi-line display)
        let _memory_str = self
            .memory_mb
            .map(|mb| format!(" [{mb}MB]"))
            .unwrap_or_default();

        // Linking duration (reserved for future multi-line display)
        let _linking_str = if self.phase == BuildPhase::Linking {
            if let Some(start) = self.linking_start {
                let dur = format_duration(start.elapsed());
                format!(" ({dur})")
            } else {
                String::new()
            }
        } else {
            String::new()
        };

        // For single-line rendering, compress to one line
        let compact_line = format!(
            "{phase_icon} Building on {worker}  {bar} {percent_str} | {crates_str} | {elapsed} | {rate_str}{warnings_str} | {phase_label}: {current}"
        );

        if let Some(progress) = &mut self.progress {
            progress.render(&compact_line);
        }
    }
}

#[cfg(all(feature = "rich-ui", unix))]
fn render_bar(
    ctx: OutputContext,
    current: u64,
    total: Option<u64>,
    percent: Option<f64>,
    width: usize,
) -> String {
    let mut bar = if let Some(total) = total {
        ProgressBar::with_total(total)
    } else {
        ProgressBar::new()
    };

    let bar_style = if ctx.supports_unicode() {
        BarStyle::Block
    } else {
        BarStyle::Ascii
    };

    let completed_style = Style::new()
        .color_str(RchTheme::SECONDARY)
        .unwrap_or_default();
    let remaining_style = Style::new().color_str("bright_black").unwrap_or_default();

    bar = bar
        .width(width)
        .bar_style(bar_style)
        .completed_style(completed_style)
        .remaining_style(remaining_style)
        .show_percentage(false)
        .show_eta(false)
        .show_speed(false)
        .show_elapsed(false);

    if total.is_some() {
        bar.update(current);
    } else if let Some(percent) = percent {
        bar.set_progress(percent);
    }

    bar.render_plain(width + 2).trim_end().to_string()
}

#[cfg(not(all(feature = "rich-ui", unix)))]
fn render_bar(
    ctx: OutputContext,
    _current: u64,
    _total: Option<u64>,
    percent: Option<f64>,
    width: usize,
) -> String {
    let progress = percent.unwrap_or(0.0).clamp(0.0, 1.0);
    let filled = (progress * width as f64).round() as usize;
    let empty = width.saturating_sub(filled);
    let filled_char = Icons::progress_filled(ctx);
    let empty_char = Icons::progress_empty(ctx);

    let mut bar = String::from("[");
    bar.push_str(&filled_char.repeat(filled));
    bar.push_str(&empty_char.repeat(empty));
    bar.push(']');
    bar
}

fn format_duration(duration: Duration) -> String {
    let total_secs = duration.as_secs();
    if total_secs < 60 {
        format!("{:.1}s", duration.as_secs_f64())
    } else if total_secs < 3600 {
        let mins = total_secs / 60;
        let secs = total_secs % 60;
        format!("{mins}:{secs:02}")
    } else {
        let hours = total_secs / 3600;
        let mins = (total_secs % 3600) / 60;
        let secs = total_secs % 60;
        format!("{hours}:{mins:02}:{secs:02}")
    }
}

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

    #[test]
    fn parse_compiling_line() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("   Compiling serde v1.0.193");

        assert_eq!(progress.crates_compiled(), 1);
        assert_eq!(progress.phase(), BuildPhase::Compiling);
        assert!(progress.current_crate.is_some());

        let crate_info = progress.current_crate.as_ref().unwrap();
        assert_eq!(crate_info.name, "serde");
        assert_eq!(crate_info.version.as_deref(), Some("1.0.193"));
    }

    #[test]
    fn parse_checking_line() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("    Checking rch-common v0.1.0 (/path/to/crate)");

        assert_eq!(progress.crates_compiled(), 1);
        assert_eq!(progress.phase(), BuildPhase::Compiling);
    }

    #[test]
    fn parse_finished_line() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("    Finished `release` profile [optimized] target(s) in 45.23s");

        assert_eq!(progress.phase(), BuildPhase::Finished);
        assert_eq!(progress.profile, BuildProfile::Release);
    }

    #[test]
    fn parse_warning_count() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("warning: 5 warnings emitted");

        assert_eq!(progress.warnings(), 5);
    }

    #[test]
    fn parse_single_warning() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("warning: 1 warning emitted");

        assert_eq!(progress.warnings(), 1);
    }

    #[test]
    fn parse_linking_phase() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("   Linking target/release/myapp");

        assert_eq!(progress.phase(), BuildPhase::Linking);
        assert!(progress.linking_start.is_some());
    }

    #[test]
    fn parse_testing_phase() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("     Running unittests src/lib.rs (target/debug/deps/rch-...)");

        assert_eq!(progress.phase(), BuildPhase::Testing);
    }

    #[test]
    fn multiple_crates_compiled() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", false);

        progress.update_from_line("   Compiling serde v1.0.193");
        progress.update_from_line("   Compiling serde_json v1.0.108");
        progress.update_from_line("   Compiling tokio v1.35.1");

        assert_eq!(progress.crates_compiled(), 3);
    }

    #[test]
    fn total_crates_percentage() {
        let ctx = OutputContext::Plain;
        let mut progress = CompilationProgress::new(ctx, "test-worker", true); // quiet mode

        progress.set_total_crates(100);
        progress.update_from_line("   Compiling crate1 v0.1.0");
        progress.update_from_line("   Compiling crate2 v0.1.0");

        assert_eq!(progress.crates_compiled(), 2);
        // 2/100 = 2%
    }

    #[test]
    fn format_duration_seconds() {
        let dur = Duration::from_secs_f64(45.7);
        assert_eq!(format_duration(dur), "45.7s");
    }

    #[test]
    fn format_duration_minutes() {
        let dur = Duration::from_secs(125);
        assert_eq!(format_duration(dur), "2:05");
    }

    #[test]
    fn format_duration_hours() {
        let dur = Duration::from_secs(3725);
        assert_eq!(format_duration(dur), "1:02:05");
    }

    #[test]
    fn rate_smoother_average() {
        let mut smoother = RateSmoother::default();
        smoother.push(1.0);
        smoother.push(2.0);
        smoother.push(3.0);

        let avg = smoother.average().unwrap();
        assert!((avg - 2.0).abs() < 0.001);
    }

    #[test]
    fn rate_smoother_ignores_invalid() {
        let mut smoother = RateSmoother::default();
        smoother.push(-1.0);
        smoother.push(f64::NAN);
        smoother.push(0.0);

        assert!(smoother.average().is_none());
    }
}