From f1c3d1d7487fbb18b228a1082fc1c966f34b6dc3 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 3 Sep 2019 16:44:02 +0200 Subject: Add mapping functions to InterpolatedTextContents --- dhall_syntax/src/core/text.rs | 46 +++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'dhall_syntax') 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 InterpolatedTextContents { Text(s) => s.is_empty(), } } + + pub fn traverse_ref<'a, SubExpr2, E, F>( + &'a self, + mut f: F, + ) -> Result, E> + where + F: FnMut(&'a SubExpr) -> Result, + { + 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 + 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 InterpolatedText { -- cgit v1.2.3