From 4cb457eddba95fb67417d49ef3fa75942424bc9c Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Wed, 6 Mar 2024 19:22:19 +0900 Subject: [PATCH] cli: leverage TypedValueParser::map() to implement RevisionArg parser I just found there's a utility for that. --- cli/src/cli_util.rs | 31 ++++++++----------------------- 1 file changed, 8 insertions(+), 23 deletions(-) diff --git a/cli/src/cli_util.rs b/cli/src/cli_util.rs index 419c08bb57..895ad69d61 100644 --- a/cli/src/cli_util.rs +++ b/cli/src/cli_util.rs @@ -15,7 +15,7 @@ use core::fmt; use std::collections::{HashMap, HashSet}; use std::env::{self, ArgsOs, VarError}; -use std::ffi::{OsStr, OsString}; +use std::ffi::OsString; use std::fmt::Debug; use std::io::{self, Write as _}; use std::ops::Deref; @@ -27,9 +27,11 @@ use std::sync::Arc; use std::time::SystemTime; use std::{fs, str}; -use clap::builder::{NonEmptyStringValueParser, TypedValueParser, ValueParserFactory}; +use clap::builder::{ + MapValueParser, NonEmptyStringValueParser, TypedValueParser, ValueParserFactory, +}; use clap::error::{ContextKind, ContextValue}; -use clap::{Arg, ArgAction, ArgMatches, Command, FromArgMatches}; +use clap::{ArgAction, ArgMatches, Command, FromArgMatches}; use indexmap::{IndexMap, IndexSet}; use itertools::Itertools; use jj_lib::backend::{ChangeId, CommitId, MergedTreeId}; @@ -2161,28 +2163,11 @@ impl Deref for RevisionArg { } } -#[derive(Clone)] -pub struct RevisionArgValueParser; - -impl TypedValueParser for RevisionArgValueParser { - type Value = RevisionArg; - - fn parse_ref( - &self, - cmd: &Command, - arg: Option<&Arg>, - value: &OsStr, - ) -> Result { - let string = NonEmptyStringValueParser::new().parse(cmd, arg, value.to_os_string())?; - Ok(RevisionArg(string)) - } -} - impl ValueParserFactory for RevisionArg { - type Parser = RevisionArgValueParser; + type Parser = MapValueParser RevisionArg>; - fn value_parser() -> RevisionArgValueParser { - RevisionArgValueParser + fn value_parser() -> Self::Parser { + NonEmptyStringValueParser::new().map(RevisionArg) } }