make path input more permissive

This commit is contained in:
Connor Skees 2022-12-26 19:06:20 -05:00
parent 7e718e3492
commit 31bbe8f60f
3 changed files with 24 additions and 14 deletions

View File

@ -107,14 +107,13 @@ fn raw_to_parse_error(map: &CodeMap, err: Error, unicode: bool) -> Box<Error> {
Box::new(Error::from_loc(message, map.look_up_span(span), unicode)) Box::new(Error::from_loc(message, map.look_up_span(span), unicode))
} }
fn from_string_with_file_name(input: String, file_name: &str, options: &Options) -> Result<String> { fn from_string_with_file_name<P: AsRef<Path>>(input: String, file_name: P, options: &Options) -> Result<String> {
let mut map = CodeMap::new(); let mut map = CodeMap::new();
let file = map.add_file(file_name.to_owned(), input); let path = file_name.as_ref();
let file = map.add_file(path.to_string_lossy().into_owned(), input);
let empty_span = file.span.subspan(0, 0); let empty_span = file.span.subspan(0, 0);
let lexer = Lexer::new_from_file(&file); let lexer = Lexer::new_from_file(&file);
let path = Path::new(file_name);
let input_syntax = options let input_syntax = options
.input_syntax .input_syntax
.unwrap_or_else(|| InputSyntax::for_path(path)); .unwrap_or_else(|| InputSyntax::for_path(path));
@ -178,9 +177,9 @@ fn from_string_with_file_name(input: String, file_name: &str, options: &Options)
/// ``` /// ```
#[inline] #[inline]
pub fn from_path(p: &str, options: &Options) -> Result<String> { pub fn from_path<P: AsRef<Path>>(p: P, options: &Options) -> Result<String> {
from_string_with_file_name( from_string_with_file_name(
String::from_utf8(options.fs.read(Path::new(p))?)?, String::from_utf8(options.fs.read(p.as_ref())?)?,
p, p,
options, options,
) )

View File

@ -1,4 +1,4 @@
use std::path::Path; use std::path::{Path, PathBuf};
use crate::{Fs, StdFs}; use crate::{Fs, StdFs};
@ -11,7 +11,7 @@ use crate::{Fs, StdFs};
pub struct Options<'a> { pub struct Options<'a> {
pub(crate) fs: &'a dyn Fs, pub(crate) fs: &'a dyn Fs,
pub(crate) style: OutputStyle, pub(crate) style: OutputStyle,
pub(crate) load_paths: Vec<&'a Path>, pub(crate) load_paths: Vec<PathBuf>,
pub(crate) allows_charset: bool, pub(crate) allows_charset: bool,
pub(crate) unicode_error_messages: bool, pub(crate) unicode_error_messages: bool,
pub(crate) quiet: bool, pub(crate) quiet: bool,
@ -88,8 +88,8 @@ impl<'a> Options<'a> {
/// This method will append a single path to the list. /// This method will append a single path to the list.
#[must_use] #[must_use]
#[inline] #[inline]
pub fn load_path(mut self, path: &'a Path) -> Self { pub fn load_path<P: AsRef<Path>>(mut self, path: P) -> Self {
self.load_paths.push(path); self.load_paths.push(path.as_ref().to_owned());
self self
} }
@ -101,8 +101,11 @@ impl<'a> Options<'a> {
/// load paths /// load paths
#[must_use] #[must_use]
#[inline] #[inline]
pub fn load_paths(mut self, paths: &'a [&'a Path]) -> Self { pub fn load_paths<P: AsRef<Path>>(mut self, paths: &[P]) -> Self {
self.load_paths.extend_from_slice(paths); for path in paths {
self.load_paths.push(path.as_ref().to_owned());
}
self self
} }

View File

@ -31,5 +31,13 @@ test!(
"a {\n color: ! important;\n}\n", "a {\n color: ! important;\n}\n",
"a {\n color: !important;\n}\n" "a {\n color: !important;\n}\n"
); );
test!(
// todo: loud comment between !<>i, silent comment between !<>i loud_comment_after_exclamation,
"a {\n color: !/**/important;\n}\n",
"a {\n color: !important;\n}\n"
);
test!(
silent_comment_after_exclamation,
"a {\n color: !//\nimportant;\n}\n",
"a {\n color: !important;\n}\n"
);