|
| 1 | +use crate::{DynamicLang, DynamicLangError, Registration}; |
| 2 | +use serde::{Deserialize, Serialize}; |
| 3 | + |
| 4 | +use std::collections::HashMap; |
| 5 | +use std::path::{Path, PathBuf}; |
| 6 | + |
| 7 | +#[derive(Serialize, Deserialize, Clone)] |
| 8 | +#[serde(untagged)] |
| 9 | +pub enum LibraryPath { |
| 10 | + Single(PathBuf), |
| 11 | + Platform(HashMap<String, PathBuf>), |
| 12 | +} |
| 13 | + |
| 14 | +#[derive(Serialize, Deserialize, Clone)] |
| 15 | +#[serde(rename_all = "camelCase")] |
| 16 | +pub struct CustomLang { |
| 17 | + pub library_path: LibraryPath, |
| 18 | + /// the dylib symbol to load ts-language, default is `tree_sitter_{name}` |
| 19 | + pub language_symbol: Option<String>, |
| 20 | + pub meta_var_char: Option<char>, |
| 21 | + pub expando_char: Option<char>, |
| 22 | + pub extensions: Vec<String>, |
| 23 | +} |
| 24 | + |
| 25 | +impl CustomLang { |
| 26 | + pub fn register( |
| 27 | + base: &Path, |
| 28 | + langs: HashMap<String, CustomLang>, |
| 29 | + ) -> Result<(), DynamicLangError> { |
| 30 | + let registrations: Result<_, _> = langs |
| 31 | + .into_iter() |
| 32 | + .map(|(name, custom)| to_registration(name, custom, base)) |
| 33 | + .collect(); |
| 34 | + unsafe { DynamicLang::register(registrations?) } |
| 35 | + } |
| 36 | +} |
| 37 | + |
| 38 | +fn to_registration( |
| 39 | + name: String, |
| 40 | + custom_lang: CustomLang, |
| 41 | + base: &Path, |
| 42 | +) -> Result<Registration, DynamicLangError> { |
| 43 | + let lib_path = match custom_lang.library_path { |
| 44 | + LibraryPath::Single(path) => path, |
| 45 | + LibraryPath::Platform(mut map) => { |
| 46 | + let target = target_triple::TARGET; |
| 47 | + map.remove(target) |
| 48 | + .ok_or(DynamicLangError::NotConfigured(target))? |
| 49 | + } |
| 50 | + }; |
| 51 | + let path = base.join(lib_path); |
| 52 | + let sym = custom_lang |
| 53 | + .language_symbol |
| 54 | + .unwrap_or_else(|| format!("tree_sitter_{name}")); |
| 55 | + Ok(Registration { |
| 56 | + lang_name: name, |
| 57 | + lib_path: path, |
| 58 | + symbol: sym, |
| 59 | + meta_var_char: custom_lang.meta_var_char, |
| 60 | + expando_char: custom_lang.expando_char, |
| 61 | + extensions: custom_lang.extensions, |
| 62 | + }) |
| 63 | +} |
| 64 | + |
| 65 | +#[cfg(test)] |
| 66 | +mod test { |
| 67 | + use super::*; |
| 68 | + use serde_yaml::from_str; |
| 69 | + |
| 70 | + #[test] |
| 71 | + fn test_custom_lang() { |
| 72 | + let yaml = r" |
| 73 | +libraryPath: a/b/c.so |
| 74 | +extensions: [d, e, f]"; |
| 75 | + let cus: CustomLang = from_str(yaml).unwrap(); |
| 76 | + assert_eq!(cus.language_symbol, None); |
| 77 | + assert_eq!(cus.extensions, vec!["d", "e", "f"]); |
| 78 | + } |
| 79 | + fn is_test_supported() -> bool { |
| 80 | + cfg!(all(target_os = "macos", target_arch = "aarch64")) |
| 81 | + || cfg!(all(target_os = "linux", target_arch = "x86_64")) |
| 82 | + } |
| 83 | + |
| 84 | + #[test] |
| 85 | + fn test_custom_lang_platform() { |
| 86 | + if !is_test_supported() { |
| 87 | + return; |
| 88 | + } |
| 89 | + let yaml = r" |
| 90 | +libraryPath: |
| 91 | + aarch64-apple-darwin: a/b/c.so |
| 92 | + x86_64-unknown-linux-gnu: a/b/c.so |
| 93 | +extensions: [d, e, f]"; |
| 94 | + let cus: CustomLang = from_str(yaml).unwrap(); |
| 95 | + assert_eq!(cus.language_symbol, None); |
| 96 | + assert_eq!(cus.extensions, vec!["d", "e", "f"]); |
| 97 | + let registration = to_registration("test_lang".to_string(), cus, Path::new(".")).unwrap(); |
| 98 | + assert_eq!(registration.lang_name, "test_lang"); |
| 99 | + assert_eq!(registration.lib_path.to_str(), Some("./a/b/c.so")); |
| 100 | + } |
| 101 | + |
| 102 | + #[test] |
| 103 | + fn test_unsupport_platform() { |
| 104 | + let yaml = r" |
| 105 | +libraryPath: |
| 106 | + impossible-platform: a/b/c.so |
| 107 | +extensions: [d, e, f]"; |
| 108 | + let cus: CustomLang = from_str(yaml).unwrap(); |
| 109 | + let reg = to_registration("test_lang".to_string(), cus, Path::new(".")); |
| 110 | + assert!(matches!( |
| 111 | + reg, |
| 112 | + Err(DynamicLangError::NotConfigured(target_triple::TARGET)) |
| 113 | + )); |
| 114 | + } |
| 115 | +} |
0 commit comments