Skip to content

Commit 52fae76

Browse files
authored
Replace direct regex crate dependency (#910)
* Replace regex with a simple string check * Remove direct dependency
1 parent 95210d7 commit 52fae76

3 files changed

Lines changed: 18 additions & 11 deletions

File tree

Cargo.lock

Lines changed: 0 additions & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ edition = "2024"
1515
clap = { version = "4.5.32", features = ["derive"] }
1616
ctrlc = { version = "3.2", features = ["termination"] }
1717
ignore = "0.4.18"
18-
regex = "1.11.1"
1918
rubyfmt = { path = "./librubyfmt" }
2019
similar = { version = "2.7.0", features = ["bytes"] }
2120

src/main.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,18 +3,14 @@
33
use clap::Parser;
44
use ignore::WalkBuilder;
55
use ignore::gitignore::GitignoreBuilder;
6-
use regex::Regex;
76
use rubyfmt::init_logger;
87
use similar::TextDiff;
98
use std::ffi::OsStr;
109
use std::fs::{File, OpenOptions, read};
1110
use std::io::{self, BufRead, BufReader, IsTerminal, Read, Write};
1211
use std::path::Path;
1312
use std::process::{Command, exit};
14-
use std::sync::{Arc, LazyLock, Mutex};
15-
16-
static MAGIC_COMMENT_REGEX: LazyLock<Regex> =
17-
LazyLock::new(|| Regex::new(r"(?m)^#\s*rubyfmt:\s*(?P<enabled>true|false)\s*$").unwrap());
13+
use std::sync::{Arc, Mutex};
1814

1915
/// Simple Enum to exit on errors or not
2016
#[derive(Debug, PartialEq, Copy, Clone)]
@@ -164,10 +160,7 @@ fn rubyfmt_string(
164160
let slice_size = buffer.len().min(500);
165161
let slice = String::from_utf8_lossy(&buffer[..slice_size]);
166162

167-
let matched = MAGIC_COMMENT_REGEX
168-
.captures(&slice)
169-
.and_then(|c| c.name("enabled"))
170-
.map(|s| s.as_str());
163+
let matched = parse_magic_comment(&slice);
171164

172165
// If opted in to magic "# rubyfmt: true" header and true is not
173166
// in the file, return early
@@ -189,6 +182,22 @@ fn rubyfmt_string(
189182
/* Helpers */
190183
/******************************************************/
191184

185+
/// Scan for a `# rubyfmt: true` or `# rubyfmt: false` magic comment
186+
fn parse_magic_comment(slice: &str) -> Option<&str> {
187+
for line in slice.lines() {
188+
if let Some(rest) = line.strip_prefix('#') {
189+
let rest = rest.trim_start();
190+
if let Some(rest) = rest.strip_prefix("rubyfmt:") {
191+
let val = rest.trim();
192+
if val == "true" || val == "false" {
193+
return Some(val);
194+
}
195+
}
196+
}
197+
}
198+
None
199+
}
200+
192201
/// Check if a path should be ignored based on .gitignore and .rubyfmtignore patterns.
193202
/// The path should be relative to the current working directory.
194203
fn is_path_ignored(path: &Path, include_gitignored: bool) -> bool {

0 commit comments

Comments
 (0)