]> git.lizzy.rs Git - rust.git/blob - src/config/summary.rs
format code and tests
[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 fn mark_parse_time(&mut self) {
35         self.timer = self.timer.done_parsing();
36     }
37
38     pub 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 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 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 fn add_parsing_error(&mut self) {
81         self.has_parsing_errors = true;
82     }
83
84     pub fn add_formatting_error(&mut self) {
85         self.has_formatting_errors = true;
86     }
87
88     pub 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 || self.has_parsing_errors || self.has_formatting_errors
94             || self.has_diff)
95     }
96
97     pub fn add(&mut self, other: Summary) {
98         self.has_operational_errors |= other.has_operational_errors;
99         self.has_formatting_errors |= other.has_formatting_errors;
100         self.has_parsing_errors |= other.has_parsing_errors;
101         self.has_diff |= other.has_diff;
102     }
103
104     pub fn print_exit_codes() {
105         let exit_codes = r#"Exit Codes:
106     0 = No errors
107     1 = Encountered operational errors e.g. an IO error
108     2 = Failed to reformat code because of parsing errors
109     3 = Code is valid, but it is impossible to format it properly
110     4 = Formatted code differs from existing code (write-mode diff only)"#;
111         println!("{}", exit_codes);
112     }
113 }
114
115 #[derive(Clone, Copy, Debug)]
116 enum Timer {
117     Initialized(Instant),
118     DoneParsing(Instant, Instant),
119     DoneFormatting(Instant, Instant, Instant),
120 }
121
122 impl Default for Timer {
123     fn default() -> Self {
124         Timer::Initialized(Instant::now())
125     }
126 }
127
128 impl Timer {
129     fn done_parsing(self) -> Self {
130         match self {
131             Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
132             _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
133         }
134     }
135
136     fn done_formatting(self) -> Self {
137         match self {
138             Timer::DoneParsing(init_time, parse_time) => {
139                 Timer::DoneFormatting(init_time, parse_time, Instant::now())
140             }
141             _ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),
142         }
143     }
144 }