From 097d3a0e6cc5dd3074753fb140e392214f61c4d0 Mon Sep 17 00:00:00 2001 From: Yuya Nishihara Date: Sun, 5 Feb 2023 19:35:34 +0900 Subject: [PATCH] templater: add generic wrappers for Option Option

allows us to embed optional argument property in tuple. let maybe_arg = maybe_pair.map(|pair| parse(...))?; chain_properties((self, maybe_arg), |_: &(Context, Option<_>)| ...) For one optional argument, we can instead switch the property functions: if let Some(pair) = maybe_pair { let arg = pair.map(|pair| parse(...))?; chain_properties((self, arg), |_: &(Context, _)| ...) } else { chain_properties(self, |_: &Context| ...) } If we have various combinations of optional arguments, using Option

would be better. --- src/templater.rs | 8 ++++++++ 1 file changed, 8 insertions(+) diff --git a/src/templater.rs b/src/templater.rs index e965ed2e7..a83aa66ce 100644 --- a/src/templater.rs +++ b/src/templater.rs @@ -210,6 +210,14 @@ impl + ?Sized> TemplateProperty for Box

{ } } +impl> TemplateProperty for Option

{ + type Output = Option; + + fn extract(&self, context: &C) -> Self::Output { + self.as_ref().map(|property| property.extract(context)) + } +} + // Implement TemplateProperty for tuples macro_rules! tuple_impls { ($( ( $($n:tt $T:ident),+ ) )+) => {