error.rs
This file
Path
src/error.rs
51 lines · 1.2 KiB · Rust
Last changed by
fix: Better sanitizingThayne McCombsbefore adoption24198d9
History of this fileTake it
The walk stopped after 200 commits: lines older than 86d6e2d point at it.
fix: Better sanitizingThayne McCombsbefore adoption24198d9
lines 1–50why, one step back1use std::fmt::Display;2use std::io;34use crate::sanitize::write_sanitized;56/// Print an error message using a format string, sanitizing any arguments if necessary7macro_rules! print_error {8 ($fmt:literal, $($arg:expr),*) => {9 eprintln!(concat!("[fd error]: ", $fmt), $($crate::error::SanitizeErr::sanitize($arg)),*)10 }11}1213/// Trait for specifying how to sanitize a type for error display, if needed.14pub(crate) trait SanitizeErr {15 type Sanitized: Display;1617 fn sanitize(self) -> Self::Sanitized;18}1920impl SanitizeErr for &str {21 type Sanitized = Self;2223 fn sanitize(self) -> Self {24 self25 }26}2728impl SanitizeErr for io::Error {29 type Sanitized = SanitizedError<io::Error>;3031 fn sanitize(self) -> Self::Sanitized {32 SanitizedError(self)33 }34}3536impl SanitizeErr for ignore::Error {37 type Sanitized = SanitizedError<ignore::Error>;3839 fn sanitize(self) -> Self::Sanitized {40 SanitizedError(self)41 }42}4344pub struct SanitizedError<E>(E);4546impl<E: std::error::Error> Display for SanitizedError<E> {47 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {48 let raw = self.0.to_string();49 write_sanitized(f, &raw)50 }Merge pull request #1657 from sharkdp/dependabot/cargo/clap-4.5.23Tavian Barnesbefore adoption86d6e2d
line 51why, one step back51}