refactor: remove url dependency

This commit is contained in:
Will Lillis 2025-09-16 21:42:42 -04:00 committed by Amaan Qureshi
parent 6a28a62369
commit 46ea65c89b
10 changed files with 29 additions and 65 deletions

10
Cargo.lock generated
View file

@ -569,6 +569,12 @@ dependencies = [
"syn",
]
[[package]]
name = "dunce"
version = "1.0.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "92773504d58c093f6de2459af4af33faa518c13451eb8f2b5698ed3d36e7c813"
[[package]]
name = "either"
version = "1.15.0"
@ -1931,7 +1937,6 @@ dependencies = [
"tree-sitter-tags",
"tree-sitter-tests-proc-macro",
"unindent",
"url",
"walkdir",
"wasmparser",
"webbrowser",
@ -1953,6 +1958,7 @@ name = "tree-sitter-generate"
version = "0.26.0"
dependencies = [
"anyhow",
"dunce",
"indexmap",
"indoc",
"log",
@ -1968,7 +1974,6 @@ dependencies = [
"tempfile",
"thiserror 2.0.16",
"topological-sort",
"url",
]
[[package]]
@ -2004,7 +2009,6 @@ dependencies = [
"tree-sitter",
"tree-sitter-highlight",
"tree-sitter-tags",
"url",
]
[[package]]

View file

@ -147,7 +147,6 @@ thiserror = "2.0.16"
tiny_http = "0.12.0"
topological-sort = "0.2.2"
unindent = "0.2.4"
url = { version = "2.5.4", features = ["serde"] }
walkdir = "2.5.0"
wasmparser = "0.229.0"
webbrowser = "1.0.5"

View file

@ -59,7 +59,6 @@ serde_json.workspace = true
similar.workspace = true
streaming-iterator.workspace = true
tiny_http.workspace = true
url.workspace = true
walkdir.workspace = true
wasmparser.workspace = true
webbrowser.workspace = true

View file

@ -12,7 +12,6 @@ use serde::{Deserialize, Serialize};
use serde_json::{Map, Value};
use tree_sitter_generate::write_file;
use tree_sitter_loader::{Author, Bindings, Grammar, Links, Metadata, PathsJSON, TreeSitterJSON};
use url::Url;
const CLI_VERSION: &str = env!("CARGO_PKG_VERSION");
const CLI_VERSION_PLACEHOLDER: &str = "CLI_VERSION";
@ -110,9 +109,9 @@ pub struct JsonConfigOpts {
pub title: String,
pub description: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub repository: Option<Url>,
pub repository: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub funding: Option<Url>,
pub funding: Option<String>,
pub scope: String,
pub file_types: Vec<String>,
pub version: Version,
@ -121,7 +120,7 @@ pub struct JsonConfigOpts {
#[serde(skip_serializing_if = "Option::is_none")]
pub email: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub url: Option<Url>,
pub url: Option<String>,
pub bindings: Bindings,
}
@ -158,11 +157,7 @@ impl JsonConfigOpts {
}]),
links: Some(Links {
repository: self.repository.unwrap_or_else(|| {
Url::parse(&format!(
"https://github.com/tree-sitter/tree-sitter-{}",
self.name
))
.expect("Failed to parse default repository URL")
format!("https://github.com/tree-sitter/tree-sitter-{}", self.name)
}),
funding: self.funding,
}),
@ -277,7 +272,7 @@ pub fn generate_grammar_files(
.metadata
.links
.as_ref()
.and_then(|l| l.funding.as_ref().map(|f| f.as_str())),
.and_then(|l| l.funding.as_deref()),
version: &tree_sitter_config.metadata.version,
camel_parser_name: &camel_name,
title_parser_name: &title_name,

View file

@ -34,7 +34,6 @@ use tree_sitter_config::Config;
use tree_sitter_highlight::Highlighter;
use tree_sitter_loader::{self as loader, Bindings, TreeSitterJSON};
use tree_sitter_tags::TagsContext;
use url::Url;
const BUILD_VERSION: &str = env!("CARGO_PKG_VERSION");
const BUILD_SHA: Option<&'static str> = option_env!("BUILD_SHA");
@ -651,15 +650,10 @@ impl Init {
};
let repository = |name: &str| {
Input::<Url>::with_theme(&ColorfulTheme::default())
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Repository URL")
.allow_empty(true)
.default(
Url::parse(&format!(
"https://github.com/tree-sitter/tree-sitter-{name}"
))
.expect("Failed to parse default repository URL"),
)
.default(format!("https://github.com/tree-sitter/tree-sitter-{name}"))
.show_default(false)
.interact_text()
};
@ -668,18 +662,8 @@ impl Init {
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Funding URL")
.allow_empty(true)
.validate_with(|input: &String| {
if input.trim().is_empty()
|| Url::parse(input)
.is_ok_and(|u| u.scheme() == "http" || u.scheme() == "https")
{
Ok(())
} else {
Err("The URL must start with 'http://' or 'https://'")
}
})
.interact_text()
.map(|e| (!e.trim().is_empty()).then(|| Url::parse(&e).unwrap()))
.map(|e| Some(e.trim().to_string()))
};
let scope = |name: &str| {
@ -746,15 +730,8 @@ impl Init {
Input::<String>::with_theme(&ColorfulTheme::default())
.with_prompt("Author URL")
.allow_empty(true)
.validate_with(|input: &String| -> Result<(), &str> {
if input.trim().is_empty() || Url::parse(input).is_ok() {
Ok(())
} else {
Err("This is not a valid URL")
}
})
.interact_text()
.map(|e| (!e.trim().is_empty()).then(|| Url::parse(&e).unwrap()))
.map(|e| Some(e.trim().to_string()))
};
let bindings = || {

View file

@ -21,11 +21,12 @@ workspace = true
[features]
default = ["qjs-rt"]
load = ["dep:semver", "dep:url"]
load = ["dep:semver"]
qjs-rt = ["load", "rquickjs", "pathdiff"]
[dependencies]
anyhow.workspace = true
dunce = "1.0.5"
indexmap.workspace = true
indoc.workspace = true
log.workspace = true
@ -46,8 +47,5 @@ smallbitvec.workspace = true
thiserror.workspace = true
topological-sort.workspace = true
[target.'cfg(windows)'.dependencies]
url = { workspace = true, optional = true }
[dev-dependencies]
tempfile.workspace = true

View file

@ -431,18 +431,18 @@ pub fn load_grammar_file(
#[cfg(feature = "load")]
fn load_js_grammar_file(grammar_path: &Path, js_runtime: Option<&str>) -> JSResult<String> {
let grammar_path = fs::canonicalize(grammar_path)?;
#[cfg(windows)]
let grammar_path = url::Url::from_file_path(grammar_path)
.expect("Failed to convert path to URL")
.to_string();
let grammar_path = dunce::canonicalize(grammar_path)?;
#[cfg(feature = "qjs-rt")]
if js_runtime == Some("native") {
return quickjs::execute_native_runtime(&grammar_path);
}
// The "file:///" prefix is incompatible with the quickjs runtime, but is required
// for node and bun
#[cfg(windows)]
let grammar_path = PathBuf::from(format!("file:///{}", grammar_path.display()));
let js_runtime = js_runtime.unwrap_or("node");
let mut js_command = Command::new(js_runtime);

View file

@ -251,13 +251,7 @@ fn load_module_from_content<'a>(
module_obj.get("exports")
}
pub fn execute_native_runtime(
#[cfg(windows)] grammar_path: &str,
#[cfg(not(windows))] grammar_path: &Path,
) -> JSResult<String> {
#[cfg(not(windows))]
let grammar_path = grammar_path.to_string_lossy();
pub fn execute_native_runtime(grammar_path: &Path) -> JSResult<String> {
let runtime = Runtime::new()?;
runtime.set_memory_limit(64 * 1024 * 1024); // 64MB
@ -272,7 +266,7 @@ pub fn execute_native_runtime(
runtime.set_loader(resolver, loader);
let cwd = std::env::current_dir()?;
let relative_path = pathdiff::diff_paths(&*grammar_path, &cwd)
let relative_path = pathdiff::diff_paths(grammar_path, &cwd)
.map(|p| p.to_string_lossy().to_string())
.ok_or_else(|| JSError::IO("Failed to get relative path".to_string()))?;
@ -301,7 +295,7 @@ pub fn execute_native_runtime(
.or_js_error(&ctx)?;
globals.set("module", module).or_js_error(&ctx)?;
let grammar_path_string = grammar_path.to_string();
let grammar_path_string = grammar_path.to_string_lossy().to_string();
let main_require = Function::new(
ctx.clone(),
move |ctx_inner, target_path: String| -> rquickjs::Result<Value> {

View file

@ -40,7 +40,6 @@ semver.workspace = true
serde.workspace = true
serde_json.workspace = true
tempfile.workspace = true
url.workspace = true
tree-sitter = { workspace = true }
tree-sitter-highlight = { workspace = true, optional = true }

View file

@ -36,7 +36,6 @@ use tree_sitter::QueryErrorKind;
use tree_sitter_highlight::HighlightConfiguration;
#[cfg(feature = "tree-sitter-tags")]
use tree_sitter_tags::{Error as TagsError, TagsConfiguration};
use url::Url;
static GRAMMAR_NAME_REGEX: LazyLock<Regex> =
LazyLock::new(|| Regex::new(r#""name":\s*"(.*?)""#).unwrap());
@ -214,9 +213,9 @@ pub struct Author {
#[derive(Serialize, Deserialize)]
pub struct Links {
pub repository: Url,
pub repository: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub funding: Option<Url>,
pub funding: Option<String>,
}
#[derive(Serialize, Deserialize, Clone)]