Skip to main content

#
typed_derive/
fn_parser.rs

    1use std::error::Error;
    2
    3use clap::Args;
    4
    5#[derive(Args, Debug)]
    6pub(crate) struct FnParser {
    7    /// Hand-written parser for tuples
    8    #[arg(short = 'D', value_name = "KEY=VALUE", value_parser = parse_key_val::<String, i32>)]
    9    defines: Vec<(String, i32)>,
    10}
    11
    12/// Parse a single key-value pair
    13fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
    14where
    15    T: std::str::FromStr,
    16    T::Err: Error + Send + Sync + 'static,
    17    U: std::str::FromStr,
    18    U::Err: Error + Send + Sync + 'static,
    19{
    20    let pos = s
    21        .find('=')
    22        .ok_or_else(|| format!("invalid KEY=value: no `=` found in `{s}`"))?;
    23    Ok((s[..pos].parse()?, s[pos + 1..].parse()?))
    24}