climer/timer/
builder.rs

1use super::Output;
2use super::Timer;
3use crate::error::ClimerResult;
4#[cfg(feature = "parser")]
5use crate::time::parser::parse_time;
6use crate::time::Time;
7
8/// The builder struct for `Timer`.
9#[derive(Default)]
10pub struct TimerBuilder {
11    time: Option<Time>,
12    #[cfg(feature = "parser")]
13    time_str: Option<String>,
14    start_time: Option<Time>,
15    quiet: bool,
16    format: Option<String>,
17    output_format: Option<String>,
18    print_interval: Option<Time>,
19    write: Option<String>,
20    continue_after_finish: bool,
21}
22
23impl TimerBuilder {
24    /// Set the `time` as a `Time`.
25    pub fn time(mut self, time: Time) -> Self {
26        self.time = Some(time);
27        self
28    }
29
30    /// Set the `time` as a string.
31    #[cfg(feature = "parser")]
32    pub fn time_str<T>(mut self, time_str: T) -> Self
33    where
34        T: ToString,
35    {
36        self.time_str = Some(time_str.to_string());
37        self
38    }
39
40    /// Set the `start_time` `Time`.
41    pub fn start_time(mut self, start_time: Time) -> Self {
42        self.start_time = Some(start_time);
43        self
44    }
45
46    /// Set the `quiet` flag, used for printing to stdout.
47    pub fn quiet(mut self, quiet: bool) -> Self {
48        self.quiet = quiet;
49        self
50    }
51
52    /// TODO: Unimplemented
53    /// Set the `format` string, used for parsing the given time string.
54    pub fn format<T>(mut self, format: T) -> Self
55    where
56        T: ToString,
57    {
58        self.format = Some(format.to_string());
59        self
60    }
61
62    /// Set the interval in which output should be printed to stdout.
63    pub fn print_interval(mut self, print_interval: Time) -> Self {
64        self.print_interval = Some(print_interval);
65        self
66    }
67
68    /// Set a file for the output to be written to, instead of stdout.
69    pub fn write<T>(mut self, write: T) -> Self
70    where
71        T: ToString,
72    {
73        self.write = Some(write.to_string());
74        self
75    }
76
77    /// Specify if timer should continue timing into negative time after it finishes.
78    pub fn continue_after_finish(
79        mut self,
80        continue_after_finish: bool,
81    ) -> Self {
82        self.continue_after_finish = continue_after_finish;
83        self
84    }
85
86    /// Build a `Timer`.
87    pub fn build(mut self) -> ClimerResult<Timer> {
88        let time = self.build_time()?;
89
90        let start_time = self.start_time;
91        let continue_after_finish = self.continue_after_finish;
92
93        let output = self.build_output();
94        let mut timer = Timer::new(time, output);
95
96        if let Some(start_time) = start_time {
97            timer.set_start_time(start_time);
98        }
99
100        timer.set_continue_after_finish(continue_after_finish);
101
102        Ok(timer)
103    }
104
105    fn build_time(&mut self) -> ClimerResult<Option<Time>> {
106        Ok(self.time.take().or(self.time_from_str()?))
107    }
108
109    #[cfg(feature = "parser")]
110    fn time_from_str(&self) -> ClimerResult<Option<Time>> {
111        if let Some(time_str) = self.time_str.as_ref() {
112            Ok(Some(parse_time(time_str, self.format.as_ref())?))
113        } else {
114            Ok(None)
115        }
116    }
117
118    #[cfg(not(feature = "parser"))]
119    fn time_from_str(&self) -> ClimerResult<Option<Time>> {
120        Ok(None)
121    }
122
123    /// Build the `Output` used by the `Timer`.
124    fn build_output(self) -> Option<Output> {
125        if self.quiet {
126            None
127        } else {
128            Some(Output::new(
129                self.output_format,
130                self.print_interval,
131                self.write,
132            ))
133        }
134    }
135}