freta/client/argparse.rs
1// Copyright (C) Microsoft Corporation. All rights reserved.
2
3use std::{error::Error, result::Result, str::FromStr};
4
5/// Parse a single key-value pair of `X=Y` into a typed tuple of `(X, Y)`.
6///
7/// # Errors
8/// Returns an `Err` if any of the keys or values cannot be parsed or if no `=` is found.
9pub fn parse_key_val<T, U>(s: &str) -> Result<(T, U), Box<dyn Error + Send + Sync + 'static>>
10where
11 T: FromStr,
12 T::Err: Error + Send + Sync + 'static,
13 U: FromStr,
14 U::Err: Error + Send + Sync + 'static,
15{
16 if let Some((key, value)) = s.split_once('=') {
17 Ok((key.parse()?, value.parse()?))
18 } else {
19 Err(format!("invalid KEY=value: no `=` found in `{s}`").into())
20 }
21}