Use &Path as source path.

This commit is contained in:
Stephen Chung 2022-04-18 17:34:53 +08:00
parent daf73d5341
commit 3f74e5e674

View File

@ -201,17 +201,15 @@ impl FileModuleResolver {
/// Is a particular path cached? /// Is a particular path cached?
#[inline] #[inline]
#[must_use] #[must_use]
pub fn is_cached(&self, path: impl AsRef<str>, source_path: Option<&str>) -> bool { pub fn is_cached(&self, path: impl AsRef<Path>) -> bool {
if !self.cache_enabled { if !self.cache_enabled {
return false; return false;
} }
let file_path = self.get_file_path(path.as_ref(), source_path);
let cache = locked_read(&self.cache); let cache = locked_read(&self.cache);
if !cache.is_empty() { if !cache.is_empty() {
cache.contains_key(&file_path) cache.contains_key(path.as_ref())
} else { } else {
false false
} }
@ -227,20 +225,14 @@ impl FileModuleResolver {
/// The next time this path is resolved, the script file will be loaded once again. /// The next time this path is resolved, the script file will be loaded once again.
#[inline] #[inline]
#[must_use] #[must_use]
pub fn clear_cache_for_path( pub fn clear_cache_for_path(&mut self, path: impl AsRef<Path>) -> Option<Shared<Module>> {
&mut self,
path: impl AsRef<str>,
source_path: Option<impl AsRef<str>>,
) -> Option<Shared<Module>> {
let file_path = self.get_file_path(path.as_ref(), source_path.as_ref().map(<_>::as_ref));
locked_write(&self.cache) locked_write(&self.cache)
.remove_entry(&file_path) .remove_entry(path.as_ref())
.map(|(.., v)| v) .map(|(.., v)| v)
} }
/// Construct a full file path. /// Construct a full file path.
#[must_use] #[must_use]
fn get_file_path(&self, path: &str, source_path: Option<&str>) -> PathBuf { pub fn get_file_path(&self, path: &str, source_path: Option<&Path>) -> PathBuf {
let path = Path::new(path); let path = Path::new(path);
let mut file_path; let mut file_path;
@ -274,9 +266,9 @@ impl FileModuleResolver {
.as_ref() .as_ref()
.and_then(|g| g.source()) .and_then(|g| g.source())
.or(source) .or(source)
.and_then(|p| Path::new(p).parent().map(|p| p.to_string_lossy())); .and_then(|p| Path::new(p).parent());
let file_path = self.get_file_path(path, source_path.as_ref().map(|p| p.as_ref())); let file_path = self.get_file_path(path, source_path);
if self.is_cache_enabled() { if self.is_cache_enabled() {
#[cfg(not(feature = "sync"))] #[cfg(not(feature = "sync"))]
@ -351,7 +343,7 @@ impl ModuleResolver for FileModuleResolver {
pos: Position, pos: Position,
) -> Option<RhaiResultOf<crate::AST>> { ) -> Option<RhaiResultOf<crate::AST>> {
// Construct the script file path // Construct the script file path
let file_path = self.get_file_path(path, source_path); let file_path = self.get_file_path(path, source_path.map(|s| Path::new(s)));
// Load the script file and compile it // Load the script file and compile it
Some( Some(