#![cfg_attr(not(feature = "diagnostics"), allow(dead_code))]
use std::borrow::Cow;
#[cfg(feature = "diagnostics")]
fn colorize_value(value: &str) -> colored::ColoredString {
use colored::Colorize;
if value == "None" || value == "null" {
return value.red();
}
if value.starts_with("Some(") {
return value.magenta();
}
if value.starts_with("0x") || value.starts_with("0X") {
return value.cyan();
}
if value == "true" || value == "false" {
return value.magenta().bold();
}
if value.parse::<i64>().is_ok() || value.parse::<f64>().is_ok() {
return value.blue();
}
if (value.starts_with('"') && value.ends_with('"')) || (value.starts_with('\'') && value.ends_with('\'')) {
return value.green();
}
value.yellow()
}
#[derive(Debug, Clone)]
pub struct Diagnostic<'a> {
pub name: Cow<'a, str>,
pub args: Vec<(Cow<'a, str>, String)>,
pub result: Option<String>,
}
impl<'a> Diagnostic<'a> {
#[cfg(feature = "diagnostics")]
pub fn print_colored(&self) {
use colored::Colorize;
let timestamp = chrono::Utc::now().format("%H:%M:%S%.6f").to_string();
let level = "<DEBUG>".green().bold();
let message = "function has called".white().bold();
println!("[{timestamp}] {level} {message}");
println!(" ╭─[ {} ]", self.name);
for (arg_name, arg_value) in &self.args {
println!(" │ {}: {}", arg_name, colorize_value(arg_value));
}
println!("{}", " ◇".blue());
if let Some(ref result) = self.result {
println!(" ╰─► {}", colorize_value(result));
}
println!();
}
}
pub struct DiagnosticBuilder<'a> {
name: Cow<'a, str>,
args: Vec<(Cow<'a, str>, String)>,
result: Option<String>,
emitted: bool,
}
impl<'a> DiagnosticBuilder<'a> {
pub fn new() -> Self {
Self {
name: Cow::Borrowed(""),
args: Vec::new(),
result: None,
emitted: false,
}
}
pub fn set_name(&mut self, name: impl Into<Cow<'a, str>>) -> &mut Self {
self.name = name.into();
self
}
pub fn add_arg(&mut self, name: impl Into<Cow<'a, str>>, value: impl ToString) -> &mut Self {
self.args.push((name.into(), value.to_string()));
self
}
pub fn set_result(&mut self, result: impl ToString) -> &mut Self {
self.result = Some(result.to_string());
self
}
pub fn build(&self) -> Diagnostic<'a> {
Diagnostic {
name: self.name.clone(),
args: self.args.clone(),
result: self.result.clone(),
}
}
#[cfg(feature = "diagnostics")]
pub fn emit(&mut self) {
self.emitted = true;
self.build().print_colored();
}
#[cfg(not(feature = "diagnostics"))]
#[inline(always)]
pub fn emit(&mut self) {
self.emitted = true;
}
}
impl Default for DiagnosticBuilder<'_> {
fn default() -> Self {
Self::new()
}
}
impl Drop for DiagnosticBuilder<'_> {
fn drop(&mut self) {
if !self.emitted {
self.emit();
}
}
}
#[cfg(feature = "diagnostics")]
pub(crate) fn emit(name: &'static str, args: &[(&'static str, String)], result: Option<&str>) {
let mut builder = DiagnosticBuilder::new();
builder.set_name(name);
for (arg, value) in args {
builder.add_arg(*arg, value);
}
if let Some(result) = result {
builder.set_result(result);
}
builder.emit();
}
macro_rules! diag {
($name:expr $(, $arg_name:expr => $arg_value:expr)* $(; result: $result:expr)? $(,)?) => {{
if cfg!(feature = "diagnostics") || $crate::observe::wants_details() {
let args: ::std::vec::Vec<(&'static str, ::std::string::String)> =
::std::vec![$(($arg_name, ::std::string::ToString::to_string(&$arg_value))),*];
let result: ::std::option::Option<::std::string::String> =
[$(::std::string::ToString::to_string(&$result))?].into_iter().next();
#[cfg(feature = "diagnostics")]
$crate::core::diagnostics::emit($name, &args, result.as_deref());
$crate::observe::suite_call_detailed($name, args, result);
} else {
$crate::observe::suite_call($name);
}
}};
}
pub(crate) use diag;