From 5812a53cc9241703bd35586da0036dc170ab3721 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 20 Apr 2019 23:07:00 +0200 Subject: Implement more normalization simplifications --- dhall_core/src/text.rs | 51 ++++++++++++++++++++++++-------------------------- 1 file changed, 24 insertions(+), 27 deletions(-) (limited to 'dhall_core') diff --git a/dhall_core/src/text.rs b/dhall_core/src/text.rs index ff5c441..83643d9 100644 --- a/dhall_core/src/text.rs +++ b/dhall_core/src/text.rs @@ -1,5 +1,4 @@ use std::iter::FromIterator; -use std::ops::Add; #[derive(Debug, Clone, PartialEq, Eq)] pub struct InterpolatedText { @@ -33,6 +32,16 @@ pub enum InterpolatedTextContents { Expr(SubExpr), } +impl InterpolatedTextContents { + pub fn is_empty(&self) -> bool { + use InterpolatedTextContents::{Expr, Text}; + match self { + Expr(_) => false, + Text(s) => s.is_empty(), + } + } +} + impl InterpolatedText { pub fn traverse_ref<'a, SubExpr2, E, F>( &'a self, @@ -51,13 +60,6 @@ impl InterpolatedText { }) } - pub fn as_ref(&self) -> InterpolatedText<&SubExpr> { - InterpolatedText { - head: self.head.clone(), - tail: self.tail.iter().map(|(e, s)| (e, s.clone())).collect(), - } - } - pub fn iter<'a>( &'a self, ) -> impl Iterator> + 'a @@ -65,24 +67,26 @@ impl InterpolatedText { SubExpr: Clone, { use std::iter::once; - once(InterpolatedTextContents::Text(self.head.clone())).chain( - self.tail.iter().flat_map(|(e, s)| { - once(InterpolatedTextContents::Expr(SubExpr::clone(e))) - .chain(once(InterpolatedTextContents::Text(s.clone()))) - }), - ) + use InterpolatedTextContents::{Expr, Text}; + once(Text(self.head.clone())) + .chain(self.tail.iter().flat_map(|(e, s)| { + once(Expr(SubExpr::clone(e))).chain(once(Text(s.clone()))) + })) + .filter(|c| !c.is_empty()) } pub fn into_iter( self, ) -> impl Iterator> { use std::iter::once; - once(InterpolatedTextContents::Text(self.head)).chain( - self.tail.into_iter().flat_map(|(e, s)| { - once(InterpolatedTextContents::Expr(e)) - .chain(once(InterpolatedTextContents::Text(s))) - }), - ) + use InterpolatedTextContents::{Expr, Text}; + once(Text(self.head)) + .chain( + self.tail + .into_iter() + .flat_map(|(e, s)| once(Expr(e)).chain(once(Text(s)))), + ) + .filter(|c| !c.is_empty()) } } @@ -110,10 +114,3 @@ impl FromIterator> res } } - -impl Add for &InterpolatedText { - type Output = InterpolatedText; - fn add(self, rhs: &InterpolatedText) -> Self::Output { - self.iter().chain(rhs.iter()).collect() - } -} -- cgit v1.2.3