From e45ca05c8e2ff84818574974f9a77b433142d750 Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Mon, 8 Jan 2024 10:28:24 -0500 Subject: [PATCH 1/2] examples: replace structopt with pico-args See https://github.com/TeXitoi/structopt/issues/525. Currently codespan-reporting is the second most downloaded dependent of structopt: https://crates.io/crates/structopt/reverse_dependencies. --- Cargo.lock | 183 ++++-------------- codespan-reporting/Cargo.toml | 2 +- codespan-reporting/examples/readme_preview.rs | 38 ++-- .../examples/reusable_diagnostic.rs | 24 +-- codespan-reporting/examples/term.rs | 26 +-- codespan-reporting/src/term.rs | 26 +-- 6 files changed, 99 insertions(+), 200 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 60d7aed9a..17d21c187 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -1,13 +1,6 @@ # This file is automatically @generated by Cargo. # It is not intended for manual editing. -[[package]] -name = "ansi_term" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ee49baf6cb617b853aa8d93bf420db2383fab46d314482ca2803b40d5fde979b" -dependencies = [ - "winapi 0.3.4", -] +version = 3 [[package]] name = "anyhow" @@ -15,17 +8,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a9ff2deb543832ee7b1a08060c38cc6af5816e96d3fcb6fc2e99bd15634e5c7f" -[[package]] -name = "atty" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d912da0db7fa85514874458ca3651fe2cddace8d0b0505571dbdcd41ab490159" -dependencies = [ - "kernel32-sys", - "libc", - "winapi 0.2.8", -] - [[package]] name = "base64" version = "0.12.0" @@ -62,21 +44,6 @@ version = "1.0.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" -[[package]] -name = "clap" -version = "2.33.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "5067f5bb2d80ef5d68b4c87db81601f0b75bca627bc2ef76b141d7b846a3c6d9" -dependencies = [ - "ansi_term", - "atty", - "bitflags 1.3.2", - "strsim", - "textwrap", - "unicode-width", - "vec_map", -] - [[package]] name = "codespan" version = "0.11.1" @@ -103,9 +70,9 @@ dependencies = [ "insta", "lazy_static", "peg", + "pico-args", "rustyline", "serde", - "structopt", "termcolor", "unicode-width", "unindent", @@ -121,7 +88,7 @@ dependencies = [ "lazy_static", "libc", "terminal_size", - "winapi 0.3.4", + "winapi", ] [[package]] @@ -142,7 +109,7 @@ checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" dependencies = [ "libc", "redox_users", - "winapi 0.3.4", + "winapi", ] [[package]] @@ -168,15 +135,6 @@ dependencies = [ "wasi", ] -[[package]] -name = "heck" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ea04fa3ead4e05e51a7c806fc07271fdbde4e246a6c6d1efd52e72230b771b82" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "idna" version = "0.2.0" @@ -209,16 +167,6 @@ version = "0.4.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1306f3464951f30e30d12373d31c79fbd52d236e5e896fd92f96ec7babbbe60b" -[[package]] -name = "kernel32-sys" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "ad1ca084b49bfd975182288e1a5f1d27ea34ff2d6ae084ae5e66e1652427eada" -dependencies = [ - "winapi 0.2.8", - "winapi-build", -] - [[package]] name = "lazy_static" version = "1.4.0" @@ -333,30 +281,25 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ba4f28a6faf4ffea762ba8f4baef48c61a6db348647c73095034041fc79dd954" [[package]] -name = "proc-macro-error" -version = "0.2.0" +name = "pico-args" +version = "0.5.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "0b5127cadb7667b00c24680b1672dfbaf9fbc5bd5889f1085b385fe950da9613" -dependencies = [ - "proc-macro2", - "quote", - "syn", -] +checksum = "5be167a7af36ee22fe3115051bc51f6e6c7054c9348e28deb4f49bd6f705a315" [[package]] name = "proc-macro2" -version = "1.0.24" +version = "1.0.78" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1e0704ee1a7e00d7bb417d0770ea303c1bccbabf0ef1667dae92b5967f5f8a71" +checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae" dependencies = [ - "unicode-xid", + "unicode-ident", ] [[package]] name = "quote" -version = "1.0.0" +version = "1.0.35" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "7ab938ebe6f1c82426b5fb82eaf10c3e3028c53deaa3fbe38f5904b37cf4d767" +checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef" dependencies = [ "proc-macro2", ] @@ -396,7 +339,7 @@ dependencies = [ "unicode-segmentation", "unicode-width", "utf8parse", - "winapi 0.3.4", + "winapi", ] [[package]] @@ -465,53 +408,24 @@ version = "1.2.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9a04629d2e8ecdcf30e0188e3699ed6d50d5750d0219db146a790065fe92a897" -[[package]] -name = "strsim" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" - -[[package]] -name = "structopt" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "48399718b3ad695558b979b08a9056a5272ec573cd3070f5ca34165bd4a5bf35" -dependencies = [ - "clap", - "structopt-derive", -] - -[[package]] -name = "structopt-derive" -version = "0.3.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2558075232402034384db060831349fb2d1303479593177cc84c25febbebbc6d" -dependencies = [ - "heck", - "proc-macro-error", - "proc-macro2", - "quote", - "syn", -] - [[package]] name = "syn" -version = "1.0.67" +version = "1.0.109" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "6498a9efc342871f91cc2d0d694c674368b4ceb40f62b65a7a08c3792935e702" +checksum = "72b64191b275b66ffe2469e8af2c1cfe3bafa67b529ead792a6d0160888b4237" dependencies = [ "proc-macro2", "quote", - "unicode-xid", + "unicode-ident", ] [[package]] name = "termcolor" -version = "1.0.4" +version = "1.4.1" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4096add70612622289f2fdcdbd5086dc81c1e2675e6ae58d6c4f62a16c6d7f2f" +checksum = "06794f8f6c5c898b3275aebefa6b8a1cb24cd2c6c79397ab15774837a0bc5755" dependencies = [ - "wincolor", + "winapi-util", ] [[package]] @@ -521,16 +435,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "5e288a11a53da73056d3c7cbdee83b1d1ce3344c3210a1f8b56c32f9662a87e9" dependencies = [ "libc", - "winapi 0.3.4", -] - -[[package]] -name = "textwrap" -version = "0.11.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "d326610f408c7a4eb6f51c37c330e496b08506c9457c9d34287ecc38809fb060" -dependencies = [ - "unicode-width", + "winapi", ] [[package]] @@ -562,6 +467,12 @@ dependencies = [ "matches", ] +[[package]] +name = "unicode-ident" +version = "1.0.12" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b" + [[package]] name = "unicode-normalization" version = "0.1.5" @@ -580,12 +491,6 @@ version = "0.1.4" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bf3a113775714a22dcb774d8ea3655c53a32debae63a063acc00a91cc586245f" -[[package]] -name = "unicode-xid" -version = "0.2.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" - [[package]] name = "unindent" version = "0.1.0" @@ -616,12 +521,6 @@ version = "0.8.1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9fde2f6a4bea1d6e007c4ad38c6839fa71cbb63b6dbf5b595aa38dc9b1093c11" -[[package]] -name = "vec_map" -version = "0.8.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "887b5b631c2ad01628bbbaa7dd4c869f80d3186688f8d0b6f58774fbe324988c" - [[package]] name = "void" version = "1.0.2" @@ -634,12 +533,6 @@ version = "0.11.0+wasi-snapshot-preview1" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "9c8d87e72b64a3b4db28d11ce29237c246188f4f51057d65a7eab63b7987e423" -[[package]] -name = "winapi" -version = "0.2.8" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "167dc9d6949a9b857f3451275e911c3f44255842c1f7a76f33c55103a909087a" - [[package]] name = "winapi" version = "0.3.4" @@ -650,12 +543,6 @@ dependencies = [ "winapi-x86_64-pc-windows-gnu", ] -[[package]] -name = "winapi-build" -version = "0.1.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2d315eee3b34aca4797b2da6b13ed88266e6d612562a0c46390af8299fc699bc" - [[package]] name = "winapi-i686-pc-windows-gnu" version = "0.4.0" @@ -663,19 +550,19 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ac3b87c63620426dd9b991e5ce0329eff545bccbbb34f3be09ff6fb6ab51b7b6" [[package]] -name = "winapi-x86_64-pc-windows-gnu" -version = "0.4.0" +name = "winapi-util" +version = "0.1.6" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" +checksum = "f29e6f9198ba0d26b4c9f07dbe6f9ed633e1f3d5b8b414090084349e46a52596" +dependencies = [ + "winapi", +] [[package]] -name = "wincolor" -version = "1.0.0" +name = "winapi-x86_64-pc-windows-gnu" +version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b9dc3aa9dcda98b5a16150c54619c1ead22e3d3a5d458778ae914be760aa981a" -dependencies = [ - "winapi 0.3.4", -] +checksum = "712e227841d057c1ee1cd2fb22fa7e5a5461ae8e48fa2ca79ec42cfc1931183f" [[package]] name = "yaml-rust" diff --git a/codespan-reporting/Cargo.toml b/codespan-reporting/Cargo.toml index 91b91c7c5..643593f23 100644 --- a/codespan-reporting/Cargo.toml +++ b/codespan-reporting/Cargo.toml @@ -12,6 +12,7 @@ exclude = ["assets/**"] edition = "2018" [dependencies] +pico-args = "0.5.0" serde = { version = "1", optional = true, features = ["derive"] } termcolor = "1.0.4" unicode-width = "0.1" @@ -22,7 +23,6 @@ insta = "1.6.3" lazy_static = "1.4" peg = "0.7" rustyline = "6" -structopt = "0.3" unindent = "0.1" [features] diff --git a/codespan-reporting/examples/readme_preview.rs b/codespan-reporting/examples/readme_preview.rs index f5a6468a8..25a9240e1 100644 --- a/codespan-reporting/examples/readme_preview.rs +++ b/codespan-reporting/examples/readme_preview.rs @@ -9,30 +9,43 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFile; -use codespan_reporting::term::termcolor::{Color, ColorSpec, StandardStream, WriteColor}; +use codespan_reporting::term::termcolor::{ + Color, ColorChoice, ColorSpec, StandardStream, WriteColor, +}; use codespan_reporting::term::{self, ColorArg}; use std::io::{self, Write}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug)] pub enum Opts { /// Render SVG output Svg, /// Render Stderr output Stderr { /// Configure coloring of output - #[structopt( - long = "color", - parse(try_from_str), - default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true - )] color: ColorArg, }, } +fn parse_args() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + match pargs.subcommand()? { + Some(value) => match value.as_str() { + "svg" => Ok(Opts::Svg), + "stderr" => { + let color = pargs + .opt_value_from_str("--color")? + .unwrap_or(ColorArg(ColorChoice::Auto)); + Ok(Opts::Stderr { color }) + } + _ => Err(pico_args::Error::Utf8ArgumentParsingFailed { + value, + cause: "not a valid subcommand".to_owned(), + }), + }, + None => Err(pico_args::Error::MissingArgument), + } +} + fn main() -> anyhow::Result<()> { let file = SimpleFile::new( "FizzBuzz.fun", @@ -76,8 +89,7 @@ fn main() -> anyhow::Result<()> { ", )])]; - // let mut files = SimpleFiles::new(); - match Opts::from_args() { + match parse_args()? { Opts::Svg => { let mut buffer = Vec::new(); let mut writer = HtmlEscapeWriter::new(SvgWriter::new(&mut buffer)); diff --git a/codespan-reporting/examples/reusable_diagnostic.rs b/codespan-reporting/examples/reusable_diagnostic.rs index d05dee854..6e501becd 100644 --- a/codespan-reporting/examples/reusable_diagnostic.rs +++ b/codespan-reporting/examples/reusable_diagnostic.rs @@ -1,22 +1,22 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFile; -use codespan_reporting::term::termcolor::StandardStream; +use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use codespan_reporting::term::{self, ColorArg}; use std::ops::Range; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug)] pub struct Opts { - #[structopt(long = "color", - parse(try_from_str), - default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true - )] color: ColorArg, } +fn parse_args() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + let color = pargs + .opt_value_from_str("--color")? + .unwrap_or(ColorArg(ColorChoice::Auto)); + Ok(Opts { color }) +} + fn main() -> anyhow::Result<()> { let file = SimpleFile::new( "main.rs", @@ -38,8 +38,8 @@ fn main() -> anyhow::Result<()> { Error::MutatingImmutable(Item::new(20..23, "foo"), Item::new(51..59, "foo += 1")), ]; - let opts = Opts::from_args(); - let writer = StandardStream::stderr(opts.color.into()); + let Opts { color } = parse_args()?; + let writer = StandardStream::stderr(color.into()); let config = codespan_reporting::term::Config::default(); for diagnostic in errors.iter().map(Error::report) { term::emit(&mut writer.lock(), &config, &file, &diagnostic)?; diff --git a/codespan-reporting/examples/term.rs b/codespan-reporting/examples/term.rs index ee2c057d7..4d75543db 100644 --- a/codespan-reporting/examples/term.rs +++ b/codespan-reporting/examples/term.rs @@ -7,26 +7,26 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFiles; -use codespan_reporting::term::termcolor::StandardStream; +use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; use codespan_reporting::term::{self, ColorArg}; -use structopt::StructOpt; -#[derive(Debug, StructOpt)] -#[structopt(name = "emit")] +#[derive(Debug)] pub struct Opts { /// Configure coloring of output - #[structopt( - long = "color", - parse(try_from_str), - default_value = "auto", - possible_values = ColorArg::VARIANTS, - case_insensitive = true - )] pub color: ColorArg, } +fn parse_args() -> Result { + let mut pargs = pico_args::Arguments::from_env(); + let color = pargs + .opt_value_from_str("--color")? + .unwrap_or(ColorArg(ColorChoice::Auto)); + Ok(Opts { color }) +} + fn main() -> anyhow::Result<()> { - let opts = Opts::from_args(); + let Opts { color } = parse_args()?; + let mut files = SimpleFiles::new(); let file_id1 = files.add( @@ -165,7 +165,7 @@ fn main() -> anyhow::Result<()> { )]), ]; - let writer = StandardStream::stderr(opts.color.into()); + let writer = StandardStream::stderr(color.into()); let config = codespan_reporting::term::Config::default(); for diagnostic in &diagnostics { term::emit(&mut writer.lock(), &config, &files, diagnostic)?; diff --git a/codespan-reporting/src/term.rs b/codespan-reporting/src/term.rs index 0e75a1424..c08cc067a 100644 --- a/codespan-reporting/src/term.rs +++ b/codespan-reporting/src/term.rs @@ -16,32 +16,33 @@ pub use self::config::{Chars, Config, DisplayStyle, Styles}; /// A command line argument that configures the coloring of the output. /// -/// This can be used with command line argument parsers like [`clap`] or [`structopt`]. +/// This can be used with command line argument parsers like [`clap`] or [`Parser`]. /// /// [`clap`]: https://crates.io/crates/clap -/// [`structopt`]: https://crates.io/crates/structopt +/// [`Parser`]: https://crates.io/crates/Parser /// /// # Example /// /// ```rust -/// use codespan_reporting::term::termcolor::StandardStream; +/// use clap::{builder::TypedValueParser as _, Parser}; /// use codespan_reporting::term::ColorArg; -/// use structopt::StructOpt; +/// use codespan_reporting::term::termcolor::StandardStream; +/// use std::str::FromStr as _; /// -/// #[derive(Debug, StructOpt)] -/// #[structopt(name = "groovey-app")] +/// #[derive(Debug, Parser)] +/// #[clap(name = "groovey-app")] /// pub struct Opts { /// /// Configure coloring of output -/// #[structopt( +/// #[clap( /// long = "color", /// default_value = "auto", -/// possible_values = ColorArg::VARIANTS, -/// case_insensitive = true, +/// value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS) +/// .map(|s| ColorArg::from_str(&s)).map(Result::unwrap), /// )] /// pub color: ColorArg, /// } /// -/// let opts = Opts::from_args(); +/// let opts = Opts::parse(); /// let writer = StandardStream::stderr(opts.color.into()); /// ``` #[derive(Copy, Clone, Debug, PartialEq, Eq)] @@ -50,11 +51,10 @@ pub struct ColorArg(pub ColorChoice); impl ColorArg { /// Allowed values the argument. /// - /// This is useful for generating documentation via [`clap`] or `structopt`'s - /// `possible_values` configuration. + /// This is useful for generating documentation via [`clap`]'s + /// `value_parser = clap::builder::PossibleValuesParser::new` configuration. /// /// [`clap`]: https://crates.io/crates/clap - /// [`structopt`]: https://crates.io/crates/structopt pub const VARIANTS: &'static [&'static str] = &["auto", "always", "ansi", "never"]; } From 14bdca2ffb4d30ab196857faa2b9ccdc15d0e26b Mon Sep 17 00:00:00 2001 From: Tamir Duberstein Date: Thu, 22 Feb 2024 10:40:52 +0000 Subject: [PATCH 2/2] Delete ColorArg This type doesn't provide any value over ColorChoice, which is already FromStr. --- codespan-reporting/examples/readme_preview.rs | 6 +- .../examples/reusable_diagnostic.rs | 6 +- codespan-reporting/examples/term.rs | 6 +- codespan-reporting/src/term.rs | 67 +------------------ 4 files changed, 10 insertions(+), 75 deletions(-) diff --git a/codespan-reporting/examples/readme_preview.rs b/codespan-reporting/examples/readme_preview.rs index 25a9240e1..2b99addc8 100644 --- a/codespan-reporting/examples/readme_preview.rs +++ b/codespan-reporting/examples/readme_preview.rs @@ -12,7 +12,7 @@ use codespan_reporting::files::SimpleFile; use codespan_reporting::term::termcolor::{ Color, ColorChoice, ColorSpec, StandardStream, WriteColor, }; -use codespan_reporting::term::{self, ColorArg}; +use codespan_reporting::term::{self}; use std::io::{self, Write}; #[derive(Debug)] @@ -22,7 +22,7 @@ pub enum Opts { /// Render Stderr output Stderr { /// Configure coloring of output - color: ColorArg, + color: ColorChoice, }, } @@ -34,7 +34,7 @@ fn parse_args() -> Result { "stderr" => { let color = pargs .opt_value_from_str("--color")? - .unwrap_or(ColorArg(ColorChoice::Auto)); + .unwrap_or(ColorChoice::Auto); Ok(Opts::Stderr { color }) } _ => Err(pico_args::Error::Utf8ArgumentParsingFailed { diff --git a/codespan-reporting/examples/reusable_diagnostic.rs b/codespan-reporting/examples/reusable_diagnostic.rs index 6e501becd..777c8101d 100644 --- a/codespan-reporting/examples/reusable_diagnostic.rs +++ b/codespan-reporting/examples/reusable_diagnostic.rs @@ -1,19 +1,19 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFile; use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; -use codespan_reporting::term::{self, ColorArg}; +use codespan_reporting::term::{self}; use std::ops::Range; #[derive(Debug)] pub struct Opts { - color: ColorArg, + color: ColorChoice, } fn parse_args() -> Result { let mut pargs = pico_args::Arguments::from_env(); let color = pargs .opt_value_from_str("--color")? - .unwrap_or(ColorArg(ColorChoice::Auto)); + .unwrap_or(ColorChoice::Auto); Ok(Opts { color }) } diff --git a/codespan-reporting/examples/term.rs b/codespan-reporting/examples/term.rs index 4d75543db..38ec5172d 100644 --- a/codespan-reporting/examples/term.rs +++ b/codespan-reporting/examples/term.rs @@ -7,20 +7,20 @@ use codespan_reporting::diagnostic::{Diagnostic, Label}; use codespan_reporting::files::SimpleFiles; +use codespan_reporting::term; use codespan_reporting::term::termcolor::{ColorChoice, StandardStream}; -use codespan_reporting::term::{self, ColorArg}; #[derive(Debug)] pub struct Opts { /// Configure coloring of output - pub color: ColorArg, + pub color: ColorChoice, } fn parse_args() -> Result { let mut pargs = pico_args::Arguments::from_env(); let color = pargs .opt_value_from_str("--color")? - .unwrap_or(ColorArg(ColorChoice::Auto)); + .unwrap_or(ColorChoice::Auto); Ok(Opts { color }) } diff --git a/codespan-reporting/src/term.rs b/codespan-reporting/src/term.rs index c08cc067a..ee4fea436 100644 --- a/codespan-reporting/src/term.rs +++ b/codespan-reporting/src/term.rs @@ -1,7 +1,6 @@ //! Terminal back-end for emitting diagnostics. -use std::str::FromStr; -use termcolor::{ColorChoice, WriteColor}; +use termcolor::WriteColor; use crate::diagnostic::Diagnostic; use crate::files::Files; @@ -14,70 +13,6 @@ pub use termcolor; pub use self::config::{Chars, Config, DisplayStyle, Styles}; -/// A command line argument that configures the coloring of the output. -/// -/// This can be used with command line argument parsers like [`clap`] or [`Parser`]. -/// -/// [`clap`]: https://crates.io/crates/clap -/// [`Parser`]: https://crates.io/crates/Parser -/// -/// # Example -/// -/// ```rust -/// use clap::{builder::TypedValueParser as _, Parser}; -/// use codespan_reporting::term::ColorArg; -/// use codespan_reporting::term::termcolor::StandardStream; -/// use std::str::FromStr as _; -/// -/// #[derive(Debug, Parser)] -/// #[clap(name = "groovey-app")] -/// pub struct Opts { -/// /// Configure coloring of output -/// #[clap( -/// long = "color", -/// default_value = "auto", -/// value_parser = clap::builder::PossibleValuesParser::new(ColorArg::VARIANTS) -/// .map(|s| ColorArg::from_str(&s)).map(Result::unwrap), -/// )] -/// pub color: ColorArg, -/// } -/// -/// let opts = Opts::parse(); -/// let writer = StandardStream::stderr(opts.color.into()); -/// ``` -#[derive(Copy, Clone, Debug, PartialEq, Eq)] -pub struct ColorArg(pub ColorChoice); - -impl ColorArg { - /// Allowed values the argument. - /// - /// This is useful for generating documentation via [`clap`]'s - /// `value_parser = clap::builder::PossibleValuesParser::new` configuration. - /// - /// [`clap`]: https://crates.io/crates/clap - pub const VARIANTS: &'static [&'static str] = &["auto", "always", "ansi", "never"]; -} - -impl FromStr for ColorArg { - type Err = &'static str; - - fn from_str(src: &str) -> Result { - match src { - _ if src.eq_ignore_ascii_case("auto") => Ok(ColorArg(ColorChoice::Auto)), - _ if src.eq_ignore_ascii_case("always") => Ok(ColorArg(ColorChoice::Always)), - _ if src.eq_ignore_ascii_case("ansi") => Ok(ColorArg(ColorChoice::AlwaysAnsi)), - _ if src.eq_ignore_ascii_case("never") => Ok(ColorArg(ColorChoice::Never)), - _ => Err("valid values: auto, always, ansi, never"), - } - } -} - -impl From for ColorChoice { - fn from(x: ColorArg) -> Self { - x.0 - } -} - /// Emit a diagnostic using the given writer, context, config, and files. /// /// The return value covers all error cases. These error case can arise if: