From d8c8514c6a903b155345f419cb56daa91d27c141 Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Thu, 7 Mar 2019 23:37:22 +0100 Subject: Make test harness less macro-y --- dhall/src/lib.rs | 46 ++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 46 insertions(+) (limited to 'dhall/src/lib.rs') diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 4e56264..4e183b4 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -6,3 +6,49 @@ mod normalize; pub use crate::normalize::*; pub mod imports; pub mod typecheck; + +use dhall_core::*; +use std::fs::File; +use std::io::Read; +use std::path::Path; +use std::fmt; + +#[derive(Debug)] +pub enum DhallError { + ParseError(parser::ParseError), + IOError(std::io::Error), +} +impl From for DhallError { + fn from(e: parser::ParseError) -> Self { + DhallError::ParseError(e) + } +} +impl From for DhallError { + fn from(e: std::io::Error) -> Self { + DhallError::IOError(e) + } +} +impl fmt::Display for DhallError { + fn fmt(&self, f: &mut fmt::Formatter) -> Result<(), fmt::Error> { + use self::DhallError::*; + match self { + ParseError(e) => e.fmt(f), + IOError(e) => e.fmt(f), + } + } +} + +/// `source_pool` is a Vec of strings to be used to store the contents of imported files. +/// No ideal but necessary for lifetime reasons for now. +pub fn load_dhall_file<'i, 'a: 'i>( + f: &Path, + source_pool: &'a mut Vec, + resolve_imports: bool, +) -> Result, DhallError> { + source_pool.push(String::new()); + let mut buffer = source_pool.last_mut().unwrap(); + File::open(f)?.read_to_string(&mut buffer)?; + let expr = parser::parse_expr(&*buffer)?; + let expr = imports::resolve_imports(&expr); + Ok(expr) +} -- cgit v1.2.3