diff options
author | Nadrieril | 2019-03-14 21:53:07 +0100 |
---|---|---|
committer | Nadrieril | 2019-03-14 21:53:07 +0100 |
commit | bc1c40d670de0e37edf525fccd13a837b5983e7e (patch) | |
tree | 1f928e89339a6c25a5b0a60a4a563b8cce01a87c /dhall | |
parent | 8c34c3bbc2fc520cce78fd445bdbc3192ce91abf (diff) |
Handle and parse interpolated strings
Closes #25
Diffstat (limited to 'dhall')
-rw-r--r-- | dhall/Cargo.toml | 3 | ||||
-rw-r--r-- | dhall/src/binary.rs | 18 | ||||
-rw-r--r-- | dhall/src/normalize.rs | 4 | ||||
-rw-r--r-- | dhall/tests/macros.rs | 8 | ||||
-rw-r--r-- | dhall/tests/tests.rs | 10 |
5 files changed, 30 insertions, 13 deletions
diff --git a/dhall/Cargo.toml b/dhall/Cargo.toml index 12622f8..fd462b1 100644 --- a/dhall/Cargo.toml +++ b/dhall/Cargo.toml @@ -15,3 +15,6 @@ term-painter = "0.2.3" serde_cbor = "0.9.0" dhall_core = { path = "../dhall_core" } dhall_generator = { path = "../dhall_generator" } + +[dev-dependencies] +pretty_assertions = "0.6.1" 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<ParsedExpr, DecodeError> { [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::<Result<_, _>>()?, + )))) } [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() { diff --git a/dhall/tests/macros.rs b/dhall/tests/macros.rs index 1d90e87..4109f84 100644 --- a/dhall/tests/macros.rs +++ b/dhall/tests/macros.rs @@ -1,5 +1,7 @@ +use pretty_assertions::assert_eq as assert_eq_pretty; + #[macro_export] -macro_rules! assert_eq_ { +macro_rules! assert_eq_display { ($left:expr, $right:expr) => {{ match (&$left, &$right) { (left_val, right_val) => { @@ -104,7 +106,7 @@ pub fn run_test(base_path: &str, feature: Feature, expected: ExpectedResult) { let expected = dhall::binary::decode(&data).unwrap(); let expected = dhall::imports::panic_imports(&expected); - assert_eq!(expr, expected); + assert_eq_pretty!(expr, expected); } (Feature::Parser, ExpectedResult::Failure) => { let file_path = base_path.to_owned() + ".dhall"; @@ -120,7 +122,7 @@ pub fn run_test(base_path: &str, feature: Feature, expected: ExpectedResult) { let expr = read_dhall_file(&expr_file_path).unwrap(); let expected = read_dhall_file(&expected_file_path).unwrap(); - assert_eq_!( + assert_eq_display!( normalize::<_, X, _>(&expr), normalize::<_, X, _>(&expected) ); diff --git a/dhall/tests/tests.rs b/dhall/tests/tests.rs index 51df149..24e8aa4 100644 --- a/dhall/tests/tests.rs +++ b/dhall/tests/tests.rs @@ -179,15 +179,15 @@ make_spec_test!(parser, spec_parser_success_builtins, "parser/success/builtins") make_spec_test!(parser, spec_parser_success_doubleQuotedString, "parser/success/doubleQuotedString"); // make_spec_test!(parser, spec_parser_success_environmentVariables, "parser/success/environmentVariables"); // make_spec_test!(parser, spec_parser_success_escapedDoubleQuotedString, "parser/success/escapedDoubleQuotedString"); -// make_spec_test!(parser, spec_parser_success_escapedSingleQuotedString, "parser/success/escapedSingleQuotedString"); +make_spec_test!(parser, spec_parser_success_escapedSingleQuotedString, "parser/success/escapedSingleQuotedString"); make_spec_test!(parser, spec_parser_success_fields, "parser/success/fields"); make_spec_test!(parser, spec_parser_success_forall, "parser/success/forall"); make_spec_test!(parser, spec_parser_success_functionType, "parser/success/functionType"); make_spec_test!(parser, spec_parser_success_identifier, "parser/success/identifier"); make_spec_test!(parser, spec_parser_success_ifThenElse, "parser/success/ifThenElse"); // make_spec_test!(parser, spec_parser_success_importAlt, "parser/success/importAlt"); -// make_spec_test!(parser, spec_parser_success_interpolatedDoubleQuotedString, "parser/success/interpolatedDoubleQuotedString"); -// make_spec_test!(parser, spec_parser_success_interpolatedSingleQuotedString, "parser/success/interpolatedSingleQuotedString"); +make_spec_test!(parser, spec_parser_success_interpolatedDoubleQuotedString, "parser/success/interpolatedDoubleQuotedString"); +make_spec_test!(parser, spec_parser_success_interpolatedSingleQuotedString, "parser/success/interpolatedSingleQuotedString"); make_spec_test!(parser, spec_parser_success_label, "parser/success/label"); make_spec_test!(parser, spec_parser_success_lambda, "parser/success/lambda"); // make_spec_test!(parser, spec_parser_success_largeExpression, "parser/success/largeExpression"); @@ -206,9 +206,9 @@ make_spec_test!(parser, spec_parser_success_operators, "parser/success/operators // make_spec_test!(parser, spec_parser_success_quotedPaths, "parser/success/quotedPaths"); make_spec_test!(parser, spec_parser_success_record, "parser/success/record"); make_spec_test!(parser, spec_parser_success_reservedPrefix, "parser/success/reservedPrefix"); -// make_spec_test!(parser, spec_parser_success_singleQuotedString, "parser/success/singleQuotedString"); +make_spec_test!(parser, spec_parser_success_singleQuotedString, "parser/success/singleQuotedString"); make_spec_test!(parser, spec_parser_success_sort, "parser/success/sort"); -// make_spec_test!(parser, spec_parser_success_template, "parser/success/template"); +make_spec_test!(parser, spec_parser_success_template, "parser/success/template"); make_spec_test!(parser, spec_parser_success_unicodeComment, "parser/success/unicodeComment"); make_spec_test!(parser, spec_parser_success_unicodeDoubleQuotedString, "parser/success/unicodeDoubleQuotedString"); // make_spec_test!(parser, spec_parser_success_unicodePaths, "parser/success/unicodePaths"); |