diff options
Diffstat (limited to 'dhall/src')
-rw-r--r-- | dhall/src/phase/binary.rs | 21 | ||||
-rw-r--r-- | dhall/src/tests.rs | 33 |
2 files changed, 53 insertions, 1 deletions
diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index bfe217f..cab3c8e 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -48,10 +48,21 @@ fn cbor_value_to_dhall( Bool(b) => BoolLit(*b), Array(vec) => match vec.as_slice() { [String(l), U64(n)] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let l = Label::from(l.as_str()); Var(V(l, *n as usize)) } [U64(0), f, args..] => { + if args.is_empty() { + Err(DecodeError::WrongFormatError( + "Function application must have at least one argument" + .to_owned(), + ))? + } let mut f = cbor_value_to_dhall(&f)?; for a in args { let a = cbor_value_to_dhall(&a)?; @@ -65,6 +76,11 @@ fn cbor_value_to_dhall( Lam(Label::from("_"), x, y) } [U64(1), String(l), x, y] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); @@ -76,6 +92,11 @@ fn cbor_value_to_dhall( Pi(Label::from("_"), x, y) } [U64(2), String(l), x, y] => { + if l.as_str() == "_" { + Err(DecodeError::WrongFormatError( + "`_` variable was encoded incorrectly".to_owned(), + ))? + } let x = cbor_value_to_dhall(&x)?; let y = cbor_value_to_dhall(&y)?; let l = Label::from(l.as_str()); diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 8b32fb4..da48d15 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -45,6 +45,7 @@ pub enum Feature { Parser, Printer, BinaryEncoding, + BinaryDecoding, Import, Normalization, AlphaNormalization, @@ -84,6 +85,7 @@ pub fn run_test( Parser => "parser/", Printer => "parser/", BinaryEncoding => "parser/", + BinaryDecoding => "binary-decode/", Import => "import/", Normalization => "normalization/", AlphaNormalization => "alpha-normalization/", @@ -94,6 +96,24 @@ pub fn run_test( "../dhall-lang/tests/".to_owned() + feature_prefix + base_path; match status { Success => { + match feature { + BinaryDecoding => { + let expr_file_path = base_path.clone() + "A.dhallb"; + let expr_file_path = PathBuf::from(&expr_file_path); + let mut expr_data = Vec::new(); + { + File::open(&expr_file_path)? + .read_to_end(&mut expr_data)?; + } + let expr = Parsed::parse_binary(&expr_data)?; + let expected_file_path = base_path + "B.dhall"; + let expected = parse_file_str(&expected_file_path)?; + assert_eq_pretty!(expr, expected); + + return Ok(()); + } + _ => {} + } let expr_file_path = base_path.clone() + "A.dhall"; let expr = parse_file_str(&expr_file_path)?; @@ -164,7 +184,9 @@ pub fn run_test( .normalize(); match feature { - Parser | Printer | BinaryEncoding => unreachable!(), + Parser | Printer | BinaryEncoding | BinaryDecoding => { + unreachable!() + } Import => { let expr = expr.skip_typecheck().normalize(); assert_eq_display!(expr, expected); @@ -201,6 +223,15 @@ pub fn run_test( } } Printer | BinaryEncoding => unreachable!(), + BinaryDecoding => { + let expr_file_path = file_path + "b"; + let mut expr_data = Vec::new(); + { + File::open(&PathBuf::from(&expr_file_path))? + .read_to_end(&mut expr_data)?; + } + Parsed::parse_binary(&expr_data).unwrap_err(); + } Import => { parse_file_str(&file_path)?.resolve().unwrap_err(); } |