From 386f34af802a812c2af8ece2cc427cfb5a7c1fe8 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Mar 2020 17:26:59 +0000 Subject: Implement `missing` and `env:VAR` imports --- dhall/src/error/mod.rs | 2 ++ dhall/src/semantics/resolve/resolve.rs | 24 ++++++++++++++++-------- dhall/src/tests.rs | 2 ++ 3 files changed, 20 insertions(+), 8 deletions(-) (limited to 'dhall/src') 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 { - 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) => { -- cgit v1.2.3