feat: Support key without value in custom fields parser

Signed-off-by: Awiteb <a@4rs.nl>
This commit is contained in:
Awiteb 2024-08-17 14:47:57 +00:00
parent 04614aff57
commit bbc2a7a944
Signed by: awiteb
GPG key ID: 3F6B55640AA6682F

View file

@ -19,12 +19,14 @@ use crate::{LprsError, LprsResult};
/// Parse the key & value arguments.
/// ## Errors
/// - If the argument value syntax not `key=value`
pub fn kv_parser(value: &str) -> LprsResult<(String, String)> {
pub fn kv_parser(value: &str) -> LprsResult<(String, Option<String>)> {
if let Some((key, value)) = value.split_once('=') {
Ok((key.trim().to_owned(), value.trim().to_owned()))
} else {
Ok((key.trim().to_owned(), Some(value.trim().to_owned())))
} else if value.trim().is_empty() {
Err(LprsError::ArgParse(
"There is no value, the syntax is `KEY=VALUE`".to_owned(),
"Invalid key, the syntax is `KEY(=VALUE)?`".to_owned(),
))
} else {
Ok((value.trim().to_owned(), None))
}
}