From b806a5359dc58fcf95ae75dee2f306a22ec650bb Mon Sep 17 00:00:00 2001 From: Erin van der Veen Date: Mon, 19 Aug 2024 16:20:08 +0200 Subject: [PATCH] fix: error handling for grammar compilation --- topiary-config/src/error.rs | 26 +++++++++++++++++++++++--- topiary-config/src/language.rs | 23 +++++++++-------------- 2 files changed, 32 insertions(+), 17 deletions(-) diff --git a/topiary-config/src/error.rs b/topiary-config/src/error.rs index 0e775441..d35034f7 100644 --- a/topiary-config/src/error.rs +++ b/topiary-config/src/error.rs @@ -5,18 +5,22 @@ pub type TopiaryConfigResult = result::Result; #[derive(Debug)] pub enum TopiaryConfigError { FileNotFound(path::PathBuf), - #[cfg(not(wasm))] + #[cfg(not(target_arch = "wasm32"))] UnknownLanguage(String), UnknownExtension(String), NoExtension(path::PathBuf), - #[cfg(not(wasm))] + #[cfg(not(target_arch = "wasm32"))] QueryFileNotFound(path::PathBuf), - #[cfg(not(wasm))] + #[cfg(not(target_arch = "wasm32"))] IoError(io::Error), Missing, TreeSitterFacade(topiary_tree_sitter_facade::LanguageError), Nickel(nickel_lang_core::error::Error), NickelDeserialization(nickel_lang_core::deserialize::RustDeserializationError), + #[cfg(not(target_arch = "wasm32"))] + LibLoading(libloading::Error), + #[cfg(not(target_arch = "wasm32"))] + Git(git2::Error), } impl fmt::Display for TopiaryConfigError { @@ -32,6 +36,8 @@ impl fmt::Display for TopiaryConfigError { TopiaryConfigError::TreeSitterFacade(_) => write!(f, "We could not load the grammar for the given language"), TopiaryConfigError::Nickel(e) => write!(f, "Nickel error: {:?}", e), TopiaryConfigError::NickelDeserialization(e) => write!(f, "Nickel error: {:?}", e), + TopiaryConfigError::LibLoading(e) => write!(f, "Libloading error: {:?}", e), + TopiaryConfigError::Git(e) => write!(f, "Git error: {:?}", e), } } } @@ -60,6 +66,20 @@ impl From for TopiaryConfigError { } } +#[cfg(not(target_arch = "wasm32"))] +impl From for TopiaryConfigError { + fn from(e: libloading::Error) -> Self { + Self::LibLoading(e) + } +} + +#[cfg(not(target_arch = "wasm32"))] +impl From for TopiaryConfigError { + fn from(e: git2::Error) -> Self { + Self::Git(e) + } +} + impl error::Error for TopiaryConfigError { fn source(&self) -> Option<&(dyn error::Error + 'static)> { match self { diff --git a/topiary-config/src/language.rs b/topiary-config/src/language.rs index ca95a03b..5c3fc52a 100644 --- a/topiary-config/src/language.rs +++ b/topiary-config/src/language.rs @@ -92,15 +92,13 @@ impl Language { pub fn grammar(&self) -> TopiaryConfigResult { let mut library_path = crate::project_dirs().cache_dir().to_path_buf(); if !library_path.exists() { - // TODO: Don't unwrap - std::fs::create_dir(&library_path).unwrap(); + std::fs::create_dir(&library_path)?; } library_path.push(self.name.clone()); if !library_path.exists() { - // TODO: Don't unwrap - std::fs::create_dir(&library_path).unwrap(); + std::fs::create_dir(&library_path)?; } library_path.push(self.config.grammar.rev.clone()); @@ -113,8 +111,7 @@ impl Language { use libloading::{Library, Symbol}; - // TODO: Don't unwrap - let library = unsafe { Library::new(&library_path) }.unwrap(); + let library = unsafe { Library::new(&library_path) }?; let language_fn_name = if let Some(symbol_name) = self.config.grammar.symbol.clone() { symbol_name } else { @@ -123,7 +120,7 @@ impl Language { let language = unsafe { let language_fn: Symbol tree_sitter::Language> = - library.get(language_fn_name.as_bytes()).unwrap(); + library.get(language_fn_name.as_bytes())?; language_fn() }; std::mem::forget(library); @@ -153,12 +150,10 @@ impl Language { #[cfg(not(target_arch = "wasm32"))] fn fetch_and_compile(&self, library_path: PathBuf) -> TopiaryConfigResult<()> { - // TODO: Don't unwrap - let tmp_dir = tempdir().unwrap(); + let tmp_dir = tempdir()?; - let repo = Repository::clone(&self.config.grammar.git, &tmp_dir).unwrap(); - repo.set_head_detached(Oid::from_str(&self.config.grammar.rev).unwrap()) - .unwrap(); + let repo = Repository::clone(&self.config.grammar.git, &tmp_dir)?; + repo.set_head_detached(Oid::from_str(&self.config.grammar.rev)?)?; let path = match self.config.grammar.subdir.clone() { Some(subdir) => tmp_dir.path().join(subdir), @@ -235,7 +230,7 @@ impl Language { .arg("-std=c++14") .arg("-c") .arg(scanner_path); - let output = cpp_command.output().unwrap(); + let output = cpp_command.output()?; if !output.status.success() { eprintln!("{:#?}, {:#?}", output.stdout, output.stderr); todo!("Return error"); @@ -254,7 +249,7 @@ impl Language { command.arg("-Wl,-z,relro,-z,now"); } - let output = command.output().unwrap(); + let output = command.output()?; if !output.status.success() { eprintln!(