Skip to content

Commit 0c58465

Browse files
committed
Add PossibleValues for format
1 parent bdb652e commit 0c58465

File tree

1 file changed

+13
-18
lines changed

1 file changed

+13
-18
lines changed

lychee-bin/src/options.rs

+13-18
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ use crate::archive::Archive;
22
use crate::parse::parse_base;
33
use crate::verbosity::Verbosity;
44
use anyhow::{anyhow, Context, Error, Result};
5+
use clap::builder::PossibleValuesParser;
56
use clap::{arg, builder::TypedValueParser, Parser};
67
use const_format::{concatcp, formatcp};
78
use lychee_lib::{
@@ -12,7 +13,7 @@ use secrecy::{ExposeSecret, SecretString};
1213
use serde::Deserialize;
1314
use std::path::Path;
1415
use std::{fs, path::PathBuf, str::FromStr, time::Duration};
15-
use strum::VariantNames;
16+
use strum::{Display, EnumIter, EnumString, EnumVariantNames, VariantNames};
1617

1718
pub(crate) const LYCHEE_IGNORE_FILE: &str = ".lycheeignore";
1819
pub(crate) const LYCHEE_CACHE_FILE: &str = ".lycheecache";
@@ -74,27 +75,21 @@ impl FromStr for StatsFormat {
7475
///
7576
/// This decides over whether to use color,
7677
/// emojis, or plain text for the output.
77-
#[derive(Debug, Deserialize, Default, Clone)]
78+
#[derive(Debug, Deserialize, Default, Clone, Display, EnumIter, EnumString, EnumVariantNames)]
79+
#[non_exhaustive]
7880
pub(crate) enum ResponseFormat {
81+
#[serde(rename = "plain")]
82+
#[strum(serialize = "plain", ascii_case_insensitive)]
7983
Plain,
84+
#[serde(rename = "color")]
85+
#[strum(serialize = "color", ascii_case_insensitive)]
8086
#[default]
8187
Color,
88+
#[serde(rename = "emoji")]
89+
#[strum(serialize = "emoji", ascii_case_insensitive)]
8290
Emoji,
8391
}
8492

85-
impl FromStr for ResponseFormat {
86-
type Err = Error;
87-
88-
fn from_str(mode: &str) -> Result<Self, Self::Err> {
89-
match mode.to_lowercase().as_str() {
90-
"plain" | "plaintext" | "raw" => Ok(ResponseFormat::Plain),
91-
"color" => Ok(ResponseFormat::Color),
92-
"emoji" => Ok(ResponseFormat::Emoji),
93-
_ => Err(anyhow!("Unknown formatter mode {mode}")),
94-
}
95-
}
96-
}
97-
9893
// Macro for generating default functions to be used by serde
9994
macro_rules! default_function {
10095
( $( $name:ident : $T:ty = $e:expr; )* ) => {
@@ -218,7 +213,7 @@ pub(crate) struct Config {
218213

219214
/// Specify the use of a specific web archive.
220215
/// Can be used in combination with `--suggest`
221-
#[arg(long, value_parser = clap::builder::PossibleValuesParser::new(Archive::VARIANTS).map(|s| s.parse::<Archive>().unwrap()))]
216+
#[arg(long, value_parser = PossibleValuesParser::new(Archive::VARIANTS).map(|s| s.parse::<Archive>().unwrap()))]
222217
#[serde(default)]
223218
pub(crate) archive: Option<Archive>,
224219

@@ -413,8 +408,8 @@ separated list of accepted status codes. This example will accept 200, 201,
413408
#[serde(default)]
414409
pub(crate) output: Option<PathBuf>,
415410

416-
/// Set the output display mode. Determines how results are presented in the terminal (color, plain, emoji).
417-
#[arg(long, default_value = "color")]
411+
/// Set the output display mode. Determines how results are presented in the terminal
412+
#[arg(long, default_value = "color", value_parser = PossibleValuesParser::new(ResponseFormat::VARIANTS).map(|s| s.parse::<ResponseFormat>().unwrap()))]
418413
#[serde(default)]
419414
pub(crate) mode: ResponseFormat,
420415

0 commit comments

Comments
 (0)