ralph_workflow/pipeline/
mod.rs1#![deny(unsafe_code)]
25
26mod clipboard;
27pub mod idle_timeout;
28pub mod logfile;
29mod prompt;
30mod types;
31
32pub use prompt::{
33 extract_error_identifier_from_logfile, extract_error_message_from_logfile, run_with_prompt,
34 PipelineRuntime, PromptCommand,
35};
36pub use types::AgentPhaseGuard;
37
38use std::time::{Duration, Instant};
41
42#[derive(Clone)]
44pub struct Timer {
45 start_time: Instant,
46 phase_start: Instant,
47}
48
49impl Timer {
50 #[must_use]
52 pub fn new() -> Self {
53 let now = Instant::now();
54 Self {
55 start_time: now,
56 phase_start: now,
57 }
58 }
59
60 pub fn start_phase(&mut self) {
62 self.phase_start = Instant::now();
63 }
64
65 #[must_use]
67 pub fn elapsed(&self) -> Duration {
68 self.start_time.elapsed()
69 }
70
71 #[must_use]
73 pub fn phase_elapsed(&self) -> Duration {
74 self.phase_start.elapsed()
75 }
76
77 #[must_use]
79 pub fn format_duration(duration: Duration) -> String {
80 let total_secs = duration.as_secs();
81 let mins = total_secs / 60;
82 let secs = total_secs % 60;
83 format!("{mins}m {secs:02}s")
84 }
85
86 #[must_use]
88 pub fn elapsed_formatted(&self) -> String {
89 Self::format_duration(self.elapsed())
90 }
91
92 #[must_use]
94 pub fn phase_elapsed_formatted(&self) -> String {
95 Self::format_duration(self.phase_elapsed())
96 }
97}
98
99impl Default for Timer {
100 fn default() -> Self {
101 Self::new()
102 }
103}
104
105#[cfg(test)]
106mod timer_tests {
107 use super::*;
108
109 #[test]
110 fn test_format_duration_zero() {
111 let d = Duration::from_secs(0);
112 assert_eq!(Timer::format_duration(d), "0m 00s");
113 }
114
115 #[test]
116 fn test_format_duration_seconds() {
117 let d = Duration::from_secs(30);
118 assert_eq!(Timer::format_duration(d), "0m 30s");
119 }
120
121 #[test]
122 fn test_format_duration_minutes() {
123 let d = Duration::from_secs(65);
124 assert_eq!(Timer::format_duration(d), "1m 05s");
125 }
126
127 #[test]
128 fn test_format_duration_large() {
129 let d = Duration::from_secs(3661);
130 assert_eq!(Timer::format_duration(d), "61m 01s");
131 }
132}