From bc1c40d670de0e37edf525fccd13a837b5983e7e Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 14 Mar 2019 21:53:07 +0100 Subject: Handle and parse interpolated strings Closes #25 --- dhall/src/binary.rs | 18 +++++++++++++++--- dhall/src/normalize.rs | 4 ++-- 2 files changed, 17 insertions(+), 5 deletions(-) (limited to 'dhall/src') diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index 0f09987..6d0f1e9 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -160,9 +160,21 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { [U64(15), U64(x)] => Ok(NaturalLit(*x as Natural)), [U64(16), U64(x)] => Ok(IntegerLit(*x as Integer)), [U64(16), I64(x)] => Ok(IntegerLit(*x as Integer)), - [U64(18), String(first), _rest..] => { - // TODO: interpolated string - Ok(TextLit(first.clone())) + [U64(18), String(first), rest..] => { + Ok(TextLit(InterpolatedText::from(( + first.clone(), + rest.iter() + .tuples() + .map(|(x, y)| { + let x = cbor_value_to_dhall(&x)?; + let y = match y { + String(s) => s.clone(), + _ => Err(DecodeError::WrongFormatError)?, + }; + Ok((x, y)) + }) + .collect::>()?, + )))) } [U64(25), bindings..] => { let mut tuples = bindings.iter().tuples(); diff --git a/dhall/src/normalize.rs b/dhall/src/normalize.rs index 50a1625..5003ccd 100644 --- a/dhall/src/normalize.rs +++ b/dhall/src/normalize.rs @@ -42,7 +42,7 @@ where (NaturalEven, NaturalLit(n)) => BoolLit(n % 2 == 0), (NaturalOdd, NaturalLit(n)) => BoolLit(n % 2 != 0), (NaturalToInteger, NaturalLit(n)) => IntegerLit(n as isize), - (NaturalShow, NaturalLit(n)) => TextLit(n.to_string()), + (NaturalShow, NaturalLit(n)) => TextLit(n.to_string().into()), (b, App(f, x)) => match (b, normalize_whnf(&f), x) { // fold/build fusion for `Natural` @@ -202,7 +202,7 @@ where (BoolNE, BoolLit(x), BoolLit(y)) => BoolLit(x != y), (NaturalPlus, NaturalLit(x), NaturalLit(y)) => NaturalLit(x + y), (NaturalTimes, NaturalLit(x), NaturalLit(y)) => NaturalLit(x * y), - (TextAppend, TextLit(x), TextLit(y)) => TextLit(x + &y), + (TextAppend, TextLit(x), TextLit(y)) => TextLit(x + y), (ListAppend, ListLit(t1, xs), ListLit(t2, ys)) => { // Drop type annotation if the result is nonempty let t = if xs.is_empty() && ys.is_empty() { -- cgit v1.2.3