diff options
-rw-r--r-- | dhall/src/phase/binary.rs | 18 | ||||
-rw-r--r-- | dhall_syntax/src/core/import.rs | 49 | ||||
-rw-r--r-- | dhall_syntax/src/parser.rs | 23 |
3 files changed, 24 insertions, 66 deletions
diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index 6bdc3f9..89b6db2 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -7,7 +7,7 @@ use dhall_syntax::map::DupTreeMap; use dhall_syntax::{ rc, ExprF, FilePrefix, Hash, Import, ImportHashed, ImportLocation, ImportMode, Integer, InterpolatedText, Label, Natural, Scheme, SubExpr, - URL, V, Directory, File, + URL, V, File, }; use crate::error::{DecodeError, EncodeError}; @@ -291,7 +291,7 @@ fn cbor_value_to_dhall( "import/remote/query".to_owned(), ))?, }; - let mut components: Vec<_> = rest + let file_path: Vec<_> = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -299,11 +299,7 @@ fn cbor_value_to_dhall( )), }) .collect::<Result<_, _>>()?; - let file = components.pop().ok_or( - DecodeError::WrongFormatError( - "import/remote/path".to_owned(), - ))?; - let path = File { directory: Directory { components: components }, file: file }; + let path = File { file_path }; ImportLocation::Remote(URL { scheme, authority, @@ -322,7 +318,7 @@ fn cbor_value_to_dhall( "import/local/prefix".to_owned(), ))?, }; - let mut components: Vec<_> = rest + let file_path: Vec<_> = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -330,11 +326,7 @@ fn cbor_value_to_dhall( )), }) .collect::<Result<_, _>>()?; - let file = components.pop().ok_or( - DecodeError::WrongFormatError( - "import/remote/path".to_owned(), - ))?; - let path = File { directory: Directory { components: components }, file: file }; + let path = File { file_path }; ImportLocation::Local(prefix, path) } 6 => { diff --git a/dhall_syntax/src/core/import.rs b/dhall_syntax/src/core/import.rs index 82bb7ff..4aad70d 100644 --- a/dhall_syntax/src/core/import.rs +++ b/dhall_syntax/src/core/import.rs @@ -12,23 +12,8 @@ pub enum FilePrefix { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Directory { - pub components: Vec<String>, -} - -impl IntoIterator for Directory { - type Item = String; - type IntoIter = ::std::vec::IntoIter<Self::Item>; - - fn into_iter(self) -> Self::IntoIter { - self.components.into_iter() - } -} - -#[derive(Debug, Clone, PartialEq, Eq, Hash)] pub struct File { - pub directory: Directory, - pub file: String, + pub file_path: Vec<String>, } impl IntoIterator for File { @@ -36,9 +21,7 @@ impl IntoIterator for File { type IntoIter = ::std::vec::IntoIter<Self::Item>; fn into_iter(self) -> Self::IntoIter { - let mut paths = self.directory.components; - paths.push(self.file); - paths.into_iter() + self.file_path.into_iter() } } @@ -97,13 +80,13 @@ pub trait Canonicalize { fn canonicalize(&self) -> Self; } -impl Canonicalize for Directory { - fn canonicalize(&self) -> Directory { - let mut components = Vec::new(); - let mut dir_components = self.clone().into_iter(); +impl Canonicalize for File { + fn canonicalize(&self) -> File { + let mut file_path = Vec::new(); + let mut file_path_components = self.clone().into_iter(); loop { - let component = dir_components.next(); + let component = file_path_components.next(); match component.as_ref() { // ─────────────────── // canonicalize(ε) = ε @@ -114,18 +97,18 @@ impl Canonicalize for Directory { // canonicalize(directory₀/.) = directory₁ Some(c) if c == "." => continue, - Some(c) if c == ".." => match dir_components.next() { + Some(c) if c == ".." => match file_path_components.next() { // canonicalize(directory₀) = ε // ──────────────────────────── // canonicalize(directory₀/..) = /.. - None => components.push("..".to_string()), + None => file_path.push("..".to_string()), // canonicalize(directory₀) = directory₁/.. // ────────────────────────────────────────────── // canonicalize(directory₀/..) = directory₁/../.. Some(ref c) if c == ".." => { - components.push("..".to_string()); - components.push("..".to_string()); + file_path.push("..".to_string()); + file_path.push("..".to_string()); }, // canonicalize(directory₀) = directory₁/component @@ -137,17 +120,11 @@ impl Canonicalize for Directory { // canonicalize(directory₀) = directory₁ // ───────────────────────────────────────────────────────── ; If no other // canonicalize(directory₀/component) = directory₁/component ; rule matches - Some(c) => components.push(c.clone()), + Some(c) => file_path.push(c.clone()), } } - Directory { components: components } - } -} - -impl Canonicalize for File { - fn canonicalize(&self) -> File { - File { directory: self.directory.canonicalize(), file: self.file.clone() } + File { file_path } } } diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index a2495ee..71d0936 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -147,14 +147,6 @@ fn debug_pair(pair: Pair<Rule>) -> String { s } -fn to_file(path: Vec<String>) -> Result<File, String> { - let mut path = path; - let file_name: Option<String> = path.pop(); - let file = file_name.ok_or("Empty file path was provided")?; - let directory = Directory { components: path }; - Ok(File { directory: directory, file: file }) -} - macro_rules! make_parser { (@pattern, rule, $name:ident) => (Rule::$name); (@pattern, token_rule, $name:ident) => (Rule::$name); @@ -638,22 +630,20 @@ make_parser! { }); rule!(http_raw<URL>; children!( - [scheme(sch), authority(auth), path(p)] => { - let file = to_file(p)?; + [scheme(sch), authority(auth), path(file_path)] => { URL { scheme: sch, authority: auth, - path: file, + path: File { file_path }, query: None, headers: None, } }, - [scheme(sch), authority(auth), path(p), query(q)] => { - let file = to_file(p)?; + [scheme(sch), authority(auth), path(file_path), query(q)] => { URL { scheme: sch, authority: auth, - path: file, + path: File { file_path }, query: Some(q), headers: None, } @@ -709,9 +699,8 @@ make_parser! { [http(url)] => { ImportLocation::Remote(url) }, - [local((prefix, p))] => { - let file = to_file(p)?; - ImportLocation::Local(prefix, file) + [local((prefix, file_path))] => { + ImportLocation::Local(prefix, File { file_path }) }, )); |