]> git.lizzy.rs Git - rust.git/blob - src/config/summary.rs
Merge pull request #2681 from topecongiro/issue-2680
[rust.git] / src / config / summary.rs
1 // Copyright 2018 The Rust Project Developers. See the COPYRIGHT
2 // file at the top-level directory of this distribution and at
3 // http://rust-lang.org/COPYRIGHT.
4 //
5 // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6 // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7 // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8 // option. This file may not be copied, modified, or distributed
9 // except according to those terms.
10
11 use std::default::Default;
12 use std::time::{Duration, Instant};
13
14 #[must_use]
15 #[derive(Debug, Default, Clone, Copy)]
16 pub struct Summary {
17     // Encountered e.g. an IO error.
18     has_operational_errors: bool,
19
20     // Failed to reformat code because of parsing errors.
21     has_parsing_errors: bool,
22
23     // Code is valid, but it is impossible to format it properly.
24     has_formatting_errors: bool,
25
26     // Formatted code differs from existing code (write-mode diff only).
27     pub has_diff: bool,
28
29     // Keeps track of time spent in parsing and formatting steps.
30     timer: Timer,
31 }
32
33 impl Summary {
34     pub(crate) fn mark_parse_time(&mut self) {
35         self.timer = self.timer.done_parsing();
36     }
37
38     pub(crate) fn mark_format_time(&mut self) {
39         self.timer = self.timer.done_formatting();
40     }
41
42     /// Returns the time it took to parse the source files in nanoseconds.
43     pub(crate) fn get_parse_time(&self) -> Option<Duration> {
44         match self.timer {
45             Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
46                 // This should never underflow since `Instant::now()` guarantees monotonicity.
47                 Some(parse_time.duration_since(init))
48             }
49             Timer::Initialized(..) => None,
50         }
51     }
52
53     /// Returns the time it took to go from the parsed AST to the formatted output. Parsing time is
54     /// not included.
55     pub(crate) fn get_format_time(&self) -> Option<Duration> {
56         match self.timer {
57             Timer::DoneFormatting(_init, parse_time, format_time) => {
58                 Some(format_time.duration_since(parse_time))
59             }
60             Timer::DoneParsing(..) | Timer::Initialized(..) => None,
61         }
62     }
63
64     pub fn has_operational_errors(&self) -> bool {
65         self.has_operational_errors
66     }
67
68     pub fn has_parsing_errors(&self) -> bool {
69         self.has_parsing_errors
70     }
71
72     pub fn has_formatting_errors(&self) -> bool {
73         self.has_formatting_errors
74     }
75
76     pub fn add_operational_error(&mut self) {
77         self.has_operational_errors = true;
78     }
79
80     pub(crate) fn add_parsing_error(&mut self) {
81         self.has_parsing_errors = true;
82     }
83
84     pub(crate) fn add_formatting_error(&mut self) {
85         self.has_formatting_errors = true;
86     }
87
88     pub(crate) fn add_diff(&mut self) {
89         self.has_diff = true;
90     }
91
92     pub fn has_no_errors(&self) -> bool {
93         !(self.has_operational_errors
94             || self.has_parsing_errors
95             || self.has_formatting_errors
96             || self.has_diff)
97     }
98
99     pub fn add(&mut self, other: Summary) {
100         self.has_operational_errors |= other.has_operational_errors;
101         self.has_formatting_errors |= other.has_formatting_errors;
102         self.has_parsing_errors |= other.has_parsing_errors;
103         self.has_diff |= other.has_diff;
104     }
105
106     pub fn print_exit_codes() {
107         let exit_codes = r#"Exit Codes:
108     0 = No errors
109     1 = Encountered error in formatting code"#;
110         println!("{}", exit_codes);
111     }
112 }
113
114 #[derive(Clone, Copy, Debug)]
115 enum Timer {
116     Initialized(Instant),
117     DoneParsing(Instant, Instant),
118     DoneFormatting(Instant, Instant, Instant),
119 }
120
121 impl Default for Timer {
122     fn default() -> Self {
123         Timer::Initialized(Instant::now())
124     }
125 }
126
127 impl Timer {
128     fn done_parsing(self) -> Self {
129         match self {
130             Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
131             _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
132         }
133     }
134
135     fn done_formatting(self) -> Self {
136         match self {
137             Timer::DoneParsing(init_time, parse_time) => {
138                 Timer::DoneFormatting(init_time, parse_time, Instant::now())
139             }
140             _ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),
141         }
142     }
143 }