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.
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.
11 use std::time::{Duration, Instant};
12 use std::default::Default;
15 #[derive(Debug, Default, Clone, Copy)]
17 // Encountered e.g. an IO error.
18 has_operational_errors: bool,
20 // Failed to reformat code because of parsing errors.
21 has_parsing_errors: bool,
23 // Code is valid, but it is impossible to format it properly.
24 has_formatting_errors: bool,
26 // Formatted code differs from existing code (write-mode diff only).
29 // Keeps track of time spent in parsing and formatting steps.
34 pub fn mark_parse_time(&mut self) {
35 self.timer = self.timer.done_parsing();
38 pub fn mark_format_time(&mut self) {
39 self.timer = self.timer.done_formatting();
42 /// Returns the time it took to parse the source files in nanoseconds.
43 pub fn get_parse_time(&self) -> Option<Duration> {
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))
49 Timer::Initialized(..) => None,
53 /// Returns the time it took to go from the parsed AST to the formatted output. Parsing time is
55 pub fn get_format_time(&self) -> Option<Duration> {
57 Timer::DoneFormatting(_init, parse_time, format_time) => {
58 Some(format_time.duration_since(parse_time))
60 Timer::DoneParsing(..) | Timer::Initialized(..) => None,
64 pub fn has_operational_errors(&self) -> bool {
65 self.has_operational_errors
68 pub fn has_parsing_errors(&self) -> bool {
69 self.has_parsing_errors
72 pub fn has_formatting_errors(&self) -> bool {
73 self.has_formatting_errors
76 pub fn add_operational_error(&mut self) {
77 self.has_operational_errors = true;
80 pub fn add_parsing_error(&mut self) {
81 self.has_parsing_errors = true;
84 pub fn add_formatting_error(&mut self) {
85 self.has_formatting_errors = true;
88 pub fn add_diff(&mut self) {
92 pub fn has_no_errors(&self) -> bool {
93 !(self.has_operational_errors || self.has_parsing_errors || self.has_formatting_errors
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;
104 pub fn print_exit_codes() {
105 let exit_codes = r#"Exit Codes:
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);
115 #[derive(Clone, Copy, Debug)]
117 Initialized(Instant),
118 DoneParsing(Instant, Instant),
119 DoneFormatting(Instant, Instant, Instant),
122 impl Default for Timer {
123 fn default() -> Self {
124 Timer::Initialized(Instant::now())
129 fn done_parsing(self) -> Self {
131 Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
132 _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
136 fn done_formatting(self) -> Self {
138 Timer::DoneParsing(init_time, parse_time) => {
139 Timer::DoneFormatting(init_time, parse_time, Instant::now())
141 _ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),