ok/jj
1
0
Fork 0
forked from mirrors/jj

templater: add helper methods to map property like Option/Result

These two are common when implementing methods. Fortunately, the
"return-position impl Trait in traits" feature is stabilized in Rust 1.75.0,
so we don't need another variant of TemplateFunction.
This commit is contained in:
Yuya Nishihara 2024-03-21 11:38:23 +09:00
parent 02a04d0d37
commit a3d44485f4

View file

@ -330,6 +330,31 @@ tuple_impls! {
(0 T0, 1 T1, 2 T2, 3 T3)
}
/// `TemplateProperty` adapters that are useful when implementing methods.
pub trait TemplatePropertyExt: TemplateProperty {
/// Translates to a property that will apply fallible `function` to an
/// extracted value.
fn and_then<O, F>(self, function: F) -> TemplateFunction<Self, F>
where
Self: Sized,
F: Fn(Self::Output) -> Result<O, TemplatePropertyError>,
{
TemplateFunction::new(self, function)
}
/// Translates to a property that will apply `function` to an extracted
/// value, leaving `Err` untouched.
fn map<O, F>(self, function: F) -> impl TemplateProperty<Output = O>
where
Self: Sized,
F: Fn(Self::Output) -> O,
{
TemplateFunction::new(self, move |value| Ok(function(value)))
}
}
impl<P: TemplateProperty + ?Sized> TemplatePropertyExt for P {}
/// Adapter to drop template context.
pub struct Literal<O>(pub O);
@ -525,6 +550,9 @@ where
}
}
/// Adapter to apply fallible `function` to the `property`.
///
/// This is usually created by `TemplatePropertyExt::and_then()`/`map()`.
pub struct TemplateFunction<P, F> {
pub property: P,
pub function: F,