From 5c5b828fcb75aef5708ff95ea1f7994f86bef5b4 Mon Sep 17 00:00:00 2001 From: 魏曹先生 <1992414357@qq.com> Date: Thu, 30 Apr 2026 22:28:07 +0800 Subject: Add `AsPicker` trait for types convertible to `Vec` --- mingling/src/parser/picker.rs | 66 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 66 insertions(+) (limited to 'mingling/src/parser') diff --git a/mingling/src/parser/picker.rs b/mingling/src/parser/picker.rs index 4641a49..915bc35 100644 --- a/mingling/src/parser/picker.rs +++ b/mingling/src/parser/picker.rs @@ -719,3 +719,69 @@ where T::build_enum(name) } } + +pub trait AsPicker +where + Self: Into>, +{ + /// Converts the value into a `Picker` by first converting it into a `Vec`. + fn as_picker(self) -> Picker + where + Self: Sized, + Vec: From, + { + let vec: Vec = self.into(); + Picker { args: vec.into() } + } + + /// Extracts a value for the given flag and returns a `Pick1` builder (no route). + /// + /// The extracted type `TNext` must implement `Pickable` and `Default`. + /// If the flag is not present, the default value for `TNext` is used. + fn pick(self, val: impl Into) -> Pick1 + where + Self: Sized, + TNext: Pickable + Default, + { + let vec: Vec = self.into(); + let picker: Picker = vec.into(); + picker.pick(val) + } + + /// Extracts a value for the given flag, returning the provided default value if not present, + /// and returns a `Pick1` builder (no route). + /// + /// The extracted type `TNext` must implement `Pickable`. + /// If the flag is not present, the provided `or` value is used. + fn pick_or(self, val: impl Into, or: impl Into) -> Pick1 + where + TNext: Pickable, + { + let vec: Vec = self.into(); + let picker: Picker = vec.into(); + picker.pick_or(val, or) + } + + /// Extracts a value for the given flag, storing the provided route if the flag is not present, + /// and returns a `PickWithRoute1` builder (with route). + /// + /// The extracted type `TNext` must implement `Pickable` and `Default`. + /// If the flag is not present, the default value for `TNext` is used and the provided `route` + /// is stored in the returned builder for later error handling. + fn pick_or_route(self, val: impl Into, route: R) -> PickWithRoute1 + where + TNext: Pickable + Default, + { + let vec: Vec = self.into(); + let picker: Picker = vec.into(); + picker.pick_or_route(val, route) + } +} + +// Implement AsPicker for any type that can be converted into a Vec +impl AsPicker for T +where + T: Sized, + Vec: From, +{ +} -- cgit