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/resolve.rs | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) (limited to 'dhall/src/phase/resolve.rs') diff --git a/dhall/src/phase/resolve.rs b/dhall/src/phase/resolve.rs index c4d7e5f..ba75be4 100644 --- a/dhall/src/phase/resolve.rs +++ b/dhall/src/phase/resolve.rs @@ -16,6 +16,7 @@ type ImportCache = HashMap; pub type ImportStack = Vec; + fn resolve_import( import: &Import, root: &ImportRoot, @@ -30,14 +31,14 @@ fn resolve_import( }; match &import.location_hashed.location { Local(prefix, path) => { - let path: PathBuf = path.iter().cloned().collect(); - let path = match prefix { + let path_buf: PathBuf = path.clone().into_iter().collect(); + let path_buf = match prefix { // TODO: fail gracefully - Parent => cwd.parent().unwrap().join(path), - Here => cwd.join(path), + Parent => cwd.parent().unwrap().join(path_buf), + Here => cwd.join(path_buf), _ => unimplemented!("{:?}", import), }; - Ok(load_import(&path, import_cache, import_stack).map_err(|e| { + Ok(load_import(&path_buf, import_cache, import_stack).map_err(|e| { ImportError::Recursive(import.clone(), Box::new(e)) })?) } -- 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/resolve.rs | 70 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 70 insertions(+) (limited to 'dhall/src/phase/resolve.rs') diff --git a/dhall/src/phase/resolve.rs b/dhall/src/phase/resolve.rs index 5bde68a..54a0f27 100644 --- a/dhall/src/phase/resolve.rs +++ b/dhall/src/phase/resolve.rs @@ -3,6 +3,7 @@ use std::path::{Path, PathBuf}; use crate::error::{Error, ImportError}; use crate::phase::{Normalized, NormalizedExpr, Parsed, Resolved}; +use dhall_syntax::{FilePath, ImportLocation, URL}; type Import = dhall_syntax::Import; @@ -105,6 +106,75 @@ pub(crate) fn skip_resolve_expr( Ok(Resolved(expr)) } +pub trait Canonicalize { + fn canonicalize(&self) -> Self; +} + +impl Canonicalize for FilePath { + fn canonicalize(&self) -> FilePath { + let mut file_path = Vec::new(); + let mut file_path_components = self.file_path.clone().into_iter(); + + loop { + let component = file_path_components.next(); + match component.as_ref() { + // ─────────────────── + // canonicalize(ε) = ε + None => break, + + // canonicalize(directory₀) = directory₁ + // ─────────────────────────────────────── + // canonicalize(directory₀/.) = directory₁ + Some(c) if c == "." => continue, + + Some(c) if c == ".." => match file_path_components.next() { + // canonicalize(directory₀) = ε + // ──────────────────────────── + // canonicalize(directory₀/..) = /.. + None => file_path.push("..".to_string()), + + // canonicalize(directory₀) = directory₁/.. + // ────────────────────────────────────────────── + // canonicalize(directory₀/..) = directory₁/../.. + Some(ref c) if c == ".." => { + file_path.push("..".to_string()); + file_path.push("..".to_string()); + }, + + // canonicalize(directory₀) = directory₁/component + // ─────────────────────────────────────────────── ; If "component" is not + // canonicalize(directory₀/..) = directory₁ ; ".." + Some(_) => continue, + }, + + // canonicalize(directory₀) = directory₁ + // ───────────────────────────────────────────────────────── ; If no other + // canonicalize(directory₀/component) = directory₁/component ; rule matches + Some(c) => file_path.push(c.clone()), + } + } + + FilePath { file_path } + } +} + +impl Canonicalize for ImportLocation { + fn canonicalize(&self) -> ImportLocation { + match self { + ImportLocation::Local(prefix, file) => ImportLocation::Local(*prefix, file.canonicalize()), + ImportLocation::Remote(url) => ImportLocation::Remote(URL { + scheme: url.scheme, + authority: url.authority.clone(), + path: url.path.canonicalize(), + query: url.query.clone(), + headers: url.headers.clone(), + }), + ImportLocation::Env(name) => ImportLocation::Env(name.to_string()), + ImportLocation::Missing => ImportLocation::Missing, + } + } +} + #[cfg(test)] #[rustfmt::skip] mod spec_tests { -- cgit v1.2.3