use std::fmt;
use std::sync::atomic::{AtomicBool, Ordering};
#[derive(Debug, Default)]
pub(crate) struct Once(AtomicBool);
impl Once {
pub(crate) const fn new() -> Self {
Self(AtomicBool::new(false))
}
pub(crate) fn first(&self) -> bool {
!self.0.swap(true, Ordering::Relaxed)
}
}
pub(crate) fn warn_once(once: &Once, problem: fmt::Arguments<'_>, help: &str) {
if once.first() {
tracing::warn!("{problem}\n = help: {help}");
} else {
tracing::debug!("{problem}");
}
}
#[cfg_attr(not(feature = "http"), allow(dead_code))]
pub(crate) fn client_text(text: &str) -> String {
format!("{:?}", crate::error::truncate(text))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn once_is_true_once() {
let once = Once::new();
assert!(once.first());
assert!(!once.first());
}
#[test]
fn client_text_is_escaped() {
assert_eq!(client_text("a\nb\"c"), r#""a\nb\"c""#);
assert!(client_text(&"x".repeat(1000)).len() < 200);
}
}