1
0
mirror of https://github.com/sharkdp/bat.git synced 2025-02-21 12:28:30 +00:00

refactor: Parse, don't validate

This commit is contained in:
Ed Page 2022-09-01 21:06:39 -05:00 committed by David Peter
parent 3d398b35c3
commit 08c91a116c

View File

@ -152,11 +152,11 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
.overrides_with("diff-context") .overrides_with("diff-context")
.takes_value(true) .takes_value(true)
.value_name("N") .value_name("N")
.validator( .value_parser(
|n| { |n: &str| {
n.parse::<usize>() n.parse::<usize>()
.map_err(|_| "must be a number") .map_err(|_| "must be a number")
.map(|_| ()) // Convert to Result<(), &str> .map(|_| n.to_owned()) // Convert to Result<String, &str>
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
}, // Convert to Result<(), String> }, // Convert to Result<(), String>
) )
@ -173,11 +173,11 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
.overrides_with("tabs") .overrides_with("tabs")
.takes_value(true) .takes_value(true)
.value_name("T") .value_name("T")
.validator( .value_parser(
|t| { |t: &str| {
t.parse::<u32>() t.parse::<u32>()
.map_err(|_t| "must be a number") .map_err(|_t| "must be a number")
.map(|_t| ()) // Convert to Result<(), &str> .map(|_t| t.to_owned()) // Convert to Result<String, &str>
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
}, // Convert to Result<(), String> }, // Convert to Result<(), String>
) )
@ -208,15 +208,15 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
.value_name("width") .value_name("width")
.hide_short_help(true) .hide_short_help(true)
.allow_hyphen_values(true) .allow_hyphen_values(true)
.validator( .value_parser(
|t| { |t: &str| {
let is_offset = t.starts_with('+') || t.starts_with('-'); let is_offset = t.starts_with('+') || t.starts_with('-');
t.parse::<i32>() t.parse::<i32>()
.map_err(|_e| "must be an offset or number") .map_err(|_e| "must be an offset or number")
.and_then(|v| if v == 0 && !is_offset { .and_then(|v| if v == 0 && !is_offset {
Err("terminal width cannot be zero") Err("terminal width cannot be zero")
} else { } else {
Ok(()) Ok(t.to_owned())
}) })
.map_err(|e| e.to_string()) .map_err(|e| e.to_string())
}) })
@ -400,7 +400,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
.overrides_with("plain") .overrides_with("plain")
.overrides_with("number") .overrides_with("number")
// Cannot use claps built in validation because we have to turn off clap's delimiters // Cannot use claps built in validation because we have to turn off clap's delimiters
.validator(|val| { .value_parser(|val: &str| {
let mut invalid_vals = val.split(',').filter(|style| { let mut invalid_vals = val.split(',').filter(|style| {
!&[ !&[
"auto", "auto",
@ -422,7 +422,7 @@ pub fn build_app(interactive_output: bool) -> Command<'static> {
if let Some(invalid) = invalid_vals.next() { if let Some(invalid) = invalid_vals.next() {
Err(format!("Unknown style, '{}'", invalid)) Err(format!("Unknown style, '{}'", invalid))
} else { } else {
Ok(()) Ok(val.to_owned())
} }
}) })
.help( .help(