diff options
Diffstat (limited to '')
-rw-r--r-- | Cargo.lock | 7 | ||||
-rw-r--r-- | dhall/Cargo.toml | 1 | ||||
-rw-r--r-- | dhall/src/error.rs | 25 | ||||
-rw-r--r-- | dhall/src/imports.rs | 54 | ||||
-rw-r--r-- | dhall/src/tests.rs | 21 | ||||
-rw-r--r-- | dhall/src/traits/deserialize.rs | 6 |
6 files changed, 58 insertions, 56 deletions
@@ -73,6 +73,7 @@ dependencies = [ "itertools 0.8.0 (registry+https://github.com/rust-lang/crates.io-index)", "lalrpop-util 0.16.3 (registry+https://github.com/rust-lang/crates.io-index)", "pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)", + "quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)", "serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)", "serde_cbor 0.9.0 (registry+https://github.com/rust-lang/crates.io-index)", "term-painter 0.2.4 (registry+https://github.com/rust-lang/crates.io-index)", @@ -266,6 +267,11 @@ dependencies = [ ] [[package]] +name = "quick-error" +version = "1.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] name = "quote" version = "0.6.11" source = "registry+https://github.com/rust-lang/crates.io-index" @@ -448,6 +454,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" "checksum pretty 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "f60c0d9f6fc88ecdd245d90c1920ff76a430ab34303fc778d33b1d0a4c3bf6d3" "checksum pretty_assertions 0.6.1 (registry+https://github.com/rust-lang/crates.io-index)" = "3f81e1644e1b54f5a68959a29aa86cde704219254669da328ecfdf6a1f09d427" "checksum proc-macro2 0.4.27 (registry+https://github.com/rust-lang/crates.io-index)" = "4d317f9caece796be1980837fd5cb3dfec5613ebdb04ad0956deea83ce168915" +"checksum quick-error 1.2.2 (registry+https://github.com/rust-lang/crates.io-index)" = "9274b940887ce9addde99c4eee6b5c44cc494b182b97e73dc8ffdcb3397fd3f0" "checksum quote 0.6.11 (registry+https://github.com/rust-lang/crates.io-index)" = "cdd8e04bd9c52e0342b406469d494fcb033be4bdbe5c606016defbb1681411e1" "checksum same-file 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)" = "8f20c4be53a8a1ff4c1f1b2bd14570d2f634628709752f0702ecdd2b3f9a5267" "checksum serde 1.0.90 (registry+https://github.com/rust-lang/crates.io-index)" = "aa5f7c20820475babd2c077c3ab5f8c77a31c15e16ea38687b4c02d3e48680f4" diff --git a/dhall/Cargo.toml b/dhall/Cargo.toml index 7aa8b4f..a7c21dd 100644 --- a/dhall/Cargo.toml +++ b/dhall/Cargo.toml @@ -16,6 +16,7 @@ bytecount = "0.5.1" itertools = "0.8.0" lalrpop-util = "0.16.3" term-painter = "0.2.3" +quick-error = "1.2.2" serde = { version = "1.0", features = ["derive"] } serde_cbor = "0.9.0" dhall_core = { path = "../dhall_core" } diff --git a/dhall/src/error.rs b/dhall/src/error.rs index ef8dd34..eba7ff5 100644 --- a/dhall/src/error.rs +++ b/dhall/src/error.rs @@ -1,3 +1,24 @@ -// TODO -pub type Error = (); +use quick_error::quick_error; + pub type Result<T> = std::result::Result<T, Error>; + +quick_error! { + #[derive(Debug)] + pub enum Error { + IO(err: std::io::Error) { + from() + } + Parse(err: dhall_core::ParseError) { + from() + } + Decode(err: crate::binary::DecodeError) { + from() + } + Resolve(err: crate::imports::ImportError) { + from() + } + Typecheck(err: crate::typecheck::TypeError<dhall_core::X>) { + from() + } + } +} diff --git a/dhall/src/imports.rs b/dhall/src/imports.rs index a65be4f..3c33b4e 100644 --- a/dhall/src/imports.rs +++ b/dhall/src/imports.rs @@ -1,46 +1,20 @@ // 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) {} } } @@ -67,7 +41,9 @@ fn resolve_import( Here => cwd.join(path), _ => unimplemented!("{:?}", import), }; - load_dhall_file(&path, true) + Ok(load_dhall_file(&path, true).map_err(|e| { + ImportError::Recursive(import.clone(), Box::new(e)) + })?) } _ => unimplemented!("{:?}", import), } @@ -82,7 +58,7 @@ fn resolve_expr( let expr = resolve_import(import, &root)?; Ok(expr.roll()) } else { - Err(ImportError::UnexpectedImportError(import.clone())) + Err(ImportError::UnexpectedImport(import.clone())) } }; let expr = expr.as_ref().traverse_embed(&resolve)?; @@ -90,7 +66,7 @@ fn resolve_expr( } impl Parsed { - pub fn parse_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 +74,13 @@ impl Parsed { Ok(Parsed(expr, root)) } - pub fn parse_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 parse_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)?; @@ -124,7 +100,7 @@ impl Parsed { pub fn load_dhall_file( f: &Path, resolve_imports: bool, -) -> Result<Expr<X, X>, ImportError> { +) -> Result<Expr<X, X>, Error> { let expr = Parsed::parse_file(f)?; let expr = resolve_expr(expr, resolve_imports)?; Ok(expr.0.unroll()) diff --git a/dhall/src/tests.rs b/dhall/src/tests.rs index 737a8a4..9e78c2f 100644 --- a/dhall/src/tests.rs +++ b/dhall/src/tests.rs @@ -29,7 +29,8 @@ macro_rules! make_spec_test { }; } -use crate::imports::ImportError; +use crate::error::{Error, Result}; +use crate::expr::Parsed; use crate::*; use dhall_core::*; use dhall_generator as dhall; @@ -47,20 +48,16 @@ pub enum Feature { } // Deprecated -fn read_dhall_file<'i>(file_path: &str) -> Result<Expr<X, X>, ImportError> { +fn read_dhall_file<'i>(file_path: &str) -> Result<Expr<X, X>> { crate::imports::load_dhall_file(&PathBuf::from(file_path), true) } -fn parse_file_str<'i>( - file_path: &str, -) -> Result<crate::expr::Parsed, ImportError> { - crate::expr::Parsed::parse_file(&PathBuf::from(file_path)) +fn parse_file_str<'i>(file_path: &str) -> Result<Parsed> { + Parsed::parse_file(&PathBuf::from(file_path)) } -fn parse_binary_file_str<'i>( - file_path: &str, -) -> Result<crate::expr::Parsed, ImportError> { - crate::expr::Parsed::parse_binary_file(&PathBuf::from(file_path)) +fn parse_binary_file_str<'i>(file_path: &str) -> Result<Parsed> { + Parsed::parse_binary_file(&PathBuf::from(file_path)) } pub fn run_test(base_path: &str, feature: Feature) { @@ -91,7 +88,7 @@ pub fn run_test(base_path: &str, feature: Feature) { assert_eq_pretty!(expr, expected); // Round-trip pretty-printer - let expr: crate::expr::Parsed = + let expr: Parsed = crate::from_str(&expr.to_string(), None).unwrap(); assert_eq!(expr, expected); } @@ -99,7 +96,7 @@ pub fn run_test(base_path: &str, feature: Feature) { let file_path = base_path + ".dhall"; let err = parse_file_str(&file_path).unwrap_err(); match err { - ImportError::ParseError(_) => {} + Error::Parse(_) => {} e => panic!("Expected parse error, got: {:?}", e), } } diff --git a/dhall/src/traits/deserialize.rs b/dhall/src/traits/deserialize.rs index 8d1f565..ad4cde6 100644 --- a/dhall/src/traits/deserialize.rs +++ b/dhall/src/traits/deserialize.rs @@ -9,7 +9,7 @@ impl<'a> Deserialize<'a> for Parsed { /// Simply parses the provided string. Ignores the /// provided type. fn from_str(s: &'a str, _ty: Option<&Type>) -> Result<Self> { - Ok(Parsed::parse_str(s).map_err(|_| ())?) + Ok(Parsed::parse_str(s)?) } } @@ -17,7 +17,7 @@ impl<'a> Deserialize<'a> for Resolved { /// Parses and resolves the provided string. Ignores the /// provided type. fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> { - Ok(Parsed::from_str(s, ty)?.resolve().map_err(|_| ())?) + Ok(Parsed::from_str(s, ty)?.resolve()?) } } @@ -25,7 +25,7 @@ impl<'a> Deserialize<'a> for Typed { /// Parses, resolves and typechecks the provided string. fn from_str(s: &'a str, ty: Option<&Type>) -> Result<Self> { // TODO: compare with provided type - Ok(Resolved::from_str(s, ty)?.typecheck().map_err(|_| ())?) + Ok(Resolved::from_str(s, ty)?.typecheck()?) } } |