I have next code:
// part of main.rs
fn main() -> Result<(), error::ParseError> {
foo()?;
Ok(())
}
Assumed that foo()
return error::ParseError::EmptyLine
.
// error.rs
use std::{error::Error, fmt};
use colored::Colorize;
pub enum ParseError {
EmptyLine,
}
impl fmt::Display for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
error_handler(self, f)
}
}
impl std::fmt::Debug for ParseError {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
error_handler(self, f)
}
}
impl Error for ParseError {}
fn error_handler(error: &ParseError, f: &mut fmt::Formatter) -> fmt::Result {
match error {
ParseError::EmptyLine => writeln!(f, "{}", "input is an emtpy line".red().bold()),
}
}
Next ouput after cargo run
Error: input is an emtpy line
The problem is that part input is an empty line is red, as I want, but word Error, which rust add by default, dosen’t change color. I can’t find any ways to modify this prefix or remove it. Appreciate any information how I can do it.
One more thing. I prefer to do it myself without additional libs, if it’s possible.
New contributor
ned is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.