fix(generate): take AsRef<Path> for the path parameter to avoid clones
This commit is contained in:
parent
5aad87a74c
commit
26fa3a76a5
2 changed files with 27 additions and 18 deletions
|
|
@ -9,6 +9,7 @@ mod token_conflicts;
|
|||
use std::collections::{BTreeSet, HashMap};
|
||||
|
||||
use anyhow::Result;
|
||||
pub use build_lex_table::LARGE_CHARACTER_RANGE_COUNT;
|
||||
use log::info;
|
||||
|
||||
use self::{
|
||||
|
|
@ -26,8 +27,6 @@ use crate::generate::{
|
|||
tables::{LexTable, ParseAction, ParseTable, ParseTableEntry},
|
||||
};
|
||||
|
||||
pub use build_lex_table::LARGE_CHARACTER_RANGE_COUNT;
|
||||
|
||||
pub struct Tables {
|
||||
pub parse_table: ParseTable,
|
||||
pub main_lex_table: LexTable,
|
||||
|
|
|
|||
|
|
@ -410,7 +410,7 @@ pub fn generate_grammar_files(
|
|||
// Generate Python bindings
|
||||
missing_path(bindings_dir.join("python"), create_dir)?.apply(|path| {
|
||||
let lang_path = path.join(format!("tree_sitter_{}", language_name.to_snake_case()));
|
||||
missing_path(lang_path.clone(), create_dir)?;
|
||||
missing_path(&lang_path, create_dir)?;
|
||||
|
||||
missing_path(lang_path.join("binding.c"), |path| {
|
||||
generate_file(path, PY_BINDING_C_TEMPLATE, language_name)
|
||||
|
|
@ -442,7 +442,7 @@ pub fn generate_grammar_files(
|
|||
// Generate Swift bindings
|
||||
missing_path(bindings_dir.join("swift"), create_dir)?.apply(|path| {
|
||||
let lang_path = path.join(format!("TreeSitter{}", language_name.to_upper_camel_case()));
|
||||
missing_path(lang_path.clone(), create_dir)?;
|
||||
missing_path(&lang_path, create_dir)?;
|
||||
|
||||
missing_path(lang_path.join(format!("{language_name}.h")), |path| {
|
||||
generate_file(path, PARSER_NAME_H_TEMPLATE, language_name)
|
||||
|
|
@ -515,23 +515,29 @@ fn create_dir(path: &Path) -> Result<()> {
|
|||
}
|
||||
|
||||
#[derive(PartialEq, Eq, Debug)]
|
||||
enum PathState {
|
||||
Exists(PathBuf),
|
||||
Missing(PathBuf),
|
||||
enum PathState<P>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
Exists(P),
|
||||
Missing(P),
|
||||
}
|
||||
|
||||
#[allow(dead_code)]
|
||||
impl PathState {
|
||||
impl<P> PathState<P>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
{
|
||||
fn exists(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
|
||||
if let Self::Exists(path) = self {
|
||||
action(path.as_path())?;
|
||||
action(path.as_ref())?;
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
||||
fn missing(&self, mut action: impl FnMut(&Path) -> Result<()>) -> Result<&Self> {
|
||||
if let Self::Missing(path) = self {
|
||||
action(path.as_path())?;
|
||||
action(path.as_ref())?;
|
||||
}
|
||||
Ok(self)
|
||||
}
|
||||
|
|
@ -548,33 +554,37 @@ impl PathState {
|
|||
|
||||
fn as_path(&self) -> &Path {
|
||||
match self {
|
||||
Self::Exists(path) | Self::Missing(path) => path.as_path(),
|
||||
Self::Exists(path) | Self::Missing(path) => path.as_ref(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_path<F>(path: PathBuf, mut action: F) -> Result<PathState>
|
||||
fn missing_path<P, F>(path: P, mut action: F) -> Result<PathState<P>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
F: FnMut(&Path) -> Result<()>,
|
||||
{
|
||||
if !path.exists() {
|
||||
action(path.as_path())?;
|
||||
let path_ref = path.as_ref();
|
||||
if !path_ref.exists() {
|
||||
action(path_ref)?;
|
||||
Ok(PathState::Missing(path))
|
||||
} else {
|
||||
Ok(PathState::Exists(path))
|
||||
}
|
||||
}
|
||||
|
||||
fn missing_path_else<T, F>(path: PathBuf, mut action: T, mut else_action: F) -> Result<PathState>
|
||||
fn missing_path_else<P, T, F>(path: P, mut action: T, mut else_action: F) -> Result<PathState<P>>
|
||||
where
|
||||
P: AsRef<Path>,
|
||||
T: FnMut(&Path) -> Result<()>,
|
||||
F: FnMut(&Path) -> Result<()>,
|
||||
{
|
||||
if !path.exists() {
|
||||
action(path.as_path())?;
|
||||
let path_ref = path.as_ref();
|
||||
if !path_ref.exists() {
|
||||
action(path_ref)?;
|
||||
Ok(PathState::Missing(path))
|
||||
} else {
|
||||
else_action(path.as_path())?;
|
||||
else_action(path_ref)?;
|
||||
Ok(PathState::Exists(path))
|
||||
}
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue