fix(cli): use a generic Into<Path> over single type for path arguments

Co-authored-by: Amaan Qureshi <amaanq12@gmail.com>
This commit is contained in:
Will Lillis 2025-05-22 06:20:18 -04:00
parent 50eaf5befd
commit ac13c86675
3 changed files with 36 additions and 25 deletions

View file

@ -144,36 +144,42 @@ impl From<semver::Error> for JSError {
}
}
pub fn generate_parser_in_directory(
repo_path: &Path,
out_path: Option<&str>,
grammar_path: Option<&str>,
pub fn generate_parser_in_directory<T, U, V>(
repo_path: T,
out_path: Option<U>,
grammar_path: Option<V>,
mut abi_version: usize,
report_symbol_name: Option<&str>,
js_runtime: Option<&str>,
) -> GenerateResult<()> {
let mut repo_path = repo_path.to_owned();
let mut grammar_path = grammar_path;
) -> GenerateResult<()>
where
T: Into<PathBuf>,
U: Into<PathBuf>,
V: Into<PathBuf>,
{
let mut repo_path: PathBuf = repo_path.into();
// Populate a new empty grammar directory.
if let Some(path) = grammar_path {
let path = PathBuf::from(path);
if !path
let grammar_path = if let Some(path) = grammar_path {
let path_buf: PathBuf = path.into();
if !path_buf
.try_exists()
.map_err(|e| GenerateError::GrammarPath(e.to_string()))?
{
fs::create_dir_all(&path)?;
grammar_path = None;
repo_path = path;
fs::create_dir_all(&path_buf)?;
repo_path = path_buf;
repo_path.join("grammar.js")
} else {
path_buf
}
}
let grammar_path = grammar_path.map_or_else(|| repo_path.join("grammar.js"), PathBuf::from);
} else {
repo_path.join("grammar.js")
};
// Read the grammar file.
let grammar_json = load_grammar_file(&grammar_path, js_runtime)?;
let src_path = out_path.map_or_else(|| repo_path.join("src"), PathBuf::from);
let src_path = out_path.map_or_else(|| repo_path.join("src"), |p| p.into());
let header_path = src_path.join("tree_sitter");
// Ensure that the output directories exist.

View file

@ -1,4 +1,9 @@
use std::{collections::HashMap, env, fs, path::Path, sync::LazyLock};
use std::{
collections::HashMap,
env, fs,
path::{Path, PathBuf},
sync::LazyLock,
};
use rand::Rng;
use regex::Regex;
@ -64,7 +69,7 @@ pub fn new_seed() -> usize {
pub struct FuzzOptions {
pub skipped: Option<Vec<String>>,
pub subdir: Option<String>,
pub subdir: Option<PathBuf>,
pub edits: usize,
pub iterations: usize,
pub include: Option<Regex>,

View file

@ -90,7 +90,7 @@ struct Init {
struct Generate {
/// The path to the grammar file
#[arg(index = 1)]
pub grammar_path: Option<String>,
pub grammar_path: Option<PathBuf>,
/// Show debug log during generation
#[arg(long, short)]
pub log: bool,
@ -115,10 +115,10 @@ struct Generate {
pub debug_build: bool,
/// The path to the directory containing the parser library
#[arg(long, value_name = "PATH")]
pub libdir: Option<String>,
pub libdir: Option<PathBuf>,
/// The path to output the generated source files
#[arg(long, short, value_name = "DIRECTORY")]
pub output: Option<String>,
pub output: Option<PathBuf>,
/// Produce a report of the states for the given rule, use `-` to report every rule
#[arg(long)]
pub report_states_for_rule: Option<String>,
@ -146,7 +146,7 @@ struct Build {
pub docker: bool,
/// The path to output the compiled file
#[arg(short, long)]
pub output: Option<String>,
pub output: Option<PathBuf>,
/// The path to the grammar directory
#[arg(index = 1, num_args = 1)]
pub path: Option<PathBuf>,
@ -316,7 +316,7 @@ struct Fuzz {
pub skip: Option<Vec<String>>,
/// Subdirectory to the language
#[arg(long)]
pub subdir: Option<String>,
pub subdir: Option<PathBuf>,
/// The path to the tree-sitter grammar directory
#[arg(long, short = 'p')]
pub grammar_path: Option<PathBuf>,
@ -802,7 +802,7 @@ impl Generate {
}
if self.build {
if let Some(path) = self.libdir {
loader = loader::Loader::with_parser_lib_path(PathBuf::from(path));
loader = loader::Loader::with_parser_lib_path(path);
}
loader.debug_build(self.debug_build);
loader.languages_at_path(current_dir)?;