diff --git a/cli/generate/src/lib.rs b/cli/generate/src/lib.rs index fa7973cb..a981d118 100644 --- a/cli/generate/src/lib.rs +++ b/cli/generate/src/lib.rs @@ -144,36 +144,42 @@ impl From 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( + repo_path: T, + out_path: Option, + grammar_path: Option, 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, + U: Into, + V: Into, +{ + 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. diff --git a/cli/src/fuzz/mod.rs b/cli/src/fuzz/mod.rs index efa07d3b..6a8d8a6f 100644 --- a/cli/src/fuzz/mod.rs +++ b/cli/src/fuzz/mod.rs @@ -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>, - pub subdir: Option, + pub subdir: Option, pub edits: usize, pub iterations: usize, pub include: Option, diff --git a/cli/src/main.rs b/cli/src/main.rs index 94286797..eb5cf0aa 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -90,7 +90,7 @@ struct Init { struct Generate { /// The path to the grammar file #[arg(index = 1)] - pub grammar_path: Option, + pub grammar_path: Option, /// 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, + pub libdir: Option, /// The path to output the generated source files #[arg(long, short, value_name = "DIRECTORY")] - pub output: Option, + pub output: Option, /// Produce a report of the states for the given rule, use `-` to report every rule #[arg(long)] pub report_states_for_rule: Option, @@ -146,7 +146,7 @@ struct Build { pub docker: bool, /// The path to output the compiled file #[arg(short, long)] - pub output: Option, + pub output: Option, /// The path to the grammar directory #[arg(index = 1, num_args = 1)] pub path: Option, @@ -316,7 +316,7 @@ struct Fuzz { pub skip: Option>, /// Subdirectory to the language #[arg(long)] - pub subdir: Option, + pub subdir: Option, /// The path to the tree-sitter grammar directory #[arg(long, short = 'p')] pub grammar_path: Option, @@ -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)?;