From 396ec334bac1e8d10a2d2b2d683c93e3b2ff4d8d Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sat, 6 Apr 2019 20:04:04 +0200 Subject: Massage import loading into new API Closes #9 --- dhall/src/expr.rs | 30 ++++++++++----- dhall/src/imports.rs | 95 ++++++++++++++++++++++++++++------------------- dhall/tests/common/mod.rs | 9 ++++- 3 files changed, 83 insertions(+), 51 deletions(-) (limited to 'dhall') diff --git a/dhall/src/expr.rs b/dhall/src/expr.rs index 0d093cb..72633ea 100644 --- a/dhall/src/expr.rs +++ b/dhall/src/expr.rs @@ -1,17 +1,27 @@ +use crate::imports::ImportError; +use crate::imports::ImportRoot; use crate::typecheck::TypeError; use dhall_core::*; -pub struct Parsed(SubExpr); -pub struct Resolved(SubExpr); -pub struct Typed(SubExpr, Type); -pub struct Type(Box); -pub struct Normalized(SubExpr); +#[derive(Debug, Clone)] +pub struct Parsed(pub(crate) SubExpr, pub(crate) ImportRoot); +#[derive(Debug, Clone)] +pub struct Resolved(pub(crate) SubExpr); +#[derive(Debug, Clone)] +pub struct Typed(pub(crate) SubExpr, Type); +#[derive(Debug, Clone)] +pub struct Type(pub(crate) Box); +#[derive(Debug, Clone)] +pub struct Normalized(pub(crate) SubExpr); -// impl Parsed { -// pub fn resolve(self) -> Result { -// Ok(Resolved(crate::imports::resolve(self.0)?)) -// } -// } +impl Parsed { + pub fn resolve(self) -> Result { + crate::imports::resolve_expr(self, true) + } + pub fn resolve_no_imports(self) -> Result { + crate::imports::resolve_expr(self, false) + } +} impl Resolved { pub fn typecheck(self) -> Result> { let typ = Type(Box::new(Normalized(crate::typecheck::type_of( diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index 95cf6fa..bc38bb6 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,6 +1,7 @@ // use dhall_core::{Expr, FilePrefix, Import, ImportLocation, ImportMode, X}; use dhall_core::{Expr, Import, X}; // use std::path::Path; +use crate::expr::*; use dhall_core::*; use std::fmt; use std::fs::File; @@ -8,6 +9,34 @@ use std::io::Read; use std::path::Path; use std::path::PathBuf; +#[derive(Debug)] +pub enum ImportError { + ParseError(ParseError), + IOError(std::io::Error), + UnexpectedImportError(Import), +} +impl From for ImportError { + fn from(e: ParseError) -> Self { + ImportError::ParseError(e) + } +} +impl From for ImportError { + fn from(e: std::io::Error) -> Self { + ImportError::IOError(e) + } +} +impl fmt::Display for ImportError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use self::ImportError::*; + match self { + ParseError(e) => e.fmt(f), + IOError(e) => e.fmt(f), + UnexpectedImportError(e) => e.fmt(f), + } + } +} + +// Deprecated pub fn panic_imports(expr: &Expr) -> Expr { let no_import = |i: &Import| -> X { panic!("ahhh import: {:?}", i) }; expr.map_embed(&no_import) @@ -42,55 +71,43 @@ fn resolve_import( } } -#[derive(Debug)] -pub enum ImportError { - ParseError(ParseError), - IOError(std::io::Error), -} -impl From for ImportError { - fn from(e: ParseError) -> Self { - ImportError::ParseError(e) - } -} -impl From for ImportError { - fn from(e: std::io::Error) -> Self { - ImportError::IOError(e) - } -} -impl fmt::Display for ImportError { - fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { - use self::ImportError::*; - match self { - ParseError(e) => e.fmt(f), - IOError(e) => e.fmt(f), +pub(crate) fn resolve_expr( + Parsed(expr, root): Parsed, + allow_imports: bool, +) -> Result { + let resolve = |import: &Import| -> Result, ImportError> { + if allow_imports { + let expr = resolve_import(import, &root)?; + Ok(expr.roll()) + } else { + Err(ImportError::UnexpectedImportError(import.clone())) } - } + }; + let expr = expr.as_ref().traverse_embed(&resolve)?; + Ok(Resolved(expr.squash_embed())) +} + +pub fn load_from_file(f: &Path) -> Result { + let mut buffer = String::new(); + File::open(f)?.read_to_string(&mut buffer)?; + let expr = parse_expr(&*buffer)?; + let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); + Ok(Parsed(expr, root)) } +// Deprecated pub fn load_dhall_file( f: &Path, resolve_imports: bool, ) -> Result, ImportError> { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; - let expr = if resolve_imports { - let root = ImportRoot::LocalDir(f.parent().unwrap().to_owned()); - let resolve = |import: &Import| -> Expr { - resolve_import(import, &root).unwrap() - }; - expr.as_ref().map_embed(&resolve).squash_embed() - } else { - panic_imports(expr.as_ref()) - }; - Ok(expr) + let expr = load_from_file(f)?; + let expr = resolve_expr(expr, resolve_imports)?; + Ok(expr.0.unroll()) } +// Deprecated pub fn load_dhall_file_no_resolve_imports( f: &Path, ) -> Result { - let mut buffer = String::new(); - File::open(f)?.read_to_string(&mut buffer)?; - let expr = parse_expr(&*buffer)?; - Ok(expr) + Ok(load_from_file(f)?.0) } diff --git a/dhall/tests/common/mod.rs b/dhall/tests/common/mod.rs index e748cf0..70b7d81 100644 --- a/dhall/tests/common/mod.rs +++ b/dhall/tests/common/mod.rs @@ -54,6 +54,12 @@ pub fn read_dhall_file_no_resolve_imports<'i>( load_dhall_file_no_resolve_imports(&PathBuf::from(file_path)) } +pub fn load_from_file_str<'i>( + file_path: &str, +) -> Result { + load_from_file(&PathBuf::from(file_path)) +} + pub fn run_test(base_path: &str, feature: Feature) { use self::Feature::*; let base_path_prefix = match feature { @@ -90,8 +96,7 @@ pub fn run_test(base_path: &str, feature: Feature) { } ParserFailure => { let file_path = base_path + ".dhall"; - let err = - read_dhall_file_no_resolve_imports(&file_path).unwrap_err(); + let err = load_from_file_str(&file_path).unwrap_err(); match err { ImportError::ParseError(_) => {} e => panic!("Expected parse error, got: {:?}", e), -- cgit v1.2.3