diff options
author | Nadrieril | 2019-09-03 16:44:02 +0200 |
---|---|---|
committer | Nadrieril | 2019-09-03 17:30:11 +0200 |
commit | f1c3d1d7487fbb18b228a1082fc1c966f34b6dc3 (patch) | |
tree | a4b8409d0d89d741a11b7227388ad0d0b1079ed7 /dhall_syntax/src | |
parent | 31a03bf9140a2fdf5eb52d0998d2d41efaf0d610 (diff) |
Add mapping functions to InterpolatedTextContents
Diffstat (limited to '')
-rw-r--r-- | dhall_syntax/src/core/text.rs | 46 |
1 files changed, 46 insertions, 0 deletions
diff --git a/dhall_syntax/src/core/text.rs b/dhall_syntax/src/core/text.rs index e17f00f..fb390ee 100644 --- a/dhall_syntax/src/core/text.rs +++ b/dhall_syntax/src/core/text.rs @@ -40,6 +40,52 @@ impl<SubExpr> InterpolatedTextContents<SubExpr> { Text(s) => s.is_empty(), } } + + pub fn traverse_ref<'a, SubExpr2, E, F>( + &'a self, + mut f: F, + ) -> Result<InterpolatedTextContents<SubExpr2>, E> + where + F: FnMut(&'a SubExpr) -> Result<SubExpr2, E>, + { + use InterpolatedTextContents::{Expr, Text}; + Ok(match self { + Expr(e) => Expr(f(e)?), + Text(s) => Text(s.clone()), + }) + } + pub fn traverse_mut<'a, E, F>(&'a mut self, mut f: F) -> Result<(), E> + where + F: FnMut(&'a mut SubExpr) -> Result<(), E>, + { + use InterpolatedTextContents::Expr; + if let Expr(e) = self { + f(e)?; + } + Ok(()) + } + pub fn map_ref<'a, SubExpr2, F>( + &'a self, + mut f: F, + ) -> InterpolatedTextContents<SubExpr2> + where + F: FnMut(&'a SubExpr) -> SubExpr2, + { + use InterpolatedTextContents::{Expr, Text}; + match self { + Expr(e) => Expr(f(e)), + Text(s) => Text(s.clone()), + } + } + pub fn map_mut<'a, F>(&'a mut self, mut f: F) + where + F: FnMut(&'a mut SubExpr), + { + use InterpolatedTextContents::Expr; + if let Expr(e) = self { + f(e); + } + } } impl<SubExpr> InterpolatedText<SubExpr> { |