From fd27541bbffc8623298680b2905712e108b8d79c Mon Sep 17 00:00:00 2001 From: nobody Date: Sun, 25 Aug 2024 23:29:43 +0200 Subject: [PATCH] move debug / util macros to lib.rs --- src/lib.rs | 84 +++++++++++++++++++++++++++++++++++++++++++++++++++++ src/main.rs | 77 +----------------------------------------------- 2 files changed, 85 insertions(+), 76 deletions(-) diff --git a/src/lib.rs b/src/lib.rs index 2ca3bcc..a3edc39 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -17,9 +17,93 @@ pub fn is_dry_run() -> bool { env::var("DRY_RUN").map(|v| !v.is_empty()).unwrap_or(false) } +/// Checks environment variable `VERBOSE` to see if status messages should be +/// printed. +/// +/// Use [`verboseln!`] to print stuff only if `VERBOSE` (or `TRACE`) is set. +pub fn is_verbose() -> bool { + env::var("VERBOSE").map(|v| !v.is_empty()).unwrap_or(false) +} +/// Like [`println!`], but only if [`is_verbose`] (or [`is_trace`]) is true (due to +/// the environment variable `VERBOSE` (or `TRACE`) being set.) +#[macro_export] +macro_rules! verboseln { + ($($arg:tt)*) => { + if $crate::is_verbose() || $crate::is_trace() { + println!($($arg)*); + } + }; +} + +/// (A. k. a. "*very* verbose".) Checks environment variable `TRACE` to see if +/// detailed small-step status messages should be printed. +/// +/// Use [`traceln!`] to print stuff only if `TRACE` is set. +pub fn is_trace() -> bool { + env::var("TRACE").map(|v| !v.is_empty()).unwrap_or(false) +} +/// Like [`println!`], but only if [`is_trace`] is true (due to the environment +/// variable `TRACE` being set.) +#[macro_export] +macro_rules! traceln { + ($($arg:tt)*) => { + if $crate::is_trace() { + println!( "{}", format!($($arg)*).yellow() ); + } + }; +} +/// `trace_var!( [msg,] var )` prints either `varname = value` or `msg: value` +/// *if `TRACE` is set* (else is silent.) +/// +/// There's an alternative form of `trace_var!( [msg,] var[, true] )` or the +/// preferred form of `trace_var_!( [msg,] var )` (i.e. just add an underscore +/// to the name), which will use the "pretty" form. +#[macro_export] +macro_rules! trace_var { + ($var:expr, $pretty:expr) => { + if $crate::is_trace() { + if $pretty { + println!("{} = {}", stringify!($var).green(), format!("{:#?}", $var).cyan()); + } else { + println!("{} = {}", stringify!($var).green(), format!("{:?}", $var).cyan()); + } + } + }; + ($msg:expr, $var:expr, $pretty:expr) => { + if $crate::is_trace() { + if $pretty { + println!("{}: {}", $msg.green(), format!("{:#?}", $var).cyan()); + } else { + println!("{}: {}", $msg.green(), format!("{:?}", $var).cyan()); + } + } + }; + ($var:expr) => { + trace_var!($var, false); + }; + ($msg:expr, $var:expr) => { + trace_var!($msg, $var, false); + }; +} + +/// Pretty form of [`trace_var!`]. +#[macro_export] +macro_rules! trace_var_ { + ($var:expr) => { + $crate::trace_var!($var, true); + }; + ($msg:expr, $var:expr) => { + $crate::trace_var!($msg, $var, true); + }; +} + +/// Similar to [`todo!`], but non-fatal; `NYI` prints a message to stderr _only once_ #[macro_export] macro_rules! NYI { ($msg:expr) => {{ + // rely on the non-snake-case warning to get a warning at use sites + fn NYI() {} + NYI(); static ONCE: std::sync::Once = std::sync::Once::new(); let location = stdext::debug_name!(); ONCE.call_once(|| { diff --git a/src/main.rs b/src/main.rs index 0041292..3df0886 100644 --- a/src/main.rs +++ b/src/main.rs @@ -48,6 +48,7 @@ use cccron_lib::email::{self, Email, SimpleEmail}; use cccron_lib::hedgedoc::{self, HedgeDoc}; use cccron_lib::is_dry_run; use cccron_lib::mediawiki::{self, Mediawiki}; +use cccron_lib::{verboseln,trace_var,trace_var_}; use cccron_lib::NYI; /* ***** Config Spec ***** */ @@ -113,82 +114,6 @@ const CONFIG_SPEC: CfgSpec<'static> = CfgSpec { /* ***** Runtime Configuration (Arguments & Environment Variables) ***** */ -/// Checks environment variable `VERBOSE` to see if status messages should be -/// printed. -/// -/// Use `verboseln!` to print stuff only if `VERBOSE` (or `TRACE`) is set. -fn is_verbose() -> bool { - env::var("VERBOSE").map(|v| !v.is_empty()).unwrap_or(false) -} -/// Like `println!`, but only if `is_verbose` (or `is_trace`) is true (due to -/// the environment variable `VERBOSE` being set.) -macro_rules! verboseln { - ($($arg:tt)*) => { - if is_verbose() || is_trace() { - println!($($arg)*); - } - }; -} - -/// (A. k. a. "*very* verbose".) Checks environment variable `TRACE` to see if -/// detailed small-step status messages should be printed. -/// -/// Use `traceln!` to print stuff only if `TRACE` is set. -fn is_trace() -> bool { - env::var("TRACE").map(|v| !v.is_empty()).unwrap_or(false) -} -/// Like `println!`, but only if `is_trace` is true (due to the environment -/// variable `TRACE` being set.) -macro_rules! traceln { - ($($arg:tt)*) => { - if is_trace() { - println!( "{}", format!($($arg)*).yellow() ); - } - }; -} -/// `trace_var!( [msg,] var )` prints either `varname = value` or `msg: value` -/// *if TRACE is set* (else is silent.) -/// -/// There's an alternative form of `trace_var!( [msg,] var[, true] )` or the -/// preferred form of `trace_var_!( [msg,] var )` (i.e. just add an underscore -/// to the name), which will use the "pretty" form. -macro_rules! trace_var { - ($var:expr, $pretty:expr) => { - if is_trace() { - if $pretty { - println!("{} = {}", stringify!($var).green(), format!("{:#?}", $var).cyan()); - } else { - println!("{} = {}", stringify!($var).green(), format!("{:?}", $var).cyan()); - } - } - }; - ($msg:expr, $var:expr, $pretty:expr) => { - if is_trace() { - if $pretty { - println!("{}: {}", $msg.green(), format!("{:#?}", $var).cyan()); - } else { - println!("{}: {}", $msg.green(), format!("{:?}", $var).cyan()); - } - } - }; - ($var:expr) => { - trace_var!($var, false); - }; - ($msg:expr, $var:expr) => { - trace_var!($msg, $var, false); - }; -} - -/// Pretty form of `trace_var!` -macro_rules! trace_var_ { - ($var:expr) => { - trace_var!($var, true); - }; - ($msg:expr, $var:expr) => { - trace_var!($msg, $var, true); - }; -} - /// Gets either today or the date from the environment variable `TODAY` (for /// testing purposes.) fn today() -> NaiveDate {