diff options
Diffstat (limited to '')
-rw-r--r-- | dhall/src/error/mod.rs | 2 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/resolve.rs | 24 | ||||
-rw-r--r-- | dhall/src/tests.rs | 2 |
3 files changed, 20 insertions, 8 deletions
diff --git a/dhall/src/error/mod.rs b/dhall/src/error/mod.rs index 8829d47..5632ea6 100644 --- a/dhall/src/error/mod.rs +++ b/dhall/src/error/mod.rs @@ -26,6 +26,8 @@ pub(crate) enum ErrorKind { #[derive(Debug)] pub(crate) enum ImportError { + Missing, + MissingEnvVar, UnexpectedImport(Import<()>), ImportCycle(ImportStack, Import<()>), } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index b27dd2c..bf7aabb 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -1,6 +1,7 @@ use itertools::Itertools; use std::borrow::Cow; -use std::path::{Path, PathBuf}; +use std::env; +use std::path::PathBuf; use crate::error::ErrorBuilder; use crate::error::{Error, ImportError}; @@ -47,8 +48,21 @@ fn resolve_one_import( FilePrefix::Here => cwd.join(path_buf), _ => unimplemented!("{:?}", import), }; - Ok(load_import(env, &path_buf)?) + + let parsed = Parsed::parse_file(&path_buf)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) + } + ImportLocation::Env(var_name) => { + let val = match env::var(var_name) { + Ok(val) => val, + Err(_) => Err(ImportError::MissingEnvVar)?, + }; + let parsed = Parsed::parse_str(&val)?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + Ok((typed.normalize().to_hir(), typed.ty().clone())) } + ImportLocation::Missing => Err(ImportError::Missing.into()), _ => unimplemented!("{:?}", import), } } @@ -122,12 +136,6 @@ fn resolve_one_import( } } -fn load_import(env: &mut ImportEnv, f: &Path) -> Result<TypedHir, Error> { - let parsed = Parsed::parse_file(f)?; - let typed = resolve_with_env(env, parsed)?.typecheck()?; - Ok((typed.normalize().to_hir(), typed.ty().clone())) -} - /// Desugar the first level of the expression. fn desugar(expr: &Expr) -> Cow<'_, Expr> { match expr.kind() { diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 044b185..369c6cc 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -247,6 +247,8 @@ fn run_test(test: Test) -> Result<()> { env::set_current_dir( PathBuf::from(env!("CARGO_MANIFEST_DIR")).parent().unwrap(), )?; + // Set environment variable for import tests. + env::set_var("DHALL_TEST_VAR", "6 * 7"); match test { ParserSuccess(expr, expected) => { |