aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authordelta <darkussdelta@gmail.com>2025-08-26 04:25:15 +0200
committerdelta <darkussdelta@gmail.com>2025-08-26 04:25:15 +0200
commit7d0c2f4617bc0119e748f59e0a6bd8da79494087 (patch)
tree9c13646ee48fe2d1293c3bcc2898443bf445e252 /src/main.rs
parentaced62ade1363ae031114302ec69dbc7f6f38a10 (diff)
refactor to use a file for code input
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs87
1 files changed, 52 insertions, 35 deletions
diff --git a/src/main.rs b/src/main.rs
index 0904b55..31bd882 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -25,7 +25,9 @@ use parsers::{
};
use tree_sitter::Language;
use tree_sitter_highlight::{
- HighlightConfiguration, Highlighter, HtmlRenderer
+ HighlightConfiguration,
+ Highlighter,
+ HtmlRenderer,
};
use url::Url;
@@ -35,22 +37,24 @@ fn get_config<'a>(
) -> Option<&'static HighlightConfiguration> {
let lang = ALIASES.get(lang).unwrap_or(&lang);
configs.borrow_mut().entry(lang).or_insert_with(|| {
- let install_info = PARSERS.get(lang).unwrap();
- let mut cache_dir = cache_dir().unwrap();
+ let install_info = PARSERS
+ .get(lang)
+ .expect("A valid language should have been passed in");
+ let mut cache_dir = cache_dir().expect("Cache directory should be available");
cache_dir.push("yah");
- let mut query_dir = current_dir().unwrap();
+ let mut query_dir = current_dir().expect("Current directory should be available");
query_dir.push("nvim-treesitter");
query_dir.push("queries");
query_dir.push(lang);
- let mut parsers_dir = current_dir().unwrap();
+ let mut parsers_dir = current_dir().expect("Current directory should be available");
parsers_dir.push(".parsers");
if !parsers_dir.exists() {
- create_dir(&parsers_dir).unwrap();
+ create_dir(&parsers_dir).expect("`parsers_dir` should be able to be created");
}
if !cache_dir.exists() {
- create_dir(&cache_dir).unwrap();
+ create_dir(&cache_dir).expect("`cache_dir` should be able to be created");
}
let parser_path = parsers_dir.join(format!("{lang}.so"));
@@ -58,7 +62,7 @@ fn get_config<'a>(
Url::parse(install_info.url)
.unwrap()
.path_segments()
- .unwrap()
+ .expect("The `url` field should contain a valid URL")
.last()
.unwrap(),
);
@@ -68,11 +72,13 @@ fn get_config<'a>(
Command::new("git")
.arg("clone")
.arg(install_info.url)
+ .arg("--depth")
+ .arg("1")
.current_dir(&cache_dir)
.spawn()
- .unwrap()
+ .expect("`git` should successfully cloned the treesitter repository")
.wait()
- .unwrap();
+ .expect("`git` should have started");
}
if install_info.requires_generate_from_grammar {
@@ -81,9 +87,9 @@ fn get_config<'a>(
.arg("install")
.current_dir(&repo_path)
.spawn()
- .unwrap()
+ .expect("`npm` should successfully installed dependencies")
.wait()
- .unwrap();
+ .expect("`npm` should have started");
}
Command::new("tree-sitter")
@@ -91,9 +97,9 @@ fn get_config<'a>(
.arg("--no-bindings")
.current_dir(&repo_path)
.spawn()
- .unwrap()
+ .expect("`tree-sitter` should successfully generated the parser")
.wait()
- .unwrap();
+ .expect("`tree-sitter` should have started");
}
Command::new("tree-sitter")
@@ -106,19 +112,20 @@ fn get_config<'a>(
.map_or(repo_path.clone(), |location| repo_path.join(location)),
)
.spawn()
- .unwrap()
+ .expect("`tree-sitter` should successfully built the parser")
.wait()
- .unwrap();
+ .expect("`tree-sitter` should have started");
}
let library = unsafe {
Library::new(parser_path)
- .unwrap()
+ .expect("`parser_path` should contain a path to a valid C dylib")
};
let parser = unsafe {
- library.get::<unsafe extern "C" fn() -> Language>(format!("tree_sitter_{lang}").as_bytes())
- .unwrap()
- ()
+ library
+ .get::<unsafe extern "C" fn() -> Language>(format!("tree_sitter_{lang}").as_bytes())
+ .expect("`parser_path` should contain a path to treesitter parser dylib ")(
+ )
};
std::mem::forget(library); // this causes the dylib to not be unloaded with dlclose after this
// scope is dropped, and thus extending its lifetime to static
@@ -147,13 +154,18 @@ fn get_config<'a>(
}
fn main() {
- if let [_, lang, code] = args().collect::<Vec<String>>().as_slice() {
+ if let [_, lang, code_path] = args().collect::<Vec<String>>().as_slice() {
+ let code = String::from_utf8(
+ read(code_path).expect("`code_path` should contain a path to a file"),
+ )
+ .expect("`code_path` should contain a path to a valid UTF-8 file");
// decode code as it may be html encoded
- let code = html_escape::decode_html_entities(code);
+ let code = html_escape::decode_html_entities(&code);
let mut highlighter = Highlighter::new();
let configs = RefCell::new(HashMap::new());
- let config = get_config(lang, &configs).unwrap();
+ let config =
+ get_config(lang, &configs).expect("A valid language should have been passed in");
let events = highlighter
.highlight(&config, code.as_bytes(), None, |v| {
@@ -163,19 +175,24 @@ fn main() {
.unwrap();
let mut renderer = HtmlRenderer::new();
- renderer.render(events, code.as_bytes(), &|highlight, output| {
- output.extend(b"class=\"");
- let mut parts = HIGHLIGHTS[highlight.0].split('.').peekable();
- while let Some(part) = parts.next() {
- output.extend(part.as_bytes());
- if parts.peek().is_some() {
- output.extend(b" ");
+ renderer
+ .render(events, code.as_bytes(), &|highlight, output| {
+ output.extend(b"class=\"");
+ let mut parts = HIGHLIGHTS[highlight.0].split('.').peekable();
+ while let Some(part) = parts.next() {
+ output.extend(part.as_bytes());
+ if parts.peek().is_some() {
+ output.extend(b" ");
+ }
}
- }
- output.extend(b"\"");
- }).unwrap();
- print!("{}", String::from_utf8(renderer.html).unwrap())
+ output.extend(b"\"");
+ })
+ .unwrap();
+ print!("{}", String::from_utf8(renderer.html).expect("`renderer.html` should contain valid UTF-8"))
} else {
- panic!("Need <lang> <code> as arguments, got {:?}", args().collect::<Vec<String>>())
+ panic!(
+ "Need <lang> <code_path> as arguments, got {:?}",
+ args().collect::<Vec<String>>()
+ )
}
}