From fa4077a24004405a374c1011a53dcd9a94b2886d Mon Sep 17 00:00:00 2001 From: Richard Carson Date: Fri, 3 May 2024 15:06:53 -0400 Subject: [PATCH] Make loader files async --- src/inner_runtime.rs | 2 +- src/lib.rs | 1 + src/module_loader.rs | 10 +++++----- src/traits.rs | 2 +- src/transpiler.rs | 2 +- src/utilities.rs | 2 +- 6 files changed, 10 insertions(+), 9 deletions(-) diff --git a/src/inner_runtime.rs b/src/inner_runtime.rs index 5574176..d932d1c 100644 --- a/src/inner_runtime.rs +++ b/src/inner_runtime.rs @@ -36,7 +36,7 @@ impl Default for InnerRuntimeOptions { extensions: Default::default(), default_entrypoint: Default::default(), timeout: Duration::MAX, - module_cache: Some(Box::new(DefaultModuleCacheProvider::default())), + module_cache: Some(Box::::default()), } } } diff --git a/src/lib.rs b/src/lib.rs index 1297c68..5a26cee 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -1,3 +1,4 @@ +#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)] //! This crate is meant to provide a quick and simple way to integrate a runtime javacript or typescript component from within rust. //! //! - **By default, the code being run is entirely sandboxed from the host, having no filesystem or network access.** diff --git a/src/module_loader.rs b/src/module_loader.rs index 586b34b..c20c676 100644 --- a/src/module_loader.rs +++ b/src/module_loader.rs @@ -46,7 +46,7 @@ impl ModuleCacheProvider for DefaultModuleCacheProvider { fn get(&self, specifier: &ModuleSpecifier) -> Option { let cache = self.0.borrow(); let source = cache.get(specifier)?; - Some(Self::clone_source(&self, specifier, source)) + Some(Self::clone_source(self, specifier, source)) } } @@ -62,7 +62,7 @@ impl ModuleLoader for RustyLoader { referrer: &str, _kind: deno_core::ResolutionKind, ) -> Result { - let url = deno_core::resolve_import(specifier, &referrer)?; + let url = deno_core::resolve_import(specifier, referrer)?; if referrer == "." { self.whitelist_add(url.as_str()); } @@ -165,7 +165,7 @@ impl RustyLoader { ) -> Result { let cache_provider = cache_provider.as_ref().as_ref().map(|p| p.as_ref()); match cache_provider.map(|p| p.get(&module_specifier)) { - Some(Some(source)) => return Ok(source), + Some(Some(source)) => Ok(source), _ => { let module_type = if module_specifier.path().ends_with(".json") { ModuleType::Json @@ -193,7 +193,7 @@ impl RustyLoader { ) -> Result { let cache_provider = cache_provider.as_ref().as_ref().map(|p| p.as_ref()); match cache_provider.map(|p| p.get(&module_specifier)) { - Some(Some(source)) => return Ok(source), + Some(Some(source)) => Ok(source), _ => { let module_type = if module_specifier.path().ends_with(".json") { ModuleType::Json @@ -204,7 +204,7 @@ impl RustyLoader { let path = module_specifier.to_file_path().map_err(|_| { anyhow!("Provided module specifier \"{module_specifier}\" is not a file URL.") })?; - let code = std::fs::read_to_string(path)?; + let code = tokio::fs::read_to_string(path).await?; let code = transpiler::transpile(&module_specifier, &code)?; Ok(ModuleSource::new( diff --git a/src/traits.rs b/src/traits.rs index 6e4ddd8..467251b 100644 --- a/src/traits.rs +++ b/src/traits.rs @@ -10,7 +10,7 @@ pub trait ToModuleSpecifier { impl ToModuleSpecifier for str { fn to_module_specifier(&self) -> Result { - resolve_path(self, ¤t_dir()?).map_err(|e| Error::from(e)) + resolve_path(self, ¤t_dir()?).map_err(Error::from) } } diff --git a/src/transpiler.rs b/src/transpiler.rs index 5ac9c16..3b2bb24 100644 --- a/src/transpiler.rs +++ b/src/transpiler.rs @@ -27,7 +27,7 @@ fn should_transpile(media_type: &MediaType) -> bool { | MediaType::Dcts | MediaType::Tsx => true, - _ => return false, + _ => false, } } diff --git a/src/utilities.rs b/src/utilities.rs index 3b73500..59c4377 100644 --- a/src/utilities.rs +++ b/src/utilities.rs @@ -54,7 +54,7 @@ pub fn validate(javascript: &str) -> Result { let mut runtime = Runtime::new(Default::default())?; match runtime.load_modules(&module, vec![]) { Ok(_) => Ok(true), - Err(e) if matches!(e, Error::Runtime(_)) => Ok(false), + Err(Error::Runtime(_)) => Ok(false), Err(e) => Err(e), } }