diff options
| author | 魏曹先生 <1992414357@qq.com> | 2026-04-15 17:22:57 +0800 |
|---|---|---|
| committer | 魏曹先生 <1992414357@qq.com> | 2026-04-15 17:22:57 +0800 |
| commit | 5e3099864d0580485f520f7285fbaf5c286dbf09 (patch) | |
| tree | cd9bb2e05d385d40611747405ab813c9386fde22 /mingling/src/parser/picker.rs | |
| parent | 23f8008778bfb7a0e74f9eed3d15bee1383eed0b (diff) | |
Add after_or_route method to picker builder
Diffstat (limited to 'mingling/src/parser/picker.rs')
| -rw-r--r-- | mingling/src/parser/picker.rs | 30 |
1 files changed, 28 insertions, 2 deletions
diff --git a/mingling/src/parser/picker.rs b/mingling/src/parser/picker.rs index d69e27a..73c3b8d 100644 --- a/mingling/src/parser/picker.rs +++ b/mingling/src/parser/picker.rs @@ -192,14 +192,40 @@ macro_rules! define_pick_structs { /// Takes a closure that receives the last extracted value and returns a new value of the same type. /// The transformed value replaces the original value in the builder. /// This method can be used to modify or validate the extracted value before final unpacking. - pub fn after<F>(mut self, edit: F) -> Self + pub fn after<F>(mut self, mut edit: F) -> Self where - F: Fn($final) -> $final, + F: FnMut($final) -> $final, { self.$final_val = edit(self.$final_val); self } + /// Applies a transformation to the last extracted value, storing a route if the transformation fails. + /// + /// Takes a closure that receives a reference to the last extracted value and returns a `Result`. + /// If the closure returns `Ok(new_value)`, the new value replaces the original value in the builder. + /// If the closure returns `Err(route)`, the provided `route` is stored in the builder for later error handling. + /// If a route was already stored from a previous `pick_or_route` call, the existing route is preserved. + pub fn after_or_route<F>(mut self, mut edit: F) -> Self + where + F: FnMut(&$final) -> Result<$final, R>, + { + let value = &self.$final_val; + match edit(value) { + Ok(new_value) => { + self.$final_val = new_value; + } + Err(err_route) => { + let new_route = match self.route { + Some(existing_route) => Some(existing_route), + None => Some(err_route), + }; + self.route = new_route; + } + } + self + } + /// Applies an operation to the parsed arguments and returns the modified `Picker`. /// /// Takes a closure that receives the current `Argument` and returns a new `Argument`. |
