From fe38fd6a8859447a154a5698a3e21d9203262be2 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 22 Mar 2019 00:01:30 +0100 Subject: Parse a lot more of the import types --- dhall/src/binary.rs | 93 +++++++++++++++++++++++++++++++++++++++++++++------ dhall/src/imports.rs | 1 + dhall/tests/parser.rs | 14 ++++---- 3 files changed, 91 insertions(+), 17 deletions(-) (limited to 'dhall') diff --git a/dhall/src/binary.rs b/dhall/src/binary.rs index 8fe66f5..a028e82 100644 --- a/dhall/src/binary.rs +++ b/dhall/src/binary.rs @@ -1,7 +1,6 @@ use dhall_core::*; use itertools::*; use serde_cbor::value::value as cbor; -use std::path::PathBuf; use std::rc::Rc; type ParsedExpr = Rc>; @@ -180,18 +179,92 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { .collect::>()?, ))) } - [U64(24), Null, U64(0), U64(3), rest..] => { - let mut path = PathBuf::new(); - for s in rest { - match s { - String(s) => path.push(s), - _ => Err(DecodeError::WrongFormatError)?, + [U64(24), _hash, U64(mode), U64(scheme), rest..] => { + let mode = match mode { + 1 => ImportMode::RawText, + _ => ImportMode::Code, + }; + let mut rest = rest.iter(); + let location = match scheme { + 0 | 1 => { + let scheme = match scheme { + 0 => Scheme::HTTP, + _ => Scheme::HTTPS, + }; + let _headers = match rest.next() { + Some(Null) => (), + _ => Err(DecodeError::WrongFormatError( + "import/remote/headers".to_owned(), + ))?, + }; + let authority = match rest.next() { + Some(String(s)) => s.to_owned(), + _ => Err(DecodeError::WrongFormatError( + "import/remote/authority".to_owned(), + ))?, + }; + let query = match rest.next_back() { + Some(Null) => None, + Some(String(s)) => Some(s.to_owned()), + _ => Err(DecodeError::WrongFormatError( + "import/remote/query".to_owned(), + ))?, + }; + let path = rest + .map(|s| { + s.as_string().ok_or( + DecodeError::WrongFormatError( + "import/remote/path".to_owned(), + ), + ) + }) + .collect::>()?; + ImportLocation::Remote(URL { + scheme, + authority, + path, + query, + }) } - } + 2 | 3 | 4 | 5 => { + let prefix = match scheme { + 2 => FilePrefix::Absolute, + 3 => FilePrefix::Here, + 4 => FilePrefix::Parent, + 5 => FilePrefix::Home, + _ => Err(DecodeError::WrongFormatError( + "import/local/prefix".to_owned(), + ))?, + }; + let path = rest + .map(|s| { + s.as_string().ok_or( + DecodeError::WrongFormatError( + "import/local/path".to_owned(), + ), + ) + }) + .collect::>()?; + ImportLocation::Local(prefix, path) + } + 6 => { + let env = match rest.next() { + Some(String(s)) => s.to_owned(), + _ => Err(DecodeError::WrongFormatError( + "import/env".to_owned(), + ))?, + }; + ImportLocation::Env(env) + } + 7 => ImportLocation::Missing, + _ => Err(DecodeError::WrongFormatError( + "import/type".to_owned(), + ))?, + }; Embed(Import { - mode: ImportMode::Code, + mode, hash: None, - location: ImportLocation::Local(FilePrefix::Here, path), + location, }) } [U64(25), bindings..] => { diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 9f60ee7..8a2edce 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -38,6 +38,7 @@ fn resolve_import( }; load_dhall_file(&path, true) } + _ => unimplemented!("{:?}", import), } } diff --git a/dhall/tests/parser.rs b/dhall/tests/parser.rs index 79a059d..637341d 100644 --- a/dhall/tests/parser.rs +++ b/dhall/tests/parser.rs @@ -14,13 +14,13 @@ macro_rules! parser_failure { } parser_success!(spec_parser_success_annotations, "annotations"); -// parser_success!(spec_parser_success_asText, "asText"); +parser_success!(spec_parser_success_asText, "asText"); parser_success!(spec_parser_success_blockComment, "blockComment"); parser_success!(spec_parser_success_builtins, "builtins"); parser_success!(spec_parser_success_collectionImportType, "collectionImportType"); parser_success!(spec_parser_success_double, "double"); parser_success!(spec_parser_success_doubleQuotedString, "doubleQuotedString"); -// parser_success!(spec_parser_success_environmentVariables, "environmentVariables"); +parser_success!(spec_parser_success_environmentVariables, "environmentVariables"); // parser_success!(spec_parser_success_escapedDoubleQuotedString, "escapedDoubleQuotedString"); parser_success!(spec_parser_success_escapedSingleQuotedString, "escapedSingleQuotedString"); parser_success!(spec_parser_success_fields, "fields"); @@ -28,7 +28,7 @@ parser_success!(spec_parser_success_forall, "forall"); parser_success!(spec_parser_success_functionType, "functionType"); parser_success!(spec_parser_success_identifier, "identifier"); parser_success!(spec_parser_success_ifThenElse, "ifThenElse"); -// parser_success!(spec_parser_success_importAlt, "importAlt"); +parser_success!(spec_parser_success_importAlt, "importAlt"); parser_success!(spec_parser_success_interpolatedDoubleQuotedString, "interpolatedDoubleQuotedString"); parser_success!(spec_parser_success_interpolatedSingleQuotedString, "interpolatedSingleQuotedString"); parser_success!(spec_parser_success_label, "label"); @@ -43,10 +43,10 @@ parser_success!(spec_parser_success_natural, "natural"); parser_success!(spec_parser_success_nestedBlockComment, "nestedBlockComment"); parser_success!(spec_parser_success_operators, "operators"); // parser_success!(spec_parser_success_parenthesizeUsing, "parenthesizeUsing"); -// parser_success!(spec_parser_success_pathTermination, "pathTermination"); -// parser_success!(spec_parser_success_paths, "paths"); +parser_success!(spec_parser_success_pathTermination, "pathTermination"); +parser_success!(spec_parser_success_paths, "paths"); // parser_success!(spec_parser_success_quotedLabel, "quotedLabel"); -// parser_success!(spec_parser_success_quotedPaths, "quotedPaths"); +parser_success!(spec_parser_success_quotedPaths, "quotedPaths"); parser_success!(spec_parser_success_record, "record"); parser_success!(spec_parser_success_reservedPrefix, "reservedPrefix"); parser_success!(spec_parser_success_singleQuotedString, "singleQuotedString"); @@ -54,7 +54,7 @@ parser_success!(spec_parser_success_sort, "sort"); parser_success!(spec_parser_success_template, "template"); parser_success!(spec_parser_success_unicodeComment, "unicodeComment"); parser_success!(spec_parser_success_unicodeDoubleQuotedString, "unicodeDoubleQuotedString"); -// parser_success!(spec_parser_success_unicodePaths, "unicodePaths"); +parser_success!(spec_parser_success_unicodePaths, "unicodePaths"); parser_success!(spec_parser_success_union, "union"); // parser_success!(spec_parser_success_urls, "urls"); parser_success!(spec_parser_success_whitespace, "whitespace"); -- cgit v1.2.3