facet_testhelpers/
lib.rs

1#![warn(missing_docs)]
2#![warn(clippy::std_instead_of_core)]
3#![warn(clippy::std_instead_of_alloc)]
4#![forbid(unsafe_code)]
5#![doc = include_str!("../README.md")]
6
7pub use facet_testhelpers_macros::test;
8
9use log::{Level, LevelFilter, Log, Metadata, Record};
10use owo_colors::{OwoColorize, Style};
11use std::io::Write;
12
13struct SimpleLogger;
14
15impl Log for SimpleLogger {
16    fn enabled(&self, _metadata: &Metadata) -> bool {
17        true
18    }
19
20    fn log(&self, record: &Record) {
21        // Create style based on log level
22        let level_style = match record.level() {
23            Level::Error => Style::new().fg_rgb::<243, 139, 168>(), // Catppuccin red (Maroon)
24            Level::Warn => Style::new().fg_rgb::<249, 226, 175>(),  // Catppuccin yellow (Peach)
25            Level::Info => Style::new().fg_rgb::<166, 227, 161>(),  // Catppuccin green (Green)
26            Level::Debug => Style::new().fg_rgb::<137, 180, 250>(), // Catppuccin blue (Blue)
27            Level::Trace => Style::new().fg_rgb::<148, 226, 213>(), // Catppuccin teal (Teal)
28        };
29
30        // Convert level to styled display
31        eprintln!(
32            "{} - {}: {}",
33            record.level().style(level_style),
34            record
35                .target()
36                .style(Style::new().fg_rgb::<137, 180, 250>()), // Blue for the target
37            record.args()
38        );
39    }
40
41    fn flush(&self) {
42        let _ = std::io::stderr().flush();
43    }
44}
45
46/// Set up a simple logger.
47pub fn setup() {
48    let logger = Box::new(SimpleLogger);
49    log::set_boxed_logger(logger).unwrap();
50    log::set_max_level(LevelFilter::Trace);
51}
52
53/// An error type that panics when it's built (such as when you use `?`
54/// to coerce to it)
55#[derive(Debug)]
56pub struct IPanic;
57
58impl<E> From<E> for IPanic
59where
60    E: core::error::Error + Send + Sync,
61{
62    #[track_caller]
63    fn from(value: E) -> Self {
64        panic!("from: {}: {value}", core::panic::Location::caller())
65    }
66}