use std::{error::Error, str::FromStr, time::Duration};
use regex::Regex;
#[derive(Clone, Copy)]
pub(crate) struct ParsedSeconds(pub Duration);
impl FromStr for ParsedSeconds {
type Err = Box<dyn Error + Send + Sync>;
fn from_str(s: &str) -> Result<Self, Self::Err> {
Ok(Self(Duration::try_from_secs_f64(f64::from_str(s)?)?))
}
}
#[derive(Clone, Copy, Default)]
pub(crate) enum Action {
#[default]
Bench,
Test,
List,
}
#[allow(dead_code)]
impl Action {
#[inline]
pub fn is_bench(&self) -> bool {
matches!(self, Self::Bench)
}
#[inline]
pub fn is_test(&self) -> bool {
matches!(self, Self::Test)
}
#[inline]
pub fn is_list(&self) -> bool {
matches!(self, Self::List)
}
}
pub(crate) enum Filter {
Regex(Regex),
Exact(String),
}
impl Filter {
pub fn is_match(&self, s: &str) -> bool {
match self {
Self::Regex(r) => r.is_match(s),
Self::Exact(e) => e == s,
}
}
}
#[derive(Copy, Clone, Default)]
pub(crate) enum RunIgnored {
#[default]
No,
Yes,
Only,
}
impl RunIgnored {
pub fn run_ignored(self) -> bool {
matches!(self, Self::Yes | Self::Only)
}
pub fn run_non_ignored(self) -> bool {
matches!(self, Self::Yes | Self::No)
}
pub fn should_run(self, ignored: bool) -> bool {
if ignored {
self.run_ignored()
} else {
self.run_non_ignored()
}
}
}
#[derive(Clone, Copy, Default)]
pub(crate) enum SortingAttr {
#[default]
Kind,
Name,
Location,
}
impl SortingAttr {
pub fn with_tie_breakers(self) -> [Self; 3] {
use SortingAttr::*;
match self {
Kind => [self, Name, Location],
Name => [self, Location, Kind],
Location => [self, Kind, Name],
}
}
}