clap_offer/
offer_arg_values.rs

1
2#[derive(Clone, Debug)]
3pub struct OfferValues(Vec<clap::builder::PossibleValue>);
4
5impl OfferValues {
6    pub fn new(values: impl Into<OfferValues>) -> Self {
7        values.into()
8    }
9}
10
11impl clap::builder::TypedValueParser for OfferValues {
12    type Value = String;
13
14    fn parse_ref(
15        &self,
16        cmd: &clap::Command,
17        arg: Option<&clap::Arg>,
18        value: &std::ffi::OsStr,
19    ) -> Result<Self::Value, clap::Error> {
20        clap::builder::TypedValueParser::parse(self, cmd, arg, value.to_owned())
21    }
22
23    fn parse(
24        &self,
25        cmd: &clap::Command,
26        _arg: Option<&clap::Arg>,
27        value: std::ffi::OsString,
28    ) -> Result<String, clap::Error> {
29        value.into_string().map_err(|_| {
30            clap::Error::new(clap::error::ErrorKind::InvalidUtf8).with_cmd(cmd)
31        })
32    }
33
34    fn possible_values(
35        &self,
36    ) -> Option<Box<dyn Iterator<Item = clap::builder::PossibleValue> + '_>> {
37        Some(Box::new(self.0.iter().cloned()))
38    }
39}
40
41impl<I, T> From<I> for OfferValues
42where
43    I: IntoIterator<Item = T>,
44    T: Into<clap::builder::PossibleValue>,
45{
46    fn from(values: I) -> Self {
47        Self(values.into_iter().map(|t| t.into()).collect())
48    }
49}