use std::default::Default;
use std::time::{Duration, Instant};
#[derive(Debug, Default, Clone, Copy)]
pub struct Summary {
has_operational_errors: bool,
has_parsing_errors: bool,
has_formatting_errors: bool,
has_check_errors: bool,
pub has_diff: bool,
timer: Timer,
}
impl Summary {
pub(crate) fn mark_parse_time(&mut self) {
self.timer = self.timer.done_parsing();
}
pub(crate) fn mark_format_time(&mut self) {
self.timer = self.timer.done_formatting();
}
pub(crate) fn get_parse_time(&self) -> Option<Duration> {
match self.timer {
Timer::DoneParsing(init, parse_time) | Timer::DoneFormatting(init, parse_time, _) => {
Some(parse_time.duration_since(init))
}
Timer::Initialized(..) => None,
}
}
pub(crate) fn get_format_time(&self) -> Option<Duration> {
match self.timer {
Timer::DoneFormatting(_init, parse_time, format_time) => {
Some(format_time.duration_since(parse_time))
}
Timer::DoneParsing(..) | Timer::Initialized(..) => None,
}
}
pub fn has_operational_errors(&self) -> bool {
self.has_operational_errors
}
pub fn has_parsing_errors(&self) -> bool {
self.has_parsing_errors
}
pub fn has_formatting_errors(&self) -> bool {
self.has_formatting_errors
}
pub fn has_check_errors(&self) -> bool {
self.has_check_errors
}
pub fn add_operational_error(&mut self) {
self.has_operational_errors = true;
}
pub(crate) fn add_parsing_error(&mut self) {
self.has_parsing_errors = true;
}
pub(crate) fn add_formatting_error(&mut self) {
self.has_formatting_errors = true;
}
pub(crate) fn add_check_error(&mut self) {
self.has_check_errors = true;
}
pub(crate) fn add_diff(&mut self) {
self.has_diff = true;
}
pub fn has_no_errors(&self) -> bool {
!(self.has_operational_errors
|| self.has_parsing_errors
|| self.has_formatting_errors
|| self.has_diff)
}
pub fn add(&mut self, other: Summary) {
self.has_operational_errors |= other.has_operational_errors;
self.has_formatting_errors |= other.has_formatting_errors;
self.has_parsing_errors |= other.has_parsing_errors;
self.has_check_errors |= other.has_check_errors;
self.has_diff |= other.has_diff;
}
}
#[derive(Clone, Copy, Debug)]
enum Timer {
Initialized(Instant),
DoneParsing(Instant, Instant),
DoneFormatting(Instant, Instant, Instant),
}
impl Default for Timer {
fn default() -> Self {
Timer::Initialized(Instant::now())
}
}
impl Timer {
fn done_parsing(self) -> Self {
match self {
Timer::Initialized(init_time) => Timer::DoneParsing(init_time, Instant::now()),
_ => panic!("Timer can only transition to DoneParsing from Initialized state"),
}
}
fn done_formatting(self) -> Self {
match self {
Timer::DoneParsing(init_time, parse_time) => {
Timer::DoneFormatting(init_time, parse_time, Instant::now())
}
_ => panic!("Timer can only transition to DoneFormatting from DoneParsing state"),
}
}
}