aboutsummaryrefslogtreecommitdiff
path: root/mingling
diff options
context:
space:
mode:
Diffstat (limited to 'mingling')
-rw-r--r--mingling/src/parser/picker.rs30
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`.