diff options
-rw-r--r-- | dhall/src/lib.rs | 5 | ||||
-rw-r--r-- | dhall/src/semantics/parse.rs | 7 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/cache.rs | 658 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/env.rs | 32 | ||||
-rw-r--r-- | dhall/src/semantics/resolve/resolve.rs | 86 | ||||
-rw-r--r-- | dhall/src/syntax/ast/expr.rs | 5 | ||||
-rw-r--r-- | dhall/src/syntax/ast/import.rs | 2 | ||||
-rw-r--r-- | dhall/src/syntax/binary/decode.rs | 2 | ||||
-rw-r--r-- | dhall/src/syntax/text/parser.rs | 2 | ||||
-rw-r--r-- | dhall/src/utils.rs | 17 | ||||
-rw-r--r-- | dhall/tests/spec.rs | 2 |
11 files changed, 156 insertions, 662 deletions
diff --git a/dhall/src/lib.rs b/dhall/src/lib.rs index 87c461b..f9d259c 100644 --- a/dhall/src/lib.rs +++ b/dhall/src/lib.rs @@ -13,6 +13,7 @@ pub mod error; pub mod operations; pub mod semantics; pub mod syntax; +pub mod utils; use std::fmt::Display; use std::path::Path; @@ -37,8 +38,8 @@ pub struct Resolved(Hir); /// A typed expression #[derive(Debug, Clone)] pub struct Typed { - hir: Hir, - ty: Type, + pub hir: Hir, + pub ty: Type, } /// A normalized expression. diff --git a/dhall/src/semantics/parse.rs b/dhall/src/semantics/parse.rs index 82396e0..a770c15 100644 --- a/dhall/src/semantics/parse.rs +++ b/dhall/src/semantics/parse.rs @@ -1,5 +1,3 @@ -use std::fs::File; -use std::io::Read; use std::path::Path; use url::Url; @@ -36,9 +34,8 @@ pub fn parse_binary(data: &[u8]) -> Result<Parsed, Error> { } 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 = binary::decode(&buffer)?; + let data = crate::utils::read_binary_file(f)?; + let expr = binary::decode(&data)?; let root = ImportLocation::Local(f.to_owned()); Ok(Parsed(expr, root)) } diff --git a/dhall/src/semantics/resolve/cache.rs b/dhall/src/semantics/resolve/cache.rs index dd686ea..2abe7a9 100644 --- a/dhall/src/semantics/resolve/cache.rs +++ b/dhall/src/semantics/resolve/cache.rs @@ -2,13 +2,10 @@ use std::env; use std::io::Write; use std::path::{Path, PathBuf}; -use crate::error::{CacheError, Error, ErrorKind}; -use crate::parse::parse_binary_file; -use crate::semantics::{Import, TypedHir}; -use crate::syntax::Hash; -use crate::syntax::{binary, Expr}; -use crate::Parsed; -use std::env::VarError; +use crate::error::{CacheError, Error}; +use crate::parse::parse_binary; +use crate::semantics::TypedHir; +use crate::syntax::{binary, Hash}; use std::ffi::OsStr; use std::fs::File; @@ -20,634 +17,99 @@ const ALTERNATE_CACHE_ENV_VAR: &str = "HOME"; const ALTERNATE_CACHE_ENV_VAR: &str = "LOCALAPPDATA"; #[cfg(any(unix, windows))] -fn load_cache_dir( - env_provider: impl Fn(&str) -> Result<String, VarError> + Copy, -) -> Result<PathBuf, CacheError> { - let env_provider = |s| { - env_provider(s) - .map(PathBuf::from) - .map_err(|_| CacheError::MissingConfiguration) +fn default_cache_dir() -> Result<PathBuf, CacheError> { + let cache_base_path = match env::var(OsStr::new(CACHE_ENV_VAR)) { + Ok(path) => PathBuf::from(path), + Err(_) => match env::var(OsStr::new(ALTERNATE_CACHE_ENV_VAR)) { + Ok(path) => PathBuf::from(path).join(".cache"), + Err(_) => return Err(CacheError::MissingConfiguration), + }, }; - let cache_base_path = env_provider(CACHE_ENV_VAR).or_else(|_| { - env_provider(ALTERNATE_CACHE_ENV_VAR).map(|path| path.join(".cache")) - })?; Ok(cache_base_path.join("dhall")) } + #[cfg(not(any(unix, windows)))] -fn load_cache_dir( - _provider: impl Fn(&str) -> Result<String, VarError> + Copy, -) -> Result<PathBuf, CacheError> { +fn default_cache_dir() -> Result<PathBuf, CacheError> { Err(CacheError::MissingConfiguration) } #[derive(Debug, Clone, PartialEq)] pub struct Cache { - cache_dir: Option<PathBuf>, + cache_dir: PathBuf, } impl Cache { - fn new_with_provider( - provider: impl Fn(&str) -> Result<String, VarError> + Copy, - ) -> Cache { - // Should warn that we can't initialize cache on error - let cache_dir = load_cache_dir(provider).and_then(|path| { - if !path.exists() { - std::fs::create_dir_all(path.as_path()) - .map(|_| path) - .map_err(|e| CacheError::InitialisationError { cause: e }) - } else { - Ok(path) - } - }); - Cache { - cache_dir: cache_dir.ok(), + pub fn new() -> Result<Cache, Error> { + let cache_dir = default_cache_dir()?; + if !cache_dir.exists() { + std::fs::create_dir_all(&cache_dir) + .map_err(|e| CacheError::InitialisationError { cause: e })?; } + Ok(Cache { cache_dir }) } - pub fn new() -> Cache { - Cache::new_with_provider(|name| env::var(OsStr::new(name))) - } -} - -impl Cache { - fn cache_file(&self, import: &Import) -> Option<PathBuf> { - 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))) + fn entry_path(&self, hash: &Hash) -> PathBuf { + self.cache_dir.join(filename_for_hash(hash)) } - fn search_cache_file(&self, import: &Import) -> Option<PathBuf> { - self.cache_file(import) - .filter(|cache_file| cache_file.exists()) + pub fn get(&self, hash: &Hash) -> Result<TypedHir, Error> { + let path = self.entry_path(hash); + let res = read_cache_file(&path, hash); + if let Err(_) = res { + if path.exists() { + // Delete cache file since it's invalid. We ignore the error. + let _ = std::fs::remove_file(&path); + } + } + res } - fn search_cache(&self, import: &Import) -> Option<Result<Parsed, Error>> { - self.search_cache_file(import) - .map(|cache_file| parse_binary_file(cache_file.as_path())) + pub fn insert(&self, hash: &Hash, expr: &TypedHir) -> Result<(), Error> { + let path = self.entry_path(hash); + write_cache_file(&path, expr) } +} - // Side effect since we don't use the result - fn delete_cache(&self, import: &Import) { - self.search_cache_file(import) - .map(|cache_file| std::fs::remove_file(cache_file.as_path())); - } +/// Read a file from the cache, also checking that its hash is valid. +fn read_cache_file(path: &Path, hash: &Hash) -> Result<TypedHir, Error> { + let data = crate::utils::read_binary_file(path)?; - // 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)); + match hash { + Hash::SHA256(hash) => { + let actual_hash = crate::utils::sha256_hash(&data); + if hash[..] != actual_hash[..] { + return Err(CacheError::CacheHashInvalid.into()); + } + } } - pub fn caching_import<F, R>( - &self, - import: &Import, - fetcher: F, - mut resolver: R, - ) -> Result<TypedHir, Error> - where - F: FnOnce() -> Result<Parsed, Error>, - R: FnMut(Parsed) -> Result<TypedHir, Error>, - { - // 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) - }, - ) { - // Cache content is invalid (can't be parsed / can't be resolved / content sha invalid ) - Err(_) => { - // Delete cache file since it's invalid - self.delete_cache(import); - // Result as there were no cache - None - } - // Cache valid - 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 - }) - } + let crate::Typed { hir, ty } = + parse_binary(&data)?.skip_resolve()?.typecheck()?; + Ok((hir, ty)) } -fn save_expr(file_path: &Path, expr: &Expr) -> Result<(), Error> { - File::create(file_path)?.write_all(binary::encode(expr)?.as_slice())?; +/// Write a file to the cache. +fn write_cache_file(path: &Path, expr: &TypedHir) -> Result<(), Error> { + let data = binary::encode(&expr.0.to_expr_noopts())?; + File::create(path)?.write_all(data.as_slice())?; Ok(()) } -fn check_hash(hash: &Hash, typed_hir: TypedHir) -> Result<TypedHir, Error> { - if hash.as_ref()[..] != typed_hir.0.to_expr_alpha().hash()?[..] { - Err(Error::new(ErrorKind::Cache(CacheError::CacheHashInvalid))) - } else { - Ok(typed_hir) - } -} - -fn cache_filename<A: AsRef<[u8]>>(v: A) -> String { - format!("1220{}", hex::encode(v.as_ref())) -} - -impl AsRef<[u8]> for Hash { - fn as_ref(&self) -> &[u8] { - match self { - Hash::SHA256(sha) => sha.as_slice(), - } +fn filename_for_hash(hash: &Hash) -> String { + match hash { + Hash::SHA256(sha) => format!("1220{}", hex::encode(&sha)), } } #[cfg(test)] mod test { use super::*; - use crate::semantics::parse::parse_str; - use crate::syntax::{ - parse_expr, ExprKind, ImportMode, ImportTarget, NumKind, Span, - }; - use rand::distributions::Alphanumeric; - use rand::Rng; - use std::env::temp_dir; - - #[cfg(any(unix, windows))] - #[test] - fn load_cache_dir_should_result_xdg_cache_first() { - let actual = load_cache_dir(|var| match var { - CACHE_ENV_VAR => Ok("/home/user/custom".to_string()), - _ => Err(VarError::NotPresent), - }); - assert_eq!(actual.unwrap(), PathBuf::from("/home/user/custom/dhall")); - } - - #[cfg(unix)] - #[test] - fn load_cache_dir_should_result_alternate() { - let actual = load_cache_dir(|var| match var { - ALTERNATE_CACHE_ENV_VAR => Ok("/home/user".to_string()), - _ => Err(VarError::NotPresent), - }); - assert_eq!(actual.unwrap(), PathBuf::from("/home/user/.cache/dhall")); - } - - #[test] - fn load_cache_dir_should_result_none() { - let actual = load_cache_dir(|_| Err(VarError::NotPresent)); - assert!(matches!( - actual.unwrap_err(), - CacheError::MissingConfiguration - )); - } - - #[test] - fn new_with_provider_should_create_cache_folder() { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path()).unwrap(); - - let actual = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - assert_eq!( - actual, - Cache { - cache_dir: Some(dir.join("dhall")) - } - ); - assert!(dir.join("dhall").exists()); - std::fs::remove_dir_all(dir.as_path()).unwrap(); - } - - #[test] - fn new_with_provider_should_return_cache_for_existing_folder() { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path()).unwrap(); - File::create(dir.join("dhall")).unwrap(); - - assert!(dir.join("dhall").exists()); - - let actual = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - assert_eq!( - actual, - Cache { - cache_dir: Some(dir.join("dhall")) - } - ); - std::fs::remove_dir_all(dir.as_path()).unwrap(); - } - - #[test] - fn caching_import_should_load_cache() -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - - // Create cache file - let expr = - Expr::new(ExprKind::Num(NumKind::Natural(1)), Span::Artificial); - File::create(dir.join("dhall").join("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15"))? - .write_all(binary::encode(&expr)?.as_ref())?; - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || panic!("Should not fetch import"), - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok()); - assert_eq!(resolve_counter, 1); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } - - #[test] - fn caching_import_should_skip_cache_if_missing_cache_folder( - ) -> Result<(), Error> { - let cache = Cache::new_with_provider(|_| Err(VarError::NotPresent)); - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut resolve_counter = 0; - let mut fetcher_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(resolve_counter, 1); - assert_eq!(fetcher_counter, 1); - Ok(()) - } - - #[test] - fn caching_import_should_skip_cache_on_no_hash_import() -> Result<(), Error> - { - let cache = Cache::new_with_provider(|_| Err(VarError::NotPresent)); - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: None, - }; - - let mut resolve_counter = 0; - let mut fetcher_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(resolve_counter, 1); - assert_eq!(fetcher_counter, 1); - Ok(()) - } - - #[test] - fn caching_import_should_fetch_import_if_no_cache() -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut fetcher_counter = 0; - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(resolve_counter, 1); - assert_eq!(fetcher_counter, 1); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } - - #[test] - fn caching_import_should_fetch_import_on_cache_parsed_error( - ) -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - - File::create(dir.join("dhall").join("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15"))? - .write_all("Invalid content".as_bytes())?; - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut fetcher_counter = 0; - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(fetcher_counter, 1, "Should fetch since cache is invalid"); - assert_eq!( - resolve_counter, 1, - "Should resolve only 1 time because cache can't be parsed" - ); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } - - #[test] - fn caching_import_should_fetch_import_on_cache_resolve_error( - ) -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - - let expr = - Expr::new(ExprKind::Num(NumKind::Natural(2)), Span::Artificial); - File::create(dir.join("dhall").join("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15"))? - .write_all(binary::encode(&expr)?.as_slice())?; - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut fetcher_counter = 0; - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - match resolve_counter { - 1 => Err(Error::new(ErrorKind::Cache( - CacheError::CacheHashInvalid, - ))), - _ => { - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - } - } - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(fetcher_counter, 1, "Should fetch since cache is invalid"); - assert_eq!( - resolve_counter, 2, - "Should resolve 2 time (one for cache that fail, one for fetch)" - ); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } - - #[test] - fn caching_import_should_fetch_import_on_invalid_hash_cache_content( - ) -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - - let expr = - Expr::new(ExprKind::Num(NumKind::Natural(2)), Span::Artificial); - File::create(dir.join("dhall").join("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15"))? - .write_all(binary::encode(&expr)?.as_slice())?; - - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut fetcher_counter = 0; - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(fetcher_counter, 1, "Should fetch since cache is invalid"); - assert_eq!( - resolve_counter, 2, - "Should resolve 2 time (one for cache, one for fetch)" - ); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } - - #[test] - fn caching_import_should_save_import_if_missing() -> Result<(), Error> { - let test_id = rand::thread_rng() - .sample_iter(Alphanumeric) - .take(36) - .collect::<String>(); - let dir = temp_dir().join(test_id); - - std::fs::create_dir_all(dir.as_path())?; - - let cache = Cache::new_with_provider(|_| { - Ok(dir.clone().to_str().map(String::from).unwrap()) - }); - let import = Import { - mode: ImportMode::Code, - location: ImportTarget::Missing, - hash: Some(Hash::SHA256(hex::decode("d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15").unwrap())), - }; - - let mut fetcher_counter = 0; - let mut resolve_counter = 0; - - let result = cache.caching_import( - &import, - || { - fetcher_counter += 1; - parse_str("1") - }, - |parsed| { - resolve_counter += 1; - let result = parsed.resolve()?.typecheck()?; - Ok((result.normalize().to_hir(), result.ty)) - }, - ); - - assert!(result.is_ok(), "caching_import Should be valid"); - assert_eq!(fetcher_counter, 1, "Should fetch since cache is mising"); - assert_eq!(resolve_counter, 1, "Should resolve 1 time"); - - let cache_file = dir.join("dhall").join("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15"); - assert!(cache_file.exists()); - - std::fs::remove_dir_all(dir.as_path()).unwrap(); - Ok(()) - } + use crate::syntax::parse_expr; #[test] - fn cache_filename_should_result_for_hash() { + fn filename_for_hash_should_work() { let hash = - Hash::SHA256(parse_expr("1").unwrap().hash().unwrap().into_vec()); - assert_eq!("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15".to_string(), cache_filename(hash)); - } - - #[test] - fn check_hash_should_be_ok_for_same_hash() -> Result<(), Error> { - let typed = parse_str("1")?.resolve()?.typecheck()?; - let hash = Hash::SHA256(parse_expr("1")?.hash()?.into_vec()); - - let expected = (typed.normalize().to_hir(), typed.ty); - let actual = check_hash(&hash, expected.clone()); - assert_eq!(actual.unwrap(), expected); - Ok(()) - } - - #[test] - fn check_hash_should_be_ok_for_unmatching_hash() -> Result<(), Error> { - let typed = parse_str("1")?.resolve()?.typecheck()?; - let hash = Hash::SHA256(parse_expr("2")?.hash()?.into_vec()); - - let expected = (typed.normalize().to_hir(), typed.ty); - let actual = check_hash(&hash, expected); - assert!(actual.is_err()); - Ok(()) + Hash::SHA256(parse_expr("1").unwrap().sha256_hash().unwrap()); + assert_eq!("1220d60d8415e36e86dae7f42933d3b0c4fe3ca238f057fba206c7e9fbf5d784fe15".to_string(), filename_for_hash(&hash)); } } diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 4d502ce..06e27a5 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -2,7 +2,7 @@ use std::collections::HashMap; use crate::error::{Error, ImportError}; use crate::semantics::{AlphaVar, Cache, ImportLocation, TypedHir, VarEnv}; -use crate::syntax::{Label, V}; +use crate::syntax::{Hash, Label, V}; /// Environment for resolving names. #[derive(Debug, Clone, Default)] @@ -16,7 +16,7 @@ pub type CyclesStack = Vec<ImportLocation>; /// Environment for resolving imports #[derive(Debug)] pub struct ImportEnv { - pub file_cache: Cache, + file_cache: Option<Cache>, mem_cache: MemCache, stack: CyclesStack, } @@ -68,20 +68,36 @@ impl NameEnv { impl ImportEnv { pub fn new() -> Self { ImportEnv { - file_cache: Cache::new(), + file_cache: Cache::new().ok(), mem_cache: Default::default(), stack: Default::default(), } } - pub fn get_from_cache<'a>( - &'a self, + pub fn get_from_cache( + &mut self, location: &ImportLocation, - ) -> Option<&'a TypedHir> { - self.mem_cache.get(location) + hash: Option<&Hash>, + ) -> Option<TypedHir> { + if let Some(expr) = self.mem_cache.get(location) { + return Some(expr.clone()); + } + let hash = hash.as_ref()?; + let expr = self.file_cache.as_ref()?.get(hash).ok()?; + Some(expr) } - pub fn set_cache(&mut self, location: ImportLocation, expr: TypedHir) { + pub fn set_cache( + &mut self, + location: ImportLocation, + hash: Option<&Hash>, + expr: TypedHir, + ) { + if let Some(file_cache) = self.file_cache.as_ref() { + if let Some(hash) = hash { + let _ = file_cache.insert(hash, &expr); + } + } self.mem_cache.insert(location, expr); } diff --git a/dhall/src/semantics/resolve/resolve.rs b/dhall/src/semantics/resolve/resolve.rs index 8f8a9fe..d6bc4cd 100644 --- a/dhall/src/semantics/resolve/resolve.rs +++ b/dhall/src/semantics/resolve/resolve.rs @@ -220,6 +220,32 @@ fn make_aslocation_uniontype() -> Expr { mkexpr(ExprKind::UnionType(union)) } +fn check_hash( + import: &Import, + typed: &TypedHir, + span: Span, +) -> Result<(), Error> { + match (import.mode, &import.hash) { + (ImportMode::Code, Some(Hash::SHA256(hash))) => { + let actual_hash = typed.0.to_expr_alpha().sha256_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(), + )? + } + } + _ => {} + } + Ok(()) +} + fn resolve_one_import( env: &mut ImportEnv, import: &Import, @@ -228,44 +254,15 @@ fn resolve_one_import( ) -> Result<TypedHir, Error> { match import.mode { ImportMode::Code => { - let (hir, ty) = env.file_cache.clone().caching_import( - import, - || location.fetch_dhall(), - |parsed| { - let typed = resolve_with_env(env, 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)) + let parsed = location.fetch_dhall()?; + let typed = resolve_with_env(env, parsed)?.typecheck()?; + let hir = typed.normalize().to_hir(); + Ok((hir, typed.ty)) } ImportMode::RawText => { let text = location.fetch_text()?; - let hir = Hir::new( - HirKind::Expr(ExprKind::TextLit(text.into())), - Span::Artificial, - ); + let hir = + Hir::new(HirKind::Expr(ExprKind::TextLit(text.into())), span); Ok((hir, Type::from_builtin(Builtin::Text))) } ImportMode::Location => { @@ -399,15 +396,20 @@ fn resolve_with_env( let do_sanity_check = import.mode != ImportMode::Location; let location = base_location.chain(&import.location, do_sanity_check)?; - if let Some(expr) = env.get_from_cache(&location) { - return Ok(expr.clone()); + // If the import is in the in-memory cache, or the hash is in the on-disk cache, return + // the cached contents. + if let Some(resolved) = + env.get_from_cache(&location, import.hash.as_ref()) + { + return Ok(resolved); } - let expr = env.with_cycle_detection(location.clone(), |env| { - resolve_one_import(env, &import, location.clone(), span) + let typed = env.with_cycle_detection(location.clone(), |env| { + resolve_one_import(env, &import, location.clone(), span.clone()) })?; - // Add the resolved import to the cache - env.set_cache(location, expr.clone()); - Ok(expr) + check_hash(&import, &typed, span)?; + // Add the resolved import to the caches + env.set_cache(location, import.hash.as_ref(), typed.clone()); + Ok(typed) }, )?; Ok(Resolved(resolved)) diff --git a/dhall/src/syntax/ast/expr.rs b/dhall/src/syntax/ast/expr.rs index b1a978f..eba2735 100644 --- a/dhall/src/syntax/ast/expr.rs +++ b/dhall/src/syntax/ast/expr.rs @@ -178,10 +178,9 @@ impl Expr { } // Compute the sha256 hash of the binary form of the expression. - pub fn hash(&self) -> Result<Box<[u8]>, Error> { - use sha2::Digest; + pub fn sha256_hash(&self) -> Result<Box<[u8]>, Error> { let data = binary::encode(self)?; - Ok(sha2::Sha256::digest(&data).as_slice().into()) + Ok(crate::utils::sha256_hash(&data)) } } diff --git a/dhall/src/syntax/ast/import.rs b/dhall/src/syntax/ast/import.rs index c45fe51..69f4021 100644 --- a/dhall/src/syntax/ast/import.rs +++ b/dhall/src/syntax/ast/import.rs @@ -52,7 +52,7 @@ pub enum ImportMode { #[derive(Debug, Clone, PartialEq, Eq, Hash)] pub enum Hash { - SHA256(Vec<u8>), + SHA256(Box<[u8]>), } /// Reference to an external resource diff --git a/dhall/src/syntax/binary/decode.rs b/dhall/src/syntax/binary/decode.rs index f4747d3..4ea7d98 100644 --- a/dhall/src/syntax/binary/decode.rs +++ b/dhall/src/syntax/binary/decode.rs @@ -290,7 +290,7 @@ fn cbor_value_to_dhall(data: &Value) -> Result<DecodedExpr, DecodeError> { Null => None, Bytes(bytes) => match bytes.as_slice() { [18, 32, rest @ ..] => { - Some(Hash::SHA256(rest.to_vec())) + Some(Hash::SHA256(rest.to_vec().into())) } _ => { return Err(DecodeError::WrongFormatError(format!( diff --git a/dhall/src/syntax/text/parser.rs b/dhall/src/syntax/text/parser.rs index 37f28e5..07921b5 100644 --- a/dhall/src/syntax/text/parser.rs +++ b/dhall/src/syntax/text/parser.rs @@ -593,7 +593,7 @@ impl DhallParser { input.error(format!("Unknown hashing protocol '{}'", protocol)) ); } - Ok(Hash::SHA256(hex::decode(hash).unwrap())) + Ok(Hash::SHA256(hex::decode(hash).unwrap().into())) } fn import_hashed( diff --git a/dhall/src/utils.rs b/dhall/src/utils.rs new file mode 100644 index 0000000..d1e642a --- /dev/null +++ b/dhall/src/utils.rs @@ -0,0 +1,17 @@ +use std::fs::File; +use std::io::Read; +use std::path::Path; + +use crate::error::Error; + +// Compute the sha256 hash of a bitstring. +pub fn sha256_hash(data: &[u8]) -> Box<[u8]> { + use sha2::Digest; + sha2::Sha256::digest(data).as_slice().into() +} + +pub fn read_binary_file(path: impl AsRef<Path>) -> Result<Box<[u8]>, Error> { + let mut buffer = Vec::new(); + File::open(path)?.read_to_end(&mut buffer)?; + Ok(buffer.into()) +} diff --git a/dhall/tests/spec.rs b/dhall/tests/spec.rs index 8d67892..ae53f4a 100644 --- a/dhall/tests/spec.rs +++ b/dhall/tests/spec.rs @@ -670,7 +670,7 @@ fn run_test(test: &SpecTest) -> Result<()> { } SemanticHash => { let expr = expr.normalize()?.to_expr_alpha(); - let hash = hex::encode(expr.hash()?); + let hash = hex::encode(expr.sha256_hash()?); expected.compare_ui(format!("sha256:{}", hash))?; } TypeInferenceSuccess => { |