evobench_tools/serde_types/
key_val.rs

1//! A key/val pair that supports serde and command line parsing for clap.
2
3use std::str::FromStr;
4
5use anyhow::anyhow;
6
7// #[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, clap::Parser)]
8// pub struct KeyVal<V: FromStr>
9// where
10//     V::Err: Into<Box<dyn Error + Send + Sync + 'static>>,
11// {
12//     pub key: String,
13//     pub val: V,
14// }
15
16#[derive(Debug, PartialEq, Clone, serde::Serialize, serde::Deserialize, clap::Parser)]
17#[serde(deny_unknown_fields)]
18pub struct KeyVal {
19    pub key: String,
20    pub val: String,
21}
22
23impl FromStr for KeyVal {
24    type Err = anyhow::Error;
25
26    fn from_str(s: &str) -> Result<Self, Self::Err> {
27        let (key, val) = s
28            .split_once('=')
29            .ok_or_else(|| anyhow!("missing '=' in key-value pair {s:?}"))?;
30
31        Ok(KeyVal {
32            key: key.into(),
33            val: val.into(),
34        })
35    }
36}