From 4aab03c67869ff4ecc473794f62e35e4565a01d6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Fri, 29 May 2020 22:22:30 +0100 Subject: Make reqwest build on wasm --- dhall/src/semantics/parse.rs | 4 ++-- dhall/src/semantics/resolve/resolve.rs | 16 +++++++++++++--- 2 files changed, 15 insertions(+), 5 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/parse.rs b/dhall/src/semantics/parse.rs index 2326471..82396e0 100644 --- a/dhall/src/semantics/parse.rs +++ b/dhall/src/semantics/parse.rs @@ -4,7 +4,7 @@ use std::path::Path; use url::Url; use crate::error::Error; -use crate::semantics::resolve::ImportLocation; +use crate::semantics::resolve::{download_http_text, ImportLocation}; use crate::syntax::binary; use crate::syntax::parse_expr; use crate::Parsed; @@ -17,7 +17,7 @@ pub fn parse_file(f: &Path) -> Result { } pub fn parse_remote(url: Url) -> Result { - let body = reqwest::blocking::get(url.clone()).unwrap().text().unwrap(); + let body = download_http_text(url.clone())?; let expr = parse_expr(&body)?; let root = ImportLocation::Remote(url); Ok(Parsed(expr, root)) diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 3b72b9e..fb770db 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -159,9 +159,7 @@ impl ImportLocation { fn fetch_text(self) -> Result { Ok(match self { ImportLocation::Local(path) => std::fs::read_to_string(&path)?, - ImportLocation::Remote(url) => { - reqwest::blocking::get(url).unwrap().text().unwrap() - } + ImportLocation::Remote(url) => download_http_text(url)?, ImportLocation::Env(var_name) => match env::var(var_name) { Ok(val) => val, Err(_) => return Err(ImportError::MissingEnvVar.into()), @@ -197,6 +195,18 @@ fn mkexpr(kind: UnspannedExpr) -> Expr { Expr::new(kind, Span::Artificial) } +// TODO: error handling +#[cfg(not(target_arch = "wasm32"))] +pub(crate) fn download_http_text(url: Url) -> Result { + Ok(reqwest::blocking::get(url).unwrap().text().unwrap()) +} +#[cfg(target_arch = "wasm32")] +pub(crate) fn download_http_text(url: Url) -> Result { + blocking::block_on(async { + Ok(reqwest::get(url).await.unwrap().text().await.unwrap()) + }) +} + fn make_aslocation_uniontype() -> Expr { let text_type = mkexpr(ExprKind::Builtin(Builtin::Text)); let mut union = BTreeMap::default(); -- cgit v1.2.3 From ec297fd75217348d81c7cee5f154d77aa7f0e42b Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 30 May 2020 11:39:07 +0100 Subject: Fix cache path resolution on wasm --- dhall/src/semantics/resolve/cache.rs | 7 +++++++ 1 file changed, 7 insertions(+) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs index b725bf3..7fdd758 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -18,6 +18,7 @@ const ALTERNATE_ENV_VAR: &str = "HOME"; #[cfg(windows)] const ALTERNATE_ENV_VAR: &str = "LOCALAPPDATA"; +#[cfg(any(unix, windows))] fn alternate_env_var_cache_dir( provider: impl Fn(&str) -> Result, ) -> Option { @@ -26,6 +27,12 @@ fn alternate_env_var_cache_dir( .map(|env_dir| env_dir.join(".cache").join("dhall")) .ok() } +#[cfg(not(any(unix, windows)))] +fn alternate_env_var_cache_dir( + _provider: impl Fn(&str) -> Result, +) -> Option { + None +} fn env_var_cache_dir( provider: impl Fn(&str) -> Result, -- cgit v1.2.3 From 15b20bd14136c5e8e44fa5d0be4b8d4eb4411cb6 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 30 May 2020 11:44:46 +0100 Subject: Refactor cache path resolution --- dhall/src/semantics/resolve/cache.rs | 83 +++++++++--------------------------- 1 file changed, 21 insertions(+), 62 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs index 7fdd758..164baea 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -12,43 +12,32 @@ use std::env::VarError; use std::ffi::OsStr; use std::fs::File; +#[cfg(any(unix, windows))] +const CACHE_ENV_VAR: &str = "XDG_CACHE_HOME"; #[cfg(unix)] -const ALTERNATE_ENV_VAR: &str = "HOME"; - +const ALTERNATE_CACHE_ENV_VAR: &str = "HOME"; #[cfg(windows)] -const ALTERNATE_ENV_VAR: &str = "LOCALAPPDATA"; +const ALTERNATE_CACHE_ENV_VAR: &str = "LOCALAPPDATA"; #[cfg(any(unix, windows))] -fn alternate_env_var_cache_dir( - provider: impl Fn(&str) -> Result, -) -> Option { - provider(ALTERNATE_ENV_VAR) - .map(PathBuf::from) - .map(|env_dir| env_dir.join(".cache").join("dhall")) - .ok() +fn load_cache_dir( + env_provider: impl Fn(&str) -> Result + Copy, +) -> Result { + let env_provider = |s| { + env_provider(s) + .map(PathBuf::from) + .map_err(|_| CacheError::MissingConfiguration) + }; + let cache_base_path = env_provider(CACHE_ENV_VAR).or_else(|_| { + env_provider(ALTERNATE_CACHE_ENV_VAR).map(|path| path.join(".cache")) + })?; + Ok(cache_base_path.join("dhall")) } #[cfg(not(any(unix, windows)))] -fn alternate_env_var_cache_dir( - _provider: impl Fn(&str) -> Result, -) -> Option { - None -} - -fn env_var_cache_dir( - provider: impl Fn(&str) -> Result, -) -> Option { - provider("XDG_CACHE_HOME") - .map(PathBuf::from) - .map(|cache_home| cache_home.join("dhall")) - .ok() -} - fn load_cache_dir( - provider: impl Fn(&str) -> Result + Copy, + _provider: impl Fn(&str) -> Result + Copy, ) -> Result { - env_var_cache_dir(provider) - .or_else(|| alternate_env_var_cache_dir(provider)) - .ok_or(CacheError::MissingConfiguration) + Err(CacheError::MissingConfiguration) } #[derive(Debug, PartialEq)] @@ -191,41 +180,11 @@ mod test { use rand::Rng; use std::env::temp_dir; - #[cfg(unix)] - #[test] - fn alternate_env_var_cache_dir_should_result_unix_folder_path() { - let actual = - alternate_env_var_cache_dir(|_| Ok("/home/user".to_string())); - assert_eq!(actual, Some(PathBuf::from("/home/user/.cache/dhall"))); - } - - #[test] - fn alternate_env_var_cache_dir_should_result_none_on_missing_var() { - let actual = alternate_env_var_cache_dir(|_| Err(VarError::NotPresent)); - assert_eq!(actual, None); - } - - #[test] - fn env_var_cache_dir_should_result_xdg_cache_home() { - let actual = env_var_cache_dir(|_| { - Ok("/home/user/custom/path/for/cache".to_string()) - }); - assert_eq!( - actual, - Some(PathBuf::from("/home/user/custom/path/for/cache/dhall")) - ); - } - - #[test] - fn env_var_cache_dir_should_result_none_on_missing_var() { - let actual = env_var_cache_dir(|_| Err(VarError::NotPresent)); - assert_eq!(actual, None); - } - + #[cfg(any(unix, windows))] #[test] fn load_cache_dir_should_result_xdg_cache_first() { let actual = load_cache_dir(|var| match var { - "XDG_CACHE_HOME" => Ok("/home/user/custom".to_string()), + CACHE_ENV_VAR => Ok("/home/user/custom".to_string()), _ => Err(VarError::NotPresent), }); assert_eq!(actual.unwrap(), PathBuf::from("/home/user/custom/dhall")); @@ -235,7 +194,7 @@ mod test { #[test] fn load_cache_dir_should_result_alternate() { let actual = load_cache_dir(|var| match var { - ALTERNATE_ENV_VAR => Ok("/home/user".to_string()), + ALTERNATE_CACHE_ENV_VAR => Ok("/home/user".to_string()), _ => Err(VarError::NotPresent), }); assert_eq!(actual.unwrap(), PathBuf::from("/home/user/.cache/dhall")); -- cgit v1.2.3