Skip to content

Commit

Permalink
fix: error handling for grammar compilation
Browse files Browse the repository at this point in the history
  • Loading branch information
Erin van der Veen committed Aug 21, 2024
1 parent c131ff5 commit b806a53
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 17 deletions.
26 changes: 23 additions & 3 deletions topiary-config/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,18 +5,22 @@ pub type TopiaryConfigResult<T> = result::Result<T, TopiaryConfigError>;
#[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 {
Expand All @@ -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),
}
}
}
Expand Down Expand Up @@ -60,6 +66,20 @@ impl From<topiary_tree_sitter_facade::LanguageError> for TopiaryConfigError {
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<libloading::Error> for TopiaryConfigError {
fn from(e: libloading::Error) -> Self {
Self::LibLoading(e)
}
}

#[cfg(not(target_arch = "wasm32"))]
impl From<git2::Error> 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 {
Expand Down
23 changes: 9 additions & 14 deletions topiary-config/src/language.rs
Original file line number Diff line number Diff line change
Expand Up @@ -92,15 +92,13 @@ impl Language {
pub fn grammar(&self) -> TopiaryConfigResult<topiary_tree_sitter_facade::Language> {
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());
Expand All @@ -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 {
Expand All @@ -123,7 +120,7 @@ impl Language {

let language = unsafe {
let language_fn: Symbol<unsafe extern "C" fn() -> tree_sitter::Language> =
library.get(language_fn_name.as_bytes()).unwrap();
library.get(language_fn_name.as_bytes())?;
language_fn()
};
std::mem::forget(library);
Expand Down Expand Up @@ -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),
Expand Down Expand Up @@ -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");
Expand All @@ -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!(
Expand Down

0 comments on commit b806a53

Please sign in to comment.