engine-q merge

This commit is contained in:
Fernando Herrera
2022-02-07 19:11:34 +00:00
1965 changed files with 119062 additions and 20 deletions

View File

@ -1,12 +1,22 @@
[package]
authors = ["The Nu Project Contributors"]
description = "Path handling library for Nushell"
<<<<<<< HEAD
edition = "2018"
license = "MIT"
name = "nu-path"
version = "0.43.0"
=======
edition = "2021"
license = "MIT"
name = "nu-path"
version = "0.37.1"
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
[dependencies]
dirs-next = "2.0.0"
dunce = "1.0.1"
<<<<<<< HEAD
=======
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce

View File

@ -1,3 +0,0 @@
# nu-path
This crate takes care of path handling in Nushell, such as canonicalization and component expansion, as well as other path-related utilities.

View File

@ -19,7 +19,11 @@ fn handle_dots_push(string: &mut String, count: u8) {
string.pop(); // remove last '/'
}
<<<<<<< HEAD
/// Expands any occurrence of more than two dots into a sequence of ../ (or ..\ on windows), e.g.,
=======
/// Expands any occurence of more than two dots into a sequence of ../ (or ..\ on windows), e.g.,
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
/// "..." into "../..", "...." into "../../../", etc.
pub fn expand_ndots(path: impl AsRef<Path>) -> PathBuf {
// Check if path is valid UTF-8 and if not, return it as it is to avoid breaking it via string

View File

@ -26,19 +26,32 @@ where
}
}
<<<<<<< HEAD
/// Resolve all symbolic links and all components (tilde, ., .., ...+) and return the path in its
/// absolute form.
///
/// Fails under the same conditions as
/// [std::fs::canonicalize](https://doc.rust-lang.org/std/fs/fn.canonicalize.html).
pub fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
=======
fn canonicalize(path: impl AsRef<Path>) -> io::Result<PathBuf> {
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
let path = expand_tilde(path);
let path = expand_ndots(path);
dunce::canonicalize(path)
}
<<<<<<< HEAD
/// Same as canonicalize() but the input path is specified relative to another path
=======
/// Resolve all symbolic links and all components (tilde, ., .., ...+) and return the path in its
/// absolute form.
///
/// Fails under the same conditions as
/// [std::fs::canonicalize](https://doc.rust-lang.org/std/fs/fn.canonicalize.html).
/// The input path is specified relative to another path
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
pub fn canonicalize_with<P, Q>(path: P, relative_to: Q) -> io::Result<PathBuf>
where
P: AsRef<Path>,
@ -49,6 +62,15 @@ where
canonicalize(path)
}
<<<<<<< HEAD
=======
fn expand_path(path: impl AsRef<Path>) -> PathBuf {
let path = expand_tilde(path);
let path = expand_ndots(path);
expand_dots(path)
}
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
/// Resolve only path components (tilde, ., .., ...+), if possible.
///
/// The function works in a "best effort" mode: It does not fail but rather returns the unexpanded
@ -57,6 +79,7 @@ where
/// Furthermore, unlike canonicalize(), it does not use sys calls (such as readlink).
///
/// Does not convert to absolute form nor does it resolve symlinks.
<<<<<<< HEAD
pub fn expand_path(path: impl AsRef<Path>) -> PathBuf {
let path = expand_tilde(path);
let path = expand_ndots(path);
@ -64,6 +87,9 @@ pub fn expand_path(path: impl AsRef<Path>) -> PathBuf {
}
/// Same as expand_path() but the input path is specified relative to another path
=======
/// The input path is specified relative to another path
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
pub fn expand_path_with<P, Q>(path: P, relative_to: Q) -> PathBuf
where
P: AsRef<Path>,

View File

@ -0,0 +1,9 @@
use std::path::PathBuf;
pub fn home_dir() -> Option<PathBuf> {
dirs_next::home_dir()
}
pub fn config_dir() -> Option<PathBuf> {
dirs_next::config_dir()
}

View File

@ -1,8 +1,17 @@
mod dots;
mod expansions;
<<<<<<< HEAD
mod tilde;
mod util;
pub use expansions::{canonicalize, canonicalize_with, expand_path, expand_path_with};
=======
mod helpers;
mod tilde;
mod util;
pub use expansions::{canonicalize_with, expand_path_with};
pub use helpers::{config_dir, home_dir};
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
pub use tilde::expand_tilde;
pub use util::trim_trailing_slash;

View File

@ -1,6 +1,10 @@
use std::path::{Path, PathBuf};
<<<<<<< HEAD
fn expand_tilde_with(path: impl AsRef<Path>, home: Option<PathBuf>) -> PathBuf {
=======
fn expand_tilde_with_home(path: impl AsRef<Path>, home: Option<PathBuf>) -> PathBuf {
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
let path = path.as_ref();
if !path.starts_with("~") {
@ -27,7 +31,11 @@ fn expand_tilde_with(path: impl AsRef<Path>, home: Option<PathBuf>) -> PathBuf {
/// Expand tilde ("~") into a home directory if it is the first path component
pub fn expand_tilde(path: impl AsRef<Path>) -> PathBuf {
// TODO: Extend this to work with "~user" style of home paths
<<<<<<< HEAD
expand_tilde_with(path, dirs_next::home_dir())
=======
expand_tilde_with_home(path, dirs_next::home_dir())
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
}
#[cfg(test)]
@ -37,17 +45,29 @@ mod tests {
fn check_expanded(s: &str) {
let home = Path::new("/home");
let buf = Some(PathBuf::from(home));
<<<<<<< HEAD
assert!(expand_tilde_with(Path::new(s), buf).starts_with(&home));
=======
assert!(expand_tilde_with_home(Path::new(s), buf).starts_with(&home));
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
// Tests the special case in expand_tilde for "/" as home
let home = Path::new("/");
let buf = Some(PathBuf::from(home));
<<<<<<< HEAD
assert!(!expand_tilde_with(Path::new(s), buf).starts_with("//"));
=======
assert!(!expand_tilde_with_home(Path::new(s), buf).starts_with("//"));
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
}
fn check_not_expanded(s: &str) {
let home = PathBuf::from("/home");
<<<<<<< HEAD
let expanded = expand_tilde_with(Path::new(s), Some(home));
=======
let expanded = expand_tilde_with_home(Path::new(s), Some(home));
>>>>>>> 9259a56a28f1dd3a4b720ad815aa19c6eaf6adce
assert!(expanded == Path::new(s));
}

View File

@ -1,4 +0,0 @@
/// Trim trailing path separator from a string
pub fn trim_trailing_slash(s: &str) -> &str {
s.trim_end_matches(std::path::is_separator)
}

View File

@ -1 +0,0 @@
mod util;

View File

@ -1,45 +0,0 @@
use nu_path::trim_trailing_slash;
use std::path::MAIN_SEPARATOR;
/// Helper function that joins string literals with '/' or '\', based on the host OS
fn join_path_sep(pieces: &[&str]) -> String {
let sep_string = String::from(MAIN_SEPARATOR);
pieces.join(&sep_string)
}
#[test]
fn trims_trailing_slash_without_trailing_slash() {
let path = join_path_sep(&["some", "path"]);
let actual = trim_trailing_slash(&path);
assert_eq!(actual, &path)
}
#[test]
fn trims_trailing_slash() {
let path = join_path_sep(&["some", "path", ""]);
let actual = trim_trailing_slash(&path);
let expected = join_path_sep(&["some", "path"]);
assert_eq!(actual, &expected)
}
#[test]
fn trims_many_trailing_slashes() {
let path = join_path_sep(&["some", "path", "", "", "", ""]);
let actual = trim_trailing_slash(&path);
let expected = join_path_sep(&["some", "path"]);
assert_eq!(actual, &expected)
}
#[test]
fn trims_trailing_slash_empty() {
let path = String::from(MAIN_SEPARATOR);
let actual = trim_trailing_slash(&path);
assert_eq!(actual, "")
}