From 405bc3d80c0e169ea74dd12422b9504b7383dab3 Mon Sep 17 00:00:00 2001 From: FintanH Date: Mon, 12 Aug 2019 22:22:24 +0100 Subject: Refactor of File to be the combination of Directory and the file name, where Directory is the Vector of component paths. The refactor meant changing some sections of the code where we were parsing and manipulating Files/Directories. This also includes a new trait Canonicalization which is needed for import logic. --- dhall/src/phase/binary.rs | 29 ++++++++++++++++++++--------- 1 file changed, 20 insertions(+), 9 deletions(-) (limited to 'dhall/src/phase/binary.rs') diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index 1812131..3acd2d4 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -1,12 +1,13 @@ use itertools::Itertools; use serde_cbor::value::value as cbor; use std::iter::FromIterator; +use std::vec; use dhall_syntax::map::DupTreeMap; use dhall_syntax::{ rc, ExprF, FilePrefix, Hash, Import, ImportHashed, ImportLocation, ImportMode, Integer, InterpolatedText, Label, Natural, Scheme, SubExpr, - URL, V, + URL, V, Directory, File, }; use crate::error::{DecodeError, EncodeError}; @@ -243,7 +244,7 @@ fn cbor_value_to_dhall( "import/remote/query".to_owned(), ))?, }; - let path = rest + let mut components: Vec<_> = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -251,6 +252,11 @@ fn cbor_value_to_dhall( )), }) .collect::>()?; + let file = components.pop().ok_or( + DecodeError::WrongFormatError( + "import/remote/path".to_owned(), + ))?; + let path = File { directory: Directory { components: components }, file: file }; ImportLocation::Remote(URL { scheme, authority, @@ -269,7 +275,7 @@ fn cbor_value_to_dhall( "import/local/prefix".to_owned(), ))?, }; - let path = rest + let mut components: Vec<_> = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -277,6 +283,11 @@ fn cbor_value_to_dhall( )), }) .collect::>()?; + let file = components.pop().ok_or( + DecodeError::WrongFormatError( + "import/remote/path".to_owned(), + ))?; + let path = File { directory: Directory { components: components }, file: file }; ImportLocation::Local(prefix, path) } 6 => { @@ -520,8 +531,8 @@ where use serde::ser::SerializeSeq; let count = 4 + match &import.location_hashed.location { - ImportLocation::Remote(url) => 3 + url.path.len(), - ImportLocation::Local(_, path) => path.len(), + ImportLocation::Remote(url) => 3 + url.path.clone().into_iter().len(), + ImportLocation::Local(_, path) => path.clone().into_iter().len(), ImportLocation::Env(_) => 1, ImportLocation::Missing => 0, }; @@ -573,8 +584,8 @@ where )?, }; ser_seq.serialize_element(&url.authority)?; - for p in &url.path { - ser_seq.serialize_element(p)?; + for p in url.path.clone().into_iter() { + ser_seq.serialize_element(&p)?; } match &url.query { None => ser_seq.serialize_element(&Null)?, @@ -582,8 +593,8 @@ where }; } ImportLocation::Local(_, path) => { - for p in path { - ser_seq.serialize_element(p)?; + for p in path.clone().into_iter() { + ser_seq.serialize_element(&p)?; } } ImportLocation::Env(env) => { -- cgit v1.2.3 From e73f822b6972e8fa2e72b56ff5378b91bea1a5e6 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Mon, 2 Sep 2019 21:17:00 +0100 Subject: Remove the notion of Directory and have File be the vector of components --- dhall/src/phase/binary.rs | 18 +++++------------- 1 file changed, 5 insertions(+), 13 deletions(-) (limited to 'dhall/src/phase/binary.rs') 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::>()?; - 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::>()?; - 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 => { -- cgit v1.2.3 From 4edaf0814868e604eed5cfd594ea3f448ca90678 Mon Sep 17 00:00:00 2001 From: Fintan Halpenny Date: Tue, 3 Sep 2019 14:42:30 +0100 Subject: Move Canonicalize into resolve. Rename File to FilePath and have more consistent naming. --- dhall/src/phase/binary.rs | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) (limited to 'dhall/src/phase/binary.rs') diff --git a/dhall/src/phase/binary.rs b/dhall/src/phase/binary.rs index 16e7ce9..3c45e81 100644 --- a/dhall/src/phase/binary.rs +++ b/dhall/src/phase/binary.rs @@ -6,7 +6,7 @@ use std::vec; use dhall_syntax::map::DupTreeMap; use dhall_syntax::{ rc, Expr, ExprF, FilePrefix, Hash, Import, ImportLocation, ImportMode, - Integer, InterpolatedText, Label, Natural, Scheme, URL, V, File + Integer, InterpolatedText, Label, Natural, Scheme, URL, V, FilePath }; use crate::error::{DecodeError, EncodeError}; @@ -279,7 +279,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { "import/remote/query".to_owned(), ))?, }; - let file_path: Vec<_> = rest + let file_path = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -287,7 +287,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { )), }) .collect::>()?; - let path = File { file_path }; + let path = FilePath { file_path }; ImportLocation::Remote(URL { scheme, authority, @@ -306,7 +306,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { "import/local/prefix".to_owned(), ))?, }; - let file_path: Vec<_> = rest + let file_path = rest .map(|s| match s.as_string() { Some(s) => Ok(s.clone()), None => Err(DecodeError::WrongFormatError( @@ -314,7 +314,7 @@ fn cbor_value_to_dhall(data: &cbor::Value) -> Result { )), }) .collect::>()?; - let path = File { file_path }; + let path = FilePath { file_path }; ImportLocation::Local(prefix, path) } 6 => { -- cgit v1.2.3