From c75a4ef5aaabfd13c1bf831710404aa96df609e5 Mon Sep 17 00:00:00 2001 From: Rene Leonhardt <65483435+reneleonhardt@users.noreply.github.com> Date: Tue, 8 Jul 2025 12:47:28 +0200 Subject: [PATCH] chore(rust): bump edition to 2021 --- Cargo.toml | 2 +- src/lib.rs | 85 ++++++++++++++++++++++++------------------------------ 2 files changed, 39 insertions(+), 48 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 1afe819..fc4c7cf 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -11,7 +11,7 @@ repository = "https://github.com/BurntSushi/termcolor" readme = "README.md" keywords = ["windows", "win", "color", "ansi", "console"] license = "Unlicense OR MIT" -edition = "2018" +edition = "2021" [lib] name = "termcolor" diff --git a/src/lib.rs b/src/lib.rs index 9e6371f..c572ea0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -39,7 +39,7 @@ except it is augmented with methods for coloring by the `WriteColor` trait. For example, to write some green text: ```rust,no_run -# fn test() -> Result<(), Box<::std::error::Error>> { +# fn test() -> Result<(), Box> { use std::io::Write; use termcolor::{Color, ColorChoice, ColorSpec, StandardStream, WriteColor}; @@ -66,7 +66,7 @@ implements `io::Write` and `io::WriteColor`. This example shows how to print some green text to stderr. ```rust,no_run -# fn test() -> Result<(), Box<::std::error::Error>> { +# fn test() -> Result<(), Box> { use std::io::Write; use termcolor::{BufferWriter, Color, ColorChoice, ColorSpec, WriteColor}; @@ -198,45 +198,45 @@ pub trait WriteColor: io::Write { } } -impl<'a, T: ?Sized + WriteColor> WriteColor for &'a mut T { +impl WriteColor for &mut T { fn supports_color(&self) -> bool { - (&**self).supports_color() + (**self).supports_color() } fn supports_hyperlinks(&self) -> bool { - (&**self).supports_hyperlinks() + (**self).supports_hyperlinks() } fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { - (&mut **self).set_color(spec) + (**self).set_color(spec) } fn set_hyperlink(&mut self, link: &HyperlinkSpec) -> io::Result<()> { - (&mut **self).set_hyperlink(link) + (**self).set_hyperlink(link) } fn reset(&mut self) -> io::Result<()> { - (&mut **self).reset() + (**self).reset() } fn is_synchronous(&self) -> bool { - (&**self).is_synchronous() + (**self).is_synchronous() } } impl WriteColor for Box { fn supports_color(&self) -> bool { - (&**self).supports_color() + (**self).supports_color() } fn supports_hyperlinks(&self) -> bool { - (&**self).supports_hyperlinks() + (**self).supports_hyperlinks() } fn set_color(&mut self, spec: &ColorSpec) -> io::Result<()> { - (&mut **self).set_color(spec) + (**self).set_color(spec) } fn set_hyperlink(&mut self, link: &HyperlinkSpec) -> io::Result<()> { - (&mut **self).set_hyperlink(link) + (**self).set_hyperlink(link) } fn reset(&mut self) -> io::Result<()> { - (&mut **self).reset() + (**self).reset() } fn is_synchronous(&self) -> bool { - (&**self).is_synchronous() + (**self).is_synchronous() } } @@ -381,7 +381,6 @@ impl fmt::Display for ColorChoiceParseError { /// `std::io` implements `Stdout` and `Stderr` (and their `Lock` variants) as /// separate types, which makes it difficult to abstract over them. We use /// some simple internal enum types to work around this. - enum StandardStreamType { Stdout, Stderr, @@ -1658,7 +1657,6 @@ impl Ansi { Color::White => write_intense!("15"), Color::Ansi256(c) => write_custom!(c), Color::Rgb(r, g, b) => write_custom!(r, g, b), - Color::__Nonexhaustive => unreachable!(), } } else { match *c { @@ -1672,7 +1670,6 @@ impl Ansi { Color::White => write_normal!("7"), Color::Ansi256(c) => write_custom!(c), Color::Rgb(r, g, b) => write_custom!(r, g, b), - Color::__Nonexhaustive => unreachable!(), } } } @@ -2068,6 +2065,7 @@ impl ColorSpec { /// Hexadecimal numbers are written with a `0x` prefix. #[allow(missing_docs)] #[derive(Clone, Copy, Debug, Eq, PartialEq)] +#[non_exhaustive] pub enum Color { Black, Blue, @@ -2079,8 +2077,6 @@ pub enum Color { White, Ansi256(u8), Rgb(u8, u8, u8), - #[doc(hidden)] - __Nonexhaustive, } impl Color { @@ -2119,7 +2115,6 @@ impl Color { Color::Ansi256(15) => return Some((Yes, wincon::Color::White)), Color::Ansi256(_) => return None, Color::Rgb(_, _, _) => return None, - Color::__Nonexhaustive => unreachable!(), }; let intense = if intense { Yes } else { No }; Some((intense, color)) @@ -2134,31 +2129,27 @@ impl Color { // by a comma corresponding to one of 256^3 colors. fn parse_number(s: &str) -> Option { - use std::u8; - - if s.starts_with("0x") { - u8::from_str_radix(&s[2..], 16).ok() + if let Some(hex) = s.strip_prefix("0x") { + u8::from_str_radix(hex, 16).ok() } else { - u8::from_str_radix(s, 10).ok() + s.parse::().ok() } } let codes: Vec<&str> = s.split(',').collect(); if codes.len() == 1 { - if let Some(n) = parse_number(&codes[0]) { + if let Some(n) = parse_number(codes[0]) { Ok(Color::Ansi256(n)) + } else if s.chars().all(|c| c.is_ascii_hexdigit()) { + Err(ParseColorError { + kind: ParseColorErrorKind::InvalidAnsi256, + given: s.to_string(), + }) } else { - if s.chars().all(|c| c.is_digit(16)) { - Err(ParseColorError { - kind: ParseColorErrorKind::InvalidAnsi256, - given: s.to_string(), - }) - } else { - Err(ParseColorError { - kind: ParseColorErrorKind::InvalidName, - given: s.to_string(), - }) - } + Err(ParseColorError { + kind: ParseColorErrorKind::InvalidName, + given: s.to_string(), + }) } } else if codes.len() == 3 { let mut v = vec![]; @@ -2505,14 +2496,14 @@ mod tests { fn all_attributes() -> Vec { let mut result = vec![]; - for fg in vec![None, Some(Color::Red)] { - for bg in vec![None, Some(Color::Red)] { - for bold in vec![false, true] { - for underline in vec![false, true] { - for intense in vec![false, true] { - for italic in vec![false, true] { - for strikethrough in vec![false, true] { - for dimmed in vec![false, true] { + for fg in [None, Some(Color::Red)] { + for bg in [None, Some(Color::Red)] { + for bold in [false, true] { + for underline in [false, true] { + for intense in [false, true] { + for italic in [false, true] { + for strikethrough in [false, true] { + for dimmed in [false, true] { let mut color = ColorSpec::new(); color.set_fg(fg); color.set_bg(bg); @@ -2552,7 +2543,7 @@ mod tests { for color in all_attributes() { let mut color1 = color.clone(); color1.clear(); - assert!(color1.is_none(), "{:?} => {:?}", color, color1); + assert!(color1.is_none(), "{color:?} => {color1:?}"); } }