macro_rules! info {
(incoming, $($arg:tt)+) => {
::tracing::info!(status = "incoming", $($arg)+)
};
(outgoing, $($arg:tt)+) => {
::tracing::info!(status = "outgoing", $($arg)+)
};
($($arg:tt)+) => {
::tracing::info!(status = "info", $($arg)+)
};
}
macro_rules! success {
($($arg:tt)+) => {
::tracing::info!(status = "success", $($arg)+)
};
}
macro_rules! error {
($($arg:tt)+) => {
::tracing::error!(status = "error", $($arg)+)
};
}
macro_rules! warn_macro {
($($arg:tt)+) => {
::tracing::warn!(status = "warn", $($arg)+)
};
}
pub(crate) use error;
pub(crate) use info;
pub(crate) use success;
pub(crate) use warn_macro as warn;
#[cfg(test)]
mod tests {
#[test]
fn every_diagnostic_is_written_in_one_voice() {
const MACROS: [&str; 4] = ["info!", "success!", "warn!", "error!"];
let root = std::path::Path::new(env!("CARGO_MANIFEST_DIR")).join("src");
let mut checked = 0;
for file in sources(&root) {
if file.file_name().is_some_and(|name| name == "logging.rs") {
continue;
}
let text = std::fs::read_to_string(&file).expect("a source file");
for (at, _) in MACROS.iter().flat_map(|name| text.match_indices(name)) {
let Some(message) = literal_after(&text, at) else {
continue;
};
let Some(first) = message.split_whitespace().next() else {
continue;
};
if message.len() < 4 || message.starts_with('{') {
continue;
}
checked += 1;
let shouts = first
.chars()
.all(|c| !c.is_alphabetic() || c.is_uppercase());
assert!(
!first.chars().next().is_some_and(char::is_uppercase) || shouts,
"{}: '{message}' begins with a capital that is not an initialism",
file.display()
);
assert!(
!message.trim_end().ends_with('.'),
"{}: '{message}' ends with a full stop",
file.display()
);
}
}
assert!(checked > 20, "the scan found only {checked} messages");
}
fn sources(root: &std::path::Path) -> Vec<std::path::PathBuf> {
let mut found = Vec::new();
let Ok(entries) = std::fs::read_dir(root) else {
return found;
};
for entry in entries.flatten() {
let path = entry.path();
if path.is_dir() {
found.extend(sources(&path));
} else if path.extension().is_some_and(|kind| kind == "rs") {
found.push(path);
}
}
found
}
fn literal_after(text: &str, at: usize) -> Option<String> {
let opening = text[at..].find('"')? + at + 1;
let mut message = String::new();
let mut chars = text[opening..].chars();
while let Some(character) = chars.next() {
match character {
'"' => return Some(message),
'\\' => match chars.next() {
Some('\n') => {
while chars.clone().next().is_some_and(char::is_whitespace) {
chars.next();
}
}
Some(escaped) => message.push(escaped),
None => return None,
},
_ => message.push(character),
}
}
None
}
}