diff --git a/cli/src/generate/binding_files.rs b/cli/src/generate/binding_files.rs index fd118059..f401510a 100644 --- a/cli/src/generate/binding_files.rs +++ b/cli/src/generate/binding_files.rs @@ -1,6 +1,6 @@ use super::write_file; use anyhow::{Context, Result}; -use std::path::{Path, PathBuf}; +use std::path::Path; use std::{fs, str}; const BINDING_CC_TEMPLATE: &str = include_str!("./templates/binding.cc"); @@ -24,7 +24,7 @@ pub fn generate_binding_files(repo_path: &Path, language_name: &str) -> Result<( // Generate rust bindings if needed. let rust_binding_dir = bindings_dir.join("rust"); - create_path(&rust_binding_dir, |path| create_dir(path))?; + create_path(&rust_binding_dir, create_dir)?; create_path(&rust_binding_dir.join("lib.rs"), |path| { generate_file(path, LIB_RS_TEMPLATE, language_name) @@ -40,7 +40,7 @@ pub fn generate_binding_files(repo_path: &Path, language_name: &str) -> Result<( // Generate node bindings let node_binding_dir = bindings_dir.join("node"); - create_path(&node_binding_dir, |path| create_dir(path))?; + create_path(&node_binding_dir, create_dir)?; create_path(&node_binding_dir.join("index.js"), |path| { generate_file(path, INDEX_JS_TEMPLATE, language_name) @@ -128,9 +128,9 @@ fn create_dir(path: &Path) -> Result<()> { .with_context(|| format!("Failed to create {:?}", path.to_string_lossy())) } -fn create_path(path: &PathBuf, action: F) -> Result +fn create_path(path: &Path, action: F) -> Result where - F: Fn(&PathBuf) -> Result<()>, + F: Fn(&Path) -> Result<()>, { if !path.exists() { action(path)?; @@ -139,10 +139,10 @@ where Ok(false) } -fn create_path_else(path: &PathBuf, action: T, else_action: F) -> Result +fn create_path_else(path: &Path, action: T, else_action: F) -> Result where - T: Fn(&PathBuf) -> Result<()>, - F: Fn(&PathBuf) -> Result<()>, + T: Fn(&Path) -> Result<()>, + F: Fn(&Path) -> Result<()>, { if !path.exists() { action(path)?; diff --git a/cli/src/playground.rs b/cli/src/playground.rs index 1e053ee2..34da71ad 100644 --- a/cli/src/playground.rs +++ b/cli/src/playground.rs @@ -12,7 +12,7 @@ use tiny_http::{Header, Response, Server}; macro_rules! optional_resource { ($name: tt, $path: tt) => { #[cfg(TREE_SITTER_EMBED_WASM_BINDING)] - fn $name(tree_sitter_dir: Option<&PathBuf>) -> Cow<'static, [u8]> { + fn $name(tree_sitter_dir: Option<&Path>) -> Cow<'static, [u8]> { if let Some(tree_sitter_dir) = tree_sitter_dir { Cow::Owned(fs::read(tree_sitter_dir.join($path)).unwrap()) } else { @@ -21,7 +21,7 @@ macro_rules! optional_resource { } #[cfg(not(TREE_SITTER_EMBED_WASM_BINDING))] - fn $name(tree_sitter_dir: Option<&PathBuf>) -> Cow<'static, [u8]> { + fn $name(tree_sitter_dir: Option<&Path>) -> Cow<'static, [u8]> { if let Some(tree_sitter_dir) = tree_sitter_dir { Cow::Owned(fs::read(tree_sitter_dir.join($path)).unwrap()) } else { @@ -35,7 +35,7 @@ optional_resource!(get_playground_js, "docs/assets/js/playground.js"); optional_resource!(get_lib_js, "lib/binding_web/tree-sitter.js"); optional_resource!(get_lib_wasm, "lib/binding_web/tree-sitter.wasm"); -fn get_main_html(tree_sitter_dir: Option<&PathBuf>) -> Cow<'static, [u8]> { +fn get_main_html(tree_sitter_dir: Option<&Path>) -> Cow<'static, [u8]> { tree_sitter_dir.map_or( Cow::Borrowed(include_bytes!("playground.html")), |tree_sitter_dir| { @@ -54,13 +54,13 @@ pub fn serve(grammar_path: &Path, open_in_browser: bool) -> Result<()> { } let tree_sitter_dir = env::var("TREE_SITTER_BASE_DIR").map(PathBuf::from).ok(); - let main_html = str::from_utf8(&get_main_html(tree_sitter_dir.as_ref())) + let main_html = str::from_utf8(&get_main_html(tree_sitter_dir.as_deref())) .unwrap() .replace("THE_LANGUAGE_NAME", &grammar_name) .into_bytes(); - let playground_js = get_playground_js(tree_sitter_dir.as_ref()); - let lib_js = get_lib_js(tree_sitter_dir.as_ref()); - let lib_wasm = get_lib_wasm(tree_sitter_dir.as_ref()); + let playground_js = get_playground_js(tree_sitter_dir.as_deref()); + let lib_js = get_lib_js(tree_sitter_dir.as_deref()); + let lib_wasm = get_lib_wasm(tree_sitter_dir.as_deref()); let html_header = Header::from_str("Content-Type: text/html").unwrap(); let js_header = Header::from_str("Content-Type: application/javascript").unwrap();