Thiserror and partial colorization?

⚓ Rust    📅 2025-06-02    👤 surdeus    👁️ 2      

surdeus

Warning

This post was published 70 days ago. The information described in this article may have changed.

Hi !

I actually use a enum to define some error and I implement fmt::Display to personalize message with some part in some color using Colorize crate.
I was looking if we can do the same with the thiserror crate but I don't find anything about colorization in it ?

Have you already use part color in message with thiserror ?

use colored::Colorize;
use core::fmt;
...

pub enum MyError {
    ErrorA {
        compared: f64,
        value: f64,
    },
...
}

impl<'a> fmt::Display for MyError <'a> {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match &self {
            MyError::ErrorA{ compared, value } => write!(
                f,
                "{} {} > {}",
                "Error A ".red(),
                compared,
                value.to_string().yellow()
            ),
...
    }
}

6 posts - 3 participants

Read full topic

🏷️ rust_feed