jsondocck: minor cleanups

- replace `OnceLock` with `LazyLock`
- use `let..else` where applicable
This commit is contained in:
Yotam Ofek 2025-02-15 22:36:11 +00:00
parent 002da76821
commit 11e7aaf6e4

View file

@ -1,6 +1,6 @@
use std::borrow::Cow;
use std::process::ExitCode;
use std::sync::OnceLock;
use std::sync::LazyLock;
use std::{env, fs};
use regex::{Regex, RegexBuilder};
@ -151,8 +151,7 @@ impl CommandKind {
}
}
static LINE_PATTERN: OnceLock<Regex> = OnceLock::new();
fn line_pattern() -> Regex {
static LINE_PATTERN: LazyLock<Regex> = LazyLock::new(|| {
RegexBuilder::new(
r#"
//@\s+
@ -165,7 +164,7 @@ fn line_pattern() -> Regex {
.unicode(true)
.build()
.unwrap()
}
});
fn print_err(msg: &str, lineno: usize) {
eprintln!("Invalid command: {} on line {}", msg, lineno)
@ -184,21 +183,17 @@ fn get_commands(template: &str) -> Result<Vec<Command>, ()> {
for (lineno, line) in file.split('\n').enumerate() {
let lineno = lineno + 1;
let cap = match LINE_PATTERN.get_or_init(line_pattern).captures(line) {
Some(c) => c,
None => continue,
let Some(cap) = LINE_PATTERN.captures(line) else {
continue;
};
let negated = cap.name("negated").unwrap().as_str() == "!";
let negated = &cap["negated"] == "!";
let args_str = &cap["args"];
let args = match shlex::split(args_str) {
Some(args) => args,
None => {
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
errors = true;
continue;
}
let Some(args) = shlex::split(args_str) else {
print_err(&format!("Invalid arguments to shlex::split: `{args_str}`",), lineno);
errors = true;
continue;
};
if let Some((kind, path)) = CommandKind::parse(&cap["cmd"], negated, &args) {