Move uses of trim_quotes to unescape for filenames (#5398)

* Move uses of trim_quotes to unescape for filenames

* Fix Windows tests
This commit is contained in:
JT
2022-05-02 06:37:20 +12:00
committed by GitHub
parent 80d57d70cd
commit 98ab31e15e
4 changed files with 43 additions and 41 deletions

View File

@ -23,7 +23,7 @@ use crate::{
parse_internal_call, parse_multispan_value, parse_signature, parse_string,
parse_var_with_opt_type, trim_quotes,
},
ParseError,
unescape_unquote_string, ParseError,
};
pub fn parse_def_predecl(
@ -1312,9 +1312,10 @@ pub fn parse_use(
} else {
// TODO: Do not close over when loading module from file
// It could be a file
if let Ok(module_filename) =
String::from_utf8(trim_quotes(&import_pattern.head.name).to_vec())
{
let (module_filename, err) =
unescape_unquote_string(&import_pattern.head.name, import_pattern.head.span);
if err.is_none() {
if let Some(module_path) =
find_in_dirs(&module_filename, working_set, &cwd, LIB_DIRS_ENV)
{
@ -1816,8 +1817,8 @@ pub fn parse_source(
// Command and one file name
if spans.len() >= 2 {
let name_expr = working_set.get_span_contents(spans[1]);
let name_expr = trim_quotes(name_expr);
if let Ok(filename) = String::from_utf8(name_expr.to_vec()) {
let (filename, err) = unescape_unquote_string(name_expr, spans[1]);
if err.is_none() {
if let Some(path) = find_in_dirs(&filename, working_set, &cwd, LIB_DIRS_ENV) {
if let Ok(contents) = std::fs::read(&path) {
// This will load the defs from the file into the
@ -1967,26 +1968,27 @@ pub fn parse_register(
.map(|expr| {
let name_expr = working_set.get_span_contents(expr.span);
let name_expr = trim_quotes(name_expr);
String::from_utf8(name_expr.to_vec())
.map_err(|_| ParseError::NonUtf8(expr.span))
.and_then(|name| {
if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV) {
Ok(p)
} else {
Err(ParseError::RegisteredFileNotFound(name, expr.span))
}
})
.and_then(|path| {
if path.exists() & path.is_file() {
Ok(path)
} else {
Err(ParseError::RegisteredFileNotFound(
format!("{:?}", path),
expr.span,
))
}
})
let (name, err) = unescape_unquote_string(name_expr, expr.span);
if let Some(err) = err {
Err(err)
} else {
let path = if let Some(p) = find_in_dirs(&name, working_set, &cwd, PLUGIN_DIRS_ENV)
{
p
} else {
return Err(ParseError::RegisteredFileNotFound(name, expr.span));
};
if path.exists() & path.is_file() {
Ok(path)
} else {
Err(ParseError::RegisteredFileNotFound(
format!("{:?}", path),
expr.span,
))
}
}
})
.expect("required positional has being checked")
.and_then(|path| {

View File

@ -1928,10 +1928,10 @@ pub fn parse_directory(
span: Span,
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let bytes = trim_quotes(bytes);
let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: directory");
if let Ok(token) = String::from_utf8(bytes.into()) {
if err.is_none() {
trace!("-- found {}", token);
(
Expression {
@ -1955,10 +1955,10 @@ pub fn parse_filepath(
span: Span,
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let bytes = trim_quotes(bytes);
let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: filepath");
if let Ok(token) = String::from_utf8(bytes.into()) {
if err.is_none() {
trace!("-- found {}", token);
(
Expression {
@ -2267,12 +2267,11 @@ pub fn parse_glob_pattern(
working_set: &mut StateWorkingSet,
span: Span,
) -> (Expression, Option<ParseError>) {
let bytes = working_set.get_span_contents(span);
let (token, err) = unescape_unquote_string(bytes, span);
trace!("parsing: glob pattern");
let bytes = working_set.get_span_contents(span);
let bytes = trim_quotes(bytes);
if let Ok(token) = String::from_utf8(bytes.into()) {
if err.is_none() {
trace!("-- found {}", token);
(
Expression {