From 6cbe21b84ffd274f92791ab8dbf9af6527978688 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 21 Mar 2019 20:30:25 +0100 Subject: oops --- dhall_core/src/text.rs | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) create mode 100644 dhall_core/src/text.rs (limited to 'dhall_core/src/text.rs') diff --git a/dhall_core/src/text.rs b/dhall_core/src/text.rs new file mode 100644 index 0000000..cd5fef3 --- /dev/null +++ b/dhall_core/src/text.rs @@ -0,0 +1,92 @@ +use crate::*; +use std::iter::FromIterator; +use std::ops::Add; +use std::rc::Rc; + +#[derive(Debug, Clone, PartialEq)] +pub struct InterpolatedText { + head: String, + tail: Vec<(Rc>, String)>, +} + +impl From<(String, Vec<(Rc>, String)>)> + for InterpolatedText +{ + fn from(x: (String, Vec<(Rc>, String)>)) -> Self { + InterpolatedText { + head: x.0, + tail: x.1, + } + } +} + +impl From for InterpolatedText { + fn from(s: String) -> Self { + InterpolatedText { + head: s, + tail: vec![], + } + } +} + +#[derive(Debug, Clone)] +pub enum InterpolatedTextContents<'a, Note, Embed> { + Text(&'a str), + Expr(SubExpr), +} + +impl InterpolatedText { + pub fn map(&self, mut f: F) -> InterpolatedText + where + F: FnMut(&Rc>) -> Rc>, + { + InterpolatedText { + head: self.head.clone(), + tail: self.tail.iter().map(|(e, s)| (f(e), s.clone())).collect(), + } + } + + pub fn iter(&self) -> impl Iterator> { + use std::iter::once; + once(InterpolatedTextContents::Text(self.head.as_ref())).chain( + self.tail.iter().flat_map(|(e, s)| { + once(InterpolatedTextContents::Expr(Rc::clone(e))) + .chain(once(InterpolatedTextContents::Text(s))) + }), + ) + } +} + +impl<'a, N: 'a, E: 'a> FromIterator> + for InterpolatedText +{ + fn from_iter(iter: T) -> Self + where + T: IntoIterator>, + { + let mut res = InterpolatedText { + head: "".to_owned(), + tail: vec![], + }; + // let mut empty_string = "".to_owned(); + let mut crnt_str = &mut res.head; + for x in iter.into_iter() { + match x { + InterpolatedTextContents::Text(s) => crnt_str.push_str(s), + InterpolatedTextContents::Expr(e) => { + // crnt_str = &mut empty_string; + res.tail.push((e.clone(), "".to_owned())); + crnt_str = &mut res.tail.last_mut().unwrap().1; + } + } + } + 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