mod colorize;
pub(crate) use colorize::colorize_debug_text;
use std::{fmt, str::FromStr};
use std::io::IsTerminal;
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum DebugDetail {
Summary,
#[default]
Normal,
Verbose,
}
impl DebugDetail {
pub const fn as_str(self) -> &'static str {
match self {
Self::Summary => "summary",
Self::Normal => "normal",
Self::Verbose => "verbose",
}
}
}
impl fmt::Display for DebugDetail {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for DebugDetail {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"summary" => Ok(Self::Summary),
"normal" => Ok(Self::Normal),
"verbose" => Ok(Self::Verbose),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, Copy, Default, Eq, PartialEq)]
pub enum DebugColorMode {
#[default]
Auto,
Always,
Never,
}
impl DebugColorMode {
pub const fn as_str(self) -> &'static str {
match self {
Self::Auto => "auto",
Self::Always => "always",
Self::Never => "never",
}
}
pub(crate) fn enabled(self) -> bool {
match self {
Self::Auto => std::io::stdout().is_terminal(),
Self::Always => true,
Self::Never => false,
}
}
}
impl fmt::Display for DebugColorMode {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(self.as_str())
}
}
impl FromStr for DebugColorMode {
type Err = ();
fn from_str(value: &str) -> Result<Self, Self::Err> {
match value {
"auto" => Ok(Self::Auto),
"always" => Ok(Self::Always),
"never" => Ok(Self::Never),
_ => Err(()),
}
}
}
#[derive(Debug, Clone, Default, Eq, PartialEq)]
pub struct DebugFilters {
pub proto: Option<usize>,
}
pub fn format_display_set(items: impl IntoIterator<Item = impl fmt::Display>) -> String {
let formatted: Vec<String> = items.into_iter().map(|item| item.to_string()).collect();
if formatted.is_empty() {
"[-]".to_string()
} else {
format!("[{}]", formatted.join(", "))
}
}
#[cfg(test)]
mod tests;