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_syntax/src/core/import.rs | 49 +++++++++++------------------------------ 1 file changed, 13 insertions(+), 36 deletions(-) (limited to 'dhall_syntax/src/core/import.rs') 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 @@ -11,24 +11,9 @@ pub enum FilePrefix { Home, } -#[derive(Debug, Clone, PartialEq, Eq, Hash)] -pub struct Directory { - pub components: Vec, -} - -impl IntoIterator for Directory { - type Item = String; - type IntoIter = ::std::vec::IntoIter; - - 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, } impl IntoIterator for File { @@ -36,9 +21,7 @@ impl IntoIterator for File { type IntoIter = ::std::vec::IntoIter; 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 } } } -- cgit v1.2.3