From 2dfaed38b835e7febb51c0e731ea00cfb85f0d71 Mon Sep 17 00:00:00 2001 From: Andrew Phillips Date: Thu, 14 Aug 2025 17:03:55 -0300 Subject: [PATCH] fix: implement FromStr for NumberOrString and KeyValue to fix clap parsing errors Co-authored-by: aider (openai/andrew/openrouter/qwen/qwen3-coder) --- src/args.rs | 27 ++++++++++++++++++++++++++- 1 file changed, 26 insertions(+), 1 deletion(-) diff --git a/src/args.rs b/src/args.rs index 71e0857..a1cd19d 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,7 +1,8 @@ use std::path::PathBuf; +use std::str::FromStr; use clap::*; -use serde::{Deserialize, Serialize}; +use anyhow::{Error, anyhow}; /** * Main struct for command-line arguments. @@ -17,6 +18,7 @@ pub struct Args { pub options: OptionsArgs, #[arg(help("A list of either item IDs or tags"))] + #[arg(value_parser = clap::value_parser!(NumberOrString))] pub ids_or_tags: Vec, } @@ -75,6 +77,7 @@ pub struct ItemArgs { #[arg(help( "Set metadata for the item using the format KEY=[VALUE], the metadata will be removed if VALUE is not provided" ))] + #[arg(value_parser = clap::value_parser!(KeyValue))] pub meta: Vec, #[arg(help_heading("Item Options"), long, env("KEEP_DIGEST"))] @@ -141,6 +144,15 @@ pub enum NumberOrString { Str(String), } +impl FromStr for NumberOrString { + type Err = Error; + fn from_str(s: &str) -> Result { + Ok(s.parse::() + .map(NumberOrString::Number) + .unwrap_or_else(|_| NumberOrString::Str(s.to_string()))) + } +} + /** * Struct for key-value pairs. */ @@ -149,3 +161,16 @@ pub struct KeyValue { pub key: String, pub value: String, } + +impl FromStr for KeyValue { + type Err = Error; + fn from_str(s: &str) -> Result { + match s.split_once('=') { + Some(kv) => Ok(KeyValue { + key: kv.0.to_string(), + value: kv.1.to_string(), + }), + None => Err(anyhow!("Unable to parse key=value pair")), + } + } +}