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_syntax/src/core/import.rs | 75 ++--------------------------------------- dhall_syntax/src/parser.rs | 6 ++-- 2 files changed, 6 insertions(+), 75 deletions(-) (limited to 'dhall_syntax/src') diff --git a/dhall_syntax/src/core/import.rs b/dhall_syntax/src/core/import.rs index 5e0ff6c..cc38bb0 100644 --- a/dhall_syntax/src/core/import.rs +++ b/dhall_syntax/src/core/import.rs @@ -12,14 +12,14 @@ pub enum FilePrefix { } #[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct File { +pub struct FilePath { pub file_path: Vec, } /// The location of import (i.e. local vs. remote vs. environment) #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum ImportLocation { - Local(FilePrefix, File), + Local(FilePrefix, FilePath), Remote(URL), Env(String), Missing, @@ -29,7 +29,7 @@ pub enum ImportLocation { pub struct URL { pub scheme: Scheme, pub authority: String, - pub path: File, + pub path: FilePath, pub query: Option, pub headers: Option, } @@ -104,72 +104,3 @@ impl Import { }) } } - -pub trait Canonicalize { - fn canonicalize(&self) -> Self; -} - -impl Canonicalize for File { - fn canonicalize(&self) -> File { - 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 { - // ─────────────────── - // 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()), - } - } - - File { 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, - } - } -} diff --git a/dhall_syntax/src/parser.rs b/dhall_syntax/src/parser.rs index b70a236..52b3760 100644 --- a/dhall_syntax/src/parser.rs +++ b/dhall_syntax/src/parser.rs @@ -729,7 +729,7 @@ make_parser! { [scheme(sch), authority(auth), path(file_path)] => URL { scheme: sch, authority: auth, - path: File { file_path }, + path: FilePath { file_path }, query: None, headers: None, }, @@ -737,7 +737,7 @@ make_parser! { URL { scheme: sch, authority: auth, - path: File { file_path }, + path: FilePath { file_path }, query: Some(q), headers: None, } @@ -794,7 +794,7 @@ make_parser! { ImportLocation::Remote(url) }, [local((prefix, file_path))] => { - ImportLocation::Local(prefix, File { file_path }) + ImportLocation::Local(prefix, FilePath { file_path }) }, )); -- cgit v1.2.3