Skip to content

Commit d7104b2

Browse files
authored
perf(fmt): use native ignore matcher (#301)
1 parent e1998f2 commit d7104b2

11 files changed

Lines changed: 432 additions & 53 deletions

File tree

.github/workflows/lint.yml

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,9 @@ jobs:
4343
- name: Lint Rust
4444
run: cargo clippy --workspace --all-targets --locked -- -D warnings
4545

46+
- name: Build Native Binding
47+
run: pnpm --filter rstack build:native
48+
4649
- name: Lint
4750
run: node --run lint
4851

Cargo.lock

Lines changed: 182 additions & 3 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
[workspace]
2-
members = ["crates/rstack-binding"]
2+
members = ["crates/rstack-binding", "crates/rstack-ignore"]
33
resolver = "2"
44

55
[workspace.package]
@@ -9,9 +9,12 @@ repository = "https://github.com/rstackjs/rstack-cli"
99
rust-version = "1.88"
1010

1111
[workspace.dependencies]
12+
ignore = { version = "0.4.33", default-features = false }
1213
napi = { version = "3.12.0", default-features = false, features = ["napi9"] }
1314
napi-build = "2.4.0"
1415
napi-derive = "3.6.2"
16+
pathdiff = "0.2.3"
17+
rstack-ignore = { path = "crates/rstack-ignore" }
1518

1619
[profile.release]
1720
lto = true

crates/rstack-binding/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,12 @@ publish = false
99

1010
[lib]
1111
crate-type = ["cdylib"]
12+
test = false
1213

1314
[dependencies]
1415
napi.workspace = true
1516
napi-derive.workspace = true
17+
rstack-ignore.workspace = true
1618

1719
[build-dependencies]
1820
napi-build.workspace = true

crates/rstack-binding/src/lib.rs

Lines changed: 34 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,44 @@
11
#![deny(clippy::all)]
22

3+
use std::path::Path;
4+
5+
use napi::Error;
36
use napi_derive::napi;
7+
use rstack_ignore::{IgnoreMatcher as CoreIgnoreMatcher, IgnoreSource as CoreIgnoreSource};
8+
9+
/// A Gitignore-compatible pattern source received from JavaScript.
10+
#[napi(object, object_to_js = false)]
11+
pub struct IgnoreSource {
12+
/// Directory that patterns are resolved from.
13+
pub root_path: String,
14+
/// Newline-delimited Gitignore patterns.
15+
pub patterns: String,
16+
}
417

18+
/// JavaScript-facing wrapper around the compiled Rust matcher.
519
#[napi]
6-
pub fn native_ping(input: String) -> String {
7-
format!("pong:{input}")
20+
pub struct IgnoreMatcher {
21+
inner: CoreIgnoreMatcher,
822
}
923

10-
#[cfg(test)]
11-
mod tests {
12-
use super::native_ping;
24+
#[napi]
25+
impl IgnoreMatcher {
26+
/// Compiles all pattern sources once and keeps the result for repeated path checks.
27+
#[napi(constructor)]
28+
pub fn new(sources: Vec<IgnoreSource>) -> napi::Result<Self> {
29+
let sources = sources
30+
.into_iter()
31+
.map(|source| CoreIgnoreSource::new(source.root_path, source.patterns));
32+
let inner = CoreIgnoreMatcher::new(sources).map_err(|error| {
33+
Error::from_reason(format!("Failed to compile ignore patterns: {error}"))
34+
})?;
35+
36+
Ok(Self { inner })
37+
}
1338

14-
#[test]
15-
fn formats_ping_response() {
16-
assert_eq!(native_ping("rstack".to_string()), "pong:rstack");
39+
/// Returns whether a file or directory is ignored by any source.
40+
#[napi]
41+
pub fn is_ignored(&mut self, file_path: String, is_directory: bool) -> bool {
42+
self.inner.is_ignored(Path::new(&file_path), is_directory)
1743
}
1844
}

crates/rstack-ignore/Cargo.toml

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
[package]
2+
name = "rstack-ignore"
3+
version = "0.1.0"
4+
edition.workspace = true
5+
license.workspace = true
6+
repository.workspace = true
7+
rust-version.workspace = true
8+
publish = false
9+
10+
[dependencies]
11+
ignore.workspace = true
12+
pathdiff.workspace = true

0 commit comments

Comments
 (0)