diff options
author | Nadrieril | 2019-04-11 13:14:13 +0200 |
---|---|---|
committer | Nadrieril | 2019-04-11 13:14:13 +0200 |
commit | d3f14966120fae672dc890b718ebada74ebed533 (patch) | |
tree | 887b4af84bc2c69886616a4a9c8aa27fa846238a /dhall/src/imports.rs | |
parent | 982f86c34f69bb78b45a4b8b37a5bf5731f881eb (diff) | |
parent | 82d62c4d7d423817a4fd9d6294d27d18d60bcd22 (diff) |
Merge branch 'serde'
Diffstat (limited to '')
-rw-r--r-- | dhall/src/imports.rs | 74 |
1 files changed, 21 insertions, 53 deletions
diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index fdde8c3..7810c55 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,46 +1,17 @@ -// use dhall_core::{Expr, FilePrefix, Import, ImportLocation, ImportMode, X}; -use dhall_core::{Expr, Import, X}; -// use std::path::Path; -use crate::binary::DecodeError; +use crate::error::Error; use crate::expr::*; use dhall_core::*; -use std::fmt; +use quick_error::quick_error; use std::fs::File; use std::io::Read; use std::path::Path; use std::path::PathBuf; -#[derive(Debug)] -pub enum ImportError { - ParseError(ParseError), - BinaryDecodeError(DecodeError), - IOError(std::io::Error), - UnexpectedImportError(Import), -} -impl From<ParseError> for ImportError { - fn from(e: ParseError) -> Self { - ImportError::ParseError(e) - } -} -impl From<DecodeError> for ImportError { - fn from(e: DecodeError) -> Self { - ImportError::BinaryDecodeError(e) - } -} -impl From<std::io::Error> 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), - BinaryDecodeError(_) => unimplemented!(), - IOError(e) => e.fmt(f), - UnexpectedImportError(e) => e.fmt(f), - } +quick_error! { + #[derive(Debug)] + pub enum ImportError { + Recursive(import: Import, err: Box<Error>) {} + UnexpectedImport(import: Import) {} } } @@ -53,7 +24,7 @@ pub enum ImportRoot { fn resolve_import( import: &Import, root: &ImportRoot, -) -> Result<Expr<X, X>, ImportError> { +) -> Result<Resolved, ImportError> { use self::ImportRoot::*; use dhall_core::FilePrefix::*; use dhall_core::ImportLocation::*; @@ -63,16 +34,23 @@ fn resolve_import( match &import.location_hashed.location { Local(prefix, path) => { let path = match prefix { + // TODO: fail gracefully Parent => cwd.parent().unwrap().join(path), Here => cwd.join(path), _ => unimplemented!("{:?}", import), }; - load_dhall_file(&path, true) + Ok(load_import(&path).map_err(|e| { + ImportError::Recursive(import.clone(), Box::new(e)) + })?) } _ => unimplemented!("{:?}", import), } } +fn load_import(f: &Path) -> Result<Resolved, Error> { + Ok(Parsed::parse_file(f)?.resolve()?) +} + fn resolve_expr( Parsed(expr, root): Parsed, allow_imports: bool, @@ -80,9 +58,9 @@ fn resolve_expr( let resolve = |import: &Import| -> Result<SubExpr<X, X>, ImportError> { if allow_imports { let expr = resolve_import(import, &root)?; - Ok(expr.roll()) + Ok(expr.0) } else { - Err(ImportError::UnexpectedImportError(import.clone())) + Err(ImportError::UnexpectedImport(import.clone())) } }; let expr = expr.as_ref().traverse_embed(&resolve)?; @@ -90,7 +68,7 @@ fn resolve_expr( } impl Parsed { - pub fn load_from_file(f: &Path) -> Result<Parsed, ImportError> { + pub fn parse_file(f: &Path) -> Result<Parsed, Error> { let mut buffer = String::new(); File::open(f)?.read_to_string(&mut buffer)?; let expr = parse_expr(&*buffer)?; @@ -98,13 +76,13 @@ impl Parsed { Ok(Parsed(expr, root)) } - pub fn load_from_str(s: &str) -> Result<Parsed, ImportError> { + pub fn parse_str(s: &str) -> Result<Parsed, Error> { let expr = parse_expr(s)?; let root = ImportRoot::LocalDir(std::env::current_dir()?); Ok(Parsed(expr, root)) } - pub fn load_from_binary_file(f: &Path) -> Result<Parsed, ImportError> { + pub fn parse_binary_file(f: &Path) -> Result<Parsed, Error> { let mut buffer = Vec::new(); File::open(f)?.read_to_end(&mut buffer)?; let expr = crate::binary::decode(&buffer)?; @@ -119,13 +97,3 @@ impl Parsed { crate::imports::resolve_expr(self, false) } } - -// Deprecated -pub fn load_dhall_file( - f: &Path, - resolve_imports: bool, -) -> Result<Expr<X, X>, ImportError> { - let expr = Parsed::load_from_file(f)?; - let expr = resolve_expr(expr, resolve_imports)?; - Ok(expr.0.unroll()) -} |