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::default::Default;
12 use std::time::{Duration, Instant};
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 (--check only).
29 // Keeps track of time spent in parsing and formatting steps.
34 pub(crate) fn mark_parse_time(&mut self) {
35 self.timer = self.timer.done_parsing();
38 pub(crate) 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(crate) 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(crate) 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(crate) fn add_parsing_error(&mut self) {
81 self.has_parsing_errors = true;
84 pub(crate) fn add_formatting_error(&mut self) {
85 self.has_formatting_errors = true;
88 pub(crate) fn add_diff(&mut self) {
92 pub fn has_no_errors(&self) -> bool {
93 !(self.has_operational_errors
94 || self.has_parsing_errors
95 || self.has_formatting_errors
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;
106 pub fn print_exit_codes() {
107 let exit_codes = r#"Exit Codes:
109 1 = Encountered error in formatting code"#;
110 println!("{}", exit_codes);
114 #[derive(Clone, Copy, Debug)]
116 Initialized(Instant),
117 DoneParsing(Instant, Instant),
118 DoneFormatting(Instant, Instant, Instant),
121 impl Default for Timer {
122 fn default() -> Self {
123 Timer::Initialized(Instant::now())
128 fn done_parsing(self) -> Self {
130 Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
131 _ => panic!("Timer can only transition to DoneParsing from Initialized state"),
135 fn done_formatting(self) -> Self {
137 Timer::DoneParsing(init_time, parse_time) => {
138 Timer::DoneFormatting(init_time, parse_time, Instant::now())
140 _ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),