Skip to content

Commit 363597d

Browse files
author
Erin van der Veen
committed
fix: documentation and error handling for compilation functions
1 parent 8c251b1 commit 363597d

File tree

2 files changed

+30
-6
lines changed

2 files changed

+30
-6
lines changed

topiary-config/src/error.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,8 @@ pub enum TopiaryConfigError {
1919
LibLoading(libloading::Error),
2020
#[cfg(not(target_arch = "wasm32"))]
2121
Git(git2::Error),
22+
#[cfg(not(target_arch = "wasm32"))]
23+
Compilation(String),
2224
}
2325

2426
impl fmt::Display for TopiaryConfigError {
@@ -39,6 +41,8 @@ impl fmt::Display for TopiaryConfigError {
3941
TopiaryConfigError::LibLoading(e) => write!(f, "Libloading error: {:?}", e),
4042
#[cfg(not(target_arch = "wasm32"))]
4143
TopiaryConfigError::Git(e) => write!(f, "Git error: {:?}", e),
44+
#[cfg(not(target_arch = "wasm32"))]
45+
TopiaryConfigError::Compilation(e) => write!(f, "Compilation error: {:?},", e),
4246
}
4347
}
4448
}

topiary-config/src/language.rs

Lines changed: 26 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -92,25 +92,35 @@ impl Language {
9292
// NOTE: Much of the following code is heavily inspired by the `helix-loader` crate with license MPL-2.0.
9393
// To be safe, assume any and all of the following code is MLP-2.0 and copyrighted to the Helix project.
9494
pub fn grammar(&self) -> TopiaryConfigResult<topiary_tree_sitter_facade::Language> {
95+
// Create cache dir, e.g. `~/.cache/topiary/
9596
let mut library_path = crate::project_dirs().cache_dir().to_path_buf();
9697
if !library_path.exists() {
9798
std::fs::create_dir(&library_path)?;
9899
}
99100

101+
// Create the language specific directory. This directory is not
102+
// necessary (the rev should be identifying enough), but allows
103+
// convenient removing of entire languages.
100104
library_path.push(self.name.clone());
101105

102106
if !library_path.exists() {
103107
std::fs::create_dir(&library_path)?;
104108
}
105109

110+
// Set the output path as the revision of the grammar
106111
library_path.push(self.config.grammar.rev.clone());
107-
// TODO: Windows?
112+
113+
// TODO: Windows Support
114+
// On both MacOS and Linux, .so is a valid file extension for shared objects.
108115
library_path.set_extension("so");
109116

117+
// Ensure the comile exists
110118
if !library_path.is_file() {
111119
self.fetch_and_compile(library_path.clone())?;
112120
}
113121

122+
assert!(library_path.is_file());
123+
114124
use libloading::{Library, Symbol};
115125

116126
let library = unsafe { Library::new(&library_path) }?;
@@ -152,21 +162,30 @@ impl Language {
152162

153163
#[cfg(not(target_arch = "wasm32"))]
154164
fn fetch_and_compile(&self, library_path: PathBuf) -> TopiaryConfigResult<()> {
165+
// Create a temporary directory to clone the repository to. We could
166+
// cached the repositories, but the additional disk space is probably
167+
// not worth the benefits gained by caching. The tempdir is deleted
168+
// when dropped
155169
let tmp_dir = tempdir()?;
156170

171+
// Clone the repository and checkout the configured revision
157172
let repo = Repository::clone(&self.config.grammar.git, &tmp_dir)?;
158173
repo.set_head_detached(Oid::from_str(&self.config.grammar.rev)?)?;
159174

160175
let path = match self.config.grammar.subdir.clone() {
176+
// Some grammars are in a subdirectory, go there
161177
Some(subdir) => tmp_dir.path().join(subdir),
162178
None => tmp_dir.path().to_owned(),
163179
}
180+
// parser.c and potenial scanners are always in src/
164181
.join("src");
165182

166183
self.build_tree_sitter_library(&path, library_path)
167184
}
168185

169186
#[cfg(not(target_arch = "wasm32"))]
187+
// NOTE: Much of the following code is heavily inspired by the `helix-loader` crate with license MPL-2.0.
188+
// To be safe, assume any and all of the following code is MLP-2.0 and copyrighted to the Helix project.
170189
fn build_tree_sitter_library(
171190
&self,
172191
src_path: &PathBuf,
@@ -234,8 +253,10 @@ impl Language {
234253
.arg(scanner_path);
235254
let output = cpp_command.output()?;
236255
if !output.status.success() {
237-
eprintln!("{:#?}, {:#?}", output.stdout, output.stderr);
238-
todo!("Return error");
256+
return Err(TopiaryConfigError::Compilation(format!(
257+
"{:#?}, {:#?}",
258+
output.stdout, output.stderr
259+
)));
239260
}
240261

241262
command.arg(&object_file);
@@ -254,12 +275,11 @@ impl Language {
254275
let output = command.output()?;
255276

256277
if !output.status.success() {
257-
eprintln!(
278+
return Err(TopiaryConfigError::Compilation(format!(
258279
"{:#?}, {:#?}",
259280
String::from_utf8_lossy(&output.stdout),
260281
String::from_utf8_lossy(&output.stderr),
261-
);
262-
todo!("{}", String::from_utf8_lossy(&output.stderr));
282+
)));
263283
}
264284

265285
Ok(())

0 commit comments

Comments
 (0)