| 1 | use std::ffi::c_void; |
| 2 | |
| 3 | use anyhow::{Context, Result}; |
| 4 | use libloading::Symbol; |
| 5 | |
| 6 | use crate::collect_grammars; |
| 7 | use crate::flags::LoadGrammar; |
| 8 | |
| 9 | impl LoadGrammar { |
| 10 | pub fn run(self) -> Result<()> { |
| 11 | let paths = if self.recursive { |
| 12 | collect_grammars(&self.path)? |
| 13 | } else { |
| 14 | vec![self.path.clone()] |
| 15 | }; |
| 16 | for path in paths { |
| 17 | let Some(name) = path.file_stem().unwrap().to_str() else { |
| 18 | continue; |
| 19 | }; |
| 20 | println!("loading {}", path.display()); |
| 21 | unsafe { |
| 22 | let lib = libloading::Library::new(&path) |
| 23 | .with_context(|| format!("failed to load {}", path.display()))?; |
| 24 | let language_fn_name = format!("tree_sitter_{}", name.replace('-', "_")); |
| 25 | let _language_fn: Symbol<unsafe extern "C" fn() -> *mut c_void> = lib |
| 26 | .get(language_fn_name.as_bytes()) |
| 27 | .with_context(|| format!("failed to load {}", path.display()))?; |
| 28 | } |
| 29 | } |
| 30 | Ok(()) |
| 31 | } |
| 32 | } |