diff options
Diffstat (limited to '')
-rw-r--r-- | dhall/Cargo.toml | 9 | ||||
-rw-r--r-- | dhall/src/semantics/parse.rs | 4 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/cache.rs | 80 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/resolve.rs | 16 |
4 files changed, 46 insertions, 63 deletions
diff --git a/dhall/Cargo.toml b/dhall/Cargo.toml index 376506f..adf92d6 100644 --- a/dhall/Cargo.toml +++ b/dhall/Cargo.toml @@ -19,13 +19,20 @@ once_cell = "1.3.1" percent-encoding = "2.1.0" pest = "2.1" pest_consume = "1.0" -reqwest = { version = "0.10", features = ["blocking"] } serde = "1.0" serde_cbor = "0.9.0" sha2 = "0.8.1" smallvec = "1.0.0" url = "2.1" +[target.'cfg(not(target_arch = "wasm32"))'.dependencies] +reqwest = { version = "0.10", features = ["blocking"] } + +[target.'cfg(target_arch = "wasm32")'.dependencies] +# Disable native tls because hyper doesn't build on wasm +reqwest = { version = "0.10", default-features = false } +blocking = "0.4.3" + [dev-dependencies] pretty_assertions = "0.6.1" version-sync = "0.8" 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<Parsed, Error> { } pub fn parse_remote(url: Url) -> Result<Parsed, Error> { - 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/cache.rs b/dhall/src/semantics/resolve/cache.rs index b725bf3..164baea 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -12,36 +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"; - -fn alternate_env_var_cache_dir( - provider: impl Fn(&str) -> Result<String, VarError>, -) -> Option<PathBuf> { - provider(ALTERNATE_ENV_VAR) - .map(PathBuf::from) - .map(|env_dir| env_dir.join(".cache").join("dhall")) - .ok() -} +const ALTERNATE_CACHE_ENV_VAR: &str = "LOCALAPPDATA"; -fn env_var_cache_dir( - provider: impl Fn(&str) -> Result<String, VarError>, -) -> Option<PathBuf> { - provider("XDG_CACHE_HOME") - .map(PathBuf::from) - .map(|cache_home| cache_home.join("dhall")) - .ok() +#[cfg(any(unix, windows))] +fn load_cache_dir( + env_provider: impl Fn(&str) -> Result<String, VarError> + Copy, +) -> Result<PathBuf, CacheError> { + 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 load_cache_dir( - provider: impl Fn(&str) -> Result<String, VarError> + Copy, + _provider: impl Fn(&str) -> Result<String, VarError> + Copy, ) -> Result<PathBuf, CacheError> { - env_var_cache_dir(provider) - .or_else(|| alternate_env_var_cache_dir(provider)) - .ok_or(CacheError::MissingConfiguration) + Err(CacheError::MissingConfiguration) } #[derive(Debug, PartialEq)] @@ -184,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")); @@ -228,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")); 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<String, Error> { 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<String, Error> { + Ok(reqwest::blocking::get(url).unwrap().text().unwrap()) +} +#[cfg(target_arch = "wasm32")] +pub(crate) fn download_http_text(url: Url) -> Result<String, Error> { + 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(); |