refactor: &PathBuf -> &Path

This commit is contained in:
Amaan Qureshi 2024-02-17 00:01:10 -05:00
parent 03c5a8540d
commit 8dd65ccbc0
2 changed files with 15 additions and 15 deletions

View file

@ -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<F>(path: &PathBuf, action: F) -> Result<bool>
fn create_path<F>(path: &Path, action: F) -> Result<bool>
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<T, F>(path: &PathBuf, action: T, else_action: F) -> Result<bool>
fn create_path_else<T, F>(path: &Path, action: T, else_action: F) -> Result<bool>
where
T: Fn(&PathBuf) -> Result<()>,
F: Fn(&PathBuf) -> Result<()>,
T: Fn(&Path) -> Result<()>,
F: Fn(&Path) -> Result<()>,
{
if !path.exists() {
action(path)?;

View file

@ -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();