Skip to content

Commit 3e30036

Browse files
committed
jsondocck: minor cleanups
- replace `OnceLock` with `LazyLock` - use `let..else` where applicable
1 parent 9cd60bd commit 3e30036

File tree

1 file changed

+10
-15
lines changed

1 file changed

+10
-15
lines changed

src/tools/jsondocck/src/main.rs

Lines changed: 10 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::borrow::Cow;
22
use std::process::ExitCode;
3-
use std::sync::OnceLock;
3+
use std::sync::LazyLock;
44
use std::{env, fs};
55

66
use regex::{Regex, RegexBuilder};
@@ -151,8 +151,7 @@ impl CommandKind {
151151
}
152152
}
153153

154-
static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
155-
fn line_pattern() -> Regex {
154+
static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
156155
RegexBuilder::new(
157156
r#"
158157
//@\s+
@@ -165,7 +164,7 @@ fn line_pattern() -> Regex {
165164
.unicode(true)
166165
.build()
167166
.unwrap()
168-
}
167+
});
169168

170169
fn print_err(msg: &str, lineno: usize) {
171170
eprintln!("Invalid command: {} on line {}", msg, lineno)
@@ -184,21 +183,17 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
184183
for (lineno, line) in file.split('\n').enumerate() {
185184
let lineno = lineno + 1;
186185

187-
let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) {
188-
Some(c) => c,
189-
None => continue,
186+
let Some(cap) = LINE_PATTERN.captures(line) else {
187+
continue;
190188
};
191189

192-
let negated = cap.name("negated").unwrap().as_str() == "!";
190+
let negated = &cap["negated"] == "!";
193191

194192
let args_str = &cap["args"];
195-
let args = match shlex::split(args_str) {
196-
Some(args) => args,
197-
None => {
198-
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
199-
errors = true;
200-
continue;
201-
}
193+
let Some(args) = shlex::split(args_str) else {
194+
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
195+
errors = true;
196+
continue;
202197
};
203198

204199
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {

0 commit comments

Comments
 (0)