cli: leverage TypedValueParser::map() to implement RevisionArg parser

I just found there's a utility for that.
This commit is contained in:
Yuya Nishihara 2024-03-06 19:22:19 +09:00
parent 4dfded2ab7
commit 4cb457eddb

View file

@ -15,7 +15,7 @@
use core::fmt; use core::fmt;
use std::collections::{HashMap, HashSet}; use std::collections::{HashMap, HashSet};
use std::env::{self, ArgsOs, VarError}; use std::env::{self, ArgsOs, VarError};
use std::ffi::{OsStr, OsString}; use std::ffi::OsString;
use std::fmt::Debug; use std::fmt::Debug;
use std::io::{self, Write as _}; use std::io::{self, Write as _};
use std::ops::Deref; use std::ops::Deref;
@ -27,9 +27,11 @@ use std::sync::Arc;
use std::time::SystemTime; use std::time::SystemTime;
use std::{fs, str}; use std::{fs, str};
use clap::builder::{NonEmptyStringValueParser, TypedValueParser, ValueParserFactory}; use clap::builder::{
MapValueParser, NonEmptyStringValueParser, TypedValueParser, ValueParserFactory,
};
use clap::error::{ContextKind, ContextValue}; use clap::error::{ContextKind, ContextValue};
use clap::{Arg, ArgAction, ArgMatches, Command, FromArgMatches}; use clap::{ArgAction, ArgMatches, Command, FromArgMatches};
use indexmap::{IndexMap, IndexSet}; use indexmap::{IndexMap, IndexSet};
use itertools::Itertools; use itertools::Itertools;
use jj_lib::backend::{ChangeId, CommitId, MergedTreeId}; 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<Self::Value, clap::Error> {
let string = NonEmptyStringValueParser::new().parse(cmd, arg, value.to_os_string())?;
Ok(RevisionArg(string))
}
}
impl ValueParserFactory for RevisionArg { impl ValueParserFactory for RevisionArg {
type Parser = RevisionArgValueParser; type Parser = MapValueParser<NonEmptyStringValueParser, fn(String) -> RevisionArg>;
fn value_parser() -> RevisionArgValueParser { fn value_parser() -> Self::Parser {
RevisionArgValueParser NonEmptyStringValueParser::new().map(RevisionArg)
} }
} }