From 43b360789afbed8adb958c95bb198b1618aa5c0d Mon Sep 17 00:00:00 2001 From: fteychene Date: Fri, 15 May 2020 00:55:30 +0200 Subject: fixup! Release serde_dhall version 0.5.2 --- dhall/src/semantics/resolve/cache.rs | 58 ++++++++++++++++++------------- dhall/src/semantics/resolve/resolve.rs | 63 ++++++++++++++++++---------------- 2 files changed, 67 insertions(+), 54 deletions(-) (limited to 'dhall/src/semantics') diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs index 3b57cdf..e616038 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -1,6 +1,6 @@ use std::env; use std::io::Write; -use std::path::{PathBuf, Path}; +use std::path::{Path, PathBuf}; use crate::error::{CacheError, Error, ErrorKind}; use crate::parse::parse_binary_file; @@ -56,8 +56,11 @@ impl Cache { impl Cache { fn cache_file(&self, import: &Import) -> Option { - self.cache_dir.as_ref() - .and_then(|cache_dir| import.hash.as_ref().map(|hash| (cache_dir, hash))) + self.cache_dir + .as_ref() + .and_then(|cache_dir| { + import.hash.as_ref().map(|hash| (cache_dir, hash)) + }) .map(|(cache_dir, hash)| cache_dir.join(cache_filename(hash))) } @@ -80,22 +83,29 @@ impl Cache { // Side effect since we don't use the result fn save_expr(&self, import: &Import, expr: &Expr) { self.cache_file(import) - .map(|cache_file| { - save_expr(cache_file.as_path(), expr) - }); + .map(|cache_file| save_expr(cache_file.as_path(), expr)); } - pub fn caching_import(&self, import: &Import, fetcher: F, mut resolver: R) -> Result - where F: FnOnce() -> Result, - R: FnMut(Parsed) -> Result { + pub fn caching_import( + &self, + import: &Import, + fetcher: F, + mut resolver: R, + ) -> Result + where + F: FnOnce() -> Result, + R: FnMut(Parsed) -> Result, + { // Lookup the cache self.search_cache(import) // On cache found .and_then(|cache_result| { // Try to resolve the cache imported content - match cache_result.and_then(|parsed| resolver(parsed)) - .and_then(|typed_hir| check_hash(import.hash.as_ref().unwrap(), typed_hir)) - { + match cache_result.and_then(|parsed| resolver(parsed)).and_then( + |typed_hir| { + check_hash(import.hash.as_ref().unwrap(), typed_hir) + }, + ) { // Cache content is invalid (can't be parsed / can't be resolved / content sha invalid ) Err(_) => { // Delete cache file since it's invalid @@ -104,23 +114,23 @@ impl Cache { None } // Cache valid - r => { - Some(r) - } + r => Some(r), } - }).unwrap_or_else(|| { - // Fetch and resolve as provided - let imported = fetcher().and_then(resolver); - // Save in cache the result if ok - let _ = imported.as_ref().map(|(hir, _)| self.save_expr(import, &hir.to_expr_noopts())); - imported - }) + }) + .unwrap_or_else(|| { + // Fetch and resolve as provided + let imported = fetcher().and_then(resolver); + // Save in cache the result if ok + let _ = imported.as_ref().map(|(hir, _)| { + self.save_expr(import, &hir.to_expr_noopts()) + }); + imported + }) } } fn save_expr(file_path: &Path, expr: &Expr) -> Result<(), Error> { - File::create(file_path)? - .write_all(binary::encode(expr)?.as_slice())?; + File::create(file_path)?.write_all(binary::encode(expr)?.as_slice())?; Ok(()) } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 264b355..3b72b9e 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -218,37 +218,38 @@ fn resolve_one_import( let location = location.chain(&import.location, do_sanity_check)?; env.handle_import(location.clone(), |env| match import.mode { ImportMode::Code => { - let (hir, ty) = cache.caching_import( - import, - || location.fetch_dhall(), - |parsed| { - let typed = resolve_with_env(env, cache, parsed)?.typecheck()?; - let hir = typed.normalize().to_hir(); - Ok((hir, typed.ty)) - } - )?; - match &import.hash { - Some(Hash::SHA256(hash)) => { - let actual_hash = hir.to_expr_alpha().hash()?; - if hash[..] != actual_hash[..] { - mkerr( - ErrorBuilder::new("hash mismatch") - .span_err(span, "hash mismatch") - .note(format!( - "Expected sha256:{}", - hex::encode(hash) - )) - .note(format!( - "Found sha256:{}", - hex::encode(actual_hash) - )) - .format(), - )? - } + let (hir, ty) = cache.caching_import( + import, + || location.fetch_dhall(), + |parsed| { + let typed = + resolve_with_env(env, cache, parsed)?.typecheck()?; + let hir = typed.normalize().to_hir(); + Ok((hir, typed.ty)) + }, + )?; + match &import.hash { + Some(Hash::SHA256(hash)) => { + let actual_hash = hir.to_expr_alpha().hash()?; + if hash[..] != actual_hash[..] { + mkerr( + ErrorBuilder::new("hash mismatch") + .span_err(span, "hash mismatch") + .note(format!( + "Expected sha256:{}", + hex::encode(hash) + )) + .note(format!( + "Found sha256:{}", + hex::encode(actual_hash) + )) + .format(), + )? } - None => {} } - Ok((hir, ty)) + None => {} + } + Ok((hir, ty)) } ImportMode::RawText => { let text = location.fetch_text()?; @@ -359,7 +360,9 @@ fn resolve_with_env( let resolved = traverse_resolve_expr( &mut NameEnv::new(), &expr, - &mut |import, span| resolve_one_import(env, cache, &import, &location, span), + &mut |import, span| { + resolve_one_import(env, cache, &import, &location, span) + }, )?; Ok(Resolved(resolved)) } -- cgit v1.2.3