diff options
author | Nadrieril | 2019-04-20 23:07:00 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-20 23:07:00 +0200 |
commit | 5812a53cc9241703bd35586da0036dc170ab3721 (patch) | |
tree | 7889925889ef89cc62b5cf102a51490fec47dab8 /dhall_core | |
parent | 6a763de51fc01067e87a5abc1261e20e32c13483 (diff) |
Implement more normalization simplifications
Diffstat (limited to 'dhall_core')
-rw-r--r-- | dhall_core/src/text.rs | 51 |
1 files changed, 24 insertions, 27 deletions
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<SubExpr> { @@ -33,6 +32,16 @@ pub enum InterpolatedTextContents<SubExpr> { Expr(SubExpr), } +impl<SubExpr> InterpolatedTextContents<SubExpr> { + pub fn is_empty(&self) -> bool { + use InterpolatedTextContents::{Expr, Text}; + match self { + Expr(_) => false, + Text(s) => s.is_empty(), + } + } +} + impl<SubExpr> InterpolatedText<SubExpr> { pub fn traverse_ref<'a, SubExpr2, E, F>( &'a self, @@ -51,13 +60,6 @@ impl<SubExpr> InterpolatedText<SubExpr> { }) } - 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<Item = InterpolatedTextContents<SubExpr>> + 'a @@ -65,24 +67,26 @@ impl<SubExpr> InterpolatedText<SubExpr> { 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<Item = InterpolatedTextContents<SubExpr>> { 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<SubExpr> FromIterator<InterpolatedTextContents<SubExpr>> res } } - -impl<SubExpr: Clone> Add for &InterpolatedText<SubExpr> { - type Output = InterpolatedText<SubExpr>; - fn add(self, rhs: &InterpolatedText<SubExpr>) -> Self::Output { - self.iter().chain(rhs.iter()).collect() - } -} |