From 8a2de7537986b1d60b9e8ce3bc4c08e9ec6c489a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Sun, 1 Nov 2020 14:53:38 +0000 Subject: Untangle ImportEnv::handle_import --- dhall/src/semantics/resolve/env.rs | 43 +++++++++++++++++++------------------- 1 file changed, 21 insertions(+), 22 deletions(-) (limited to 'dhall/src/semantics/resolve/env.rs') diff --git a/dhall/src/semantics/resolve/env.rs b/dhall/src/semantics/resolve/env.rs index 6346a6d..25d2277 100644 --- a/dhall/src/semantics/resolve/env.rs +++ b/dhall/src/semantics/resolve/env.rs @@ -69,9 +69,20 @@ impl ImportEnv { ImportEnv::default() } - pub fn handle_import( + pub fn get_from_cache<'a>( + &'a self, + location: &ImportLocation, + ) -> Option<&'a TypedHir> { + self.cache.get(location) + } + + pub fn set_cache(&mut self, location: ImportLocation, expr: TypedHir) { + self.cache.insert(location, expr); + } + + pub fn with_cycle_detection( &mut self, - mut location: ImportLocation, + location: ImportLocation, do_resolve: impl FnOnce(&mut Self) -> Result, ) -> Result { if self.stack.contains(&location) { @@ -79,25 +90,13 @@ impl ImportEnv { ImportError::ImportCycle(self.stack.clone(), location).into() ); } - Ok(match self.cache.get(&location) { - Some(expr) => expr.clone(), - None => { - let expr = { - // Push the current location on the stack - self.stack.push(location); - // Resolve the import recursively - // WARNING: do not propagate errors here or the stack will get messed up. - let result = do_resolve(self); - // Remove location from the stack. - location = self.stack.pop().unwrap(); - result - }?; - - // Add the resolved import to the cache - self.cache.insert(location, expr.clone()); - - expr - } - }) + // Push the current location on the stack + self.stack.push(location); + // Resolve the import recursively + // WARNING: do not propagate errors here or the stack will get messed up. + let result = do_resolve(self); + // Remove location from the stack. + self.stack.pop().unwrap(); + result } } -- cgit v1.2.3