diff options
author | Matthias Beyer <mail@beyermatthias.de> | 2021-11-23 09:36:50 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2021-11-23 09:36:50 +0100 |
commit | c3a1e8b6bac2c644e4c61f6ea29fa6e80eec7c3e (patch) | |
tree | 76b831c779c8d6d60f854d0ec3f1ada74935f3e7 /src | |
parent | 1819dca65ae02afee9e495e961283a0b9ee62f0f (diff) | |
parent | cdc212c5983b547826c59a6b0faf5cc6116b0db3 (diff) |
Merge pull request #250 from conradludgate/pathdiff
chore: use pathdiff crate
Diffstat (limited to 'src')
-rw-r--r-- | src/file/source/file.rs | 47 |
1 files changed, 2 insertions, 45 deletions
diff --git a/src/file/source/file.rs b/src/file/source/file.rs index 5a50d93..9bd3ddd 100644 --- a/src/file/source/file.rs +++ b/src/file/source/file.rs @@ -2,8 +2,7 @@ use std::env; use std::error::Error; use std::fs; use std::io; -use std::iter::Iterator; -use std::path::{Path, PathBuf}; +use std::path::PathBuf; use crate::file::{ format::ALL_EXTENSIONS, source::FileSourceResult, FileSource, FileStoredFormat, Format, @@ -110,7 +109,7 @@ where // Attempt to use a relative path for the URI let uri = env::current_dir() .ok() - .and_then(|base| path_relative_from(&filename, &base)) + .and_then(|base| pathdiff::diff_paths(&filename, base)) .unwrap_or_else(|| filename.clone()); // Read contents from file @@ -123,45 +122,3 @@ where }) } } - -// TODO: This should probably be a crate -// https://github.com/rust-lang/rust/blob/master/src/librustc_trans/back/rpath.rs#L128 -fn path_relative_from(path: &Path, base: &Path) -> Option<PathBuf> { - use std::path::Component; - - if path.is_absolute() != base.is_absolute() { - if path.is_absolute() { - Some(PathBuf::from(path)) - } else { - None - } - } else { - let mut ita = path.components(); - let mut itb = base.components(); - let mut comps: Vec<Component> = vec![]; - loop { - match (ita.next(), itb.next()) { - (None, None) => break, - (Some(a), None) => { - comps.push(a); - comps.extend(ita.by_ref()); - break; - } - (None, _) => comps.push(Component::ParentDir), - (Some(a), Some(b)) if comps.is_empty() && a == b => (), - (Some(a), Some(b)) if b == Component::CurDir => comps.push(a), - (Some(_), Some(b)) if b == Component::ParentDir => return None, - (Some(a), Some(_)) => { - comps.push(Component::ParentDir); - for _ in itb { - comps.push(Component::ParentDir); - } - comps.push(a); - comps.extend(ita.by_ref()); - break; - } - } - } - Some(comps.iter().map(|c| c.as_os_str()).collect()) - } -} |