core: add sign pattern command line argument handling

This commit is contained in:
Matthias Schiffer 2024-01-04 14:21:12 +01:00
parent 6da921cca3
commit 9fd5689ebb
Signed by: neocturne
GPG key ID: 16EF3F64CB201D9C
4 changed files with 75 additions and 4 deletions

39
Cargo.lock generated
View file

@ -29,6 +29,15 @@ dependencies = [
"zerocopy",
]
[[package]]
name = "aho-corasick"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b2969dcb958b36655471fc61f7e416fa76033bdd4bfed0678d8fee1e2d07a1f0"
dependencies = [
"memchr",
]
[[package]]
name = "allocator-api2"
version = "0.2.16"
@ -554,6 +563,7 @@ dependencies = [
"num-integer",
"num_cpus",
"rayon",
"regex",
"rustc-hash",
"serde",
"serde_json",
@ -775,6 +785,35 @@ dependencies = [
"bitflags 1.3.2",
]
[[package]]
name = "regex"
version = "1.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "380b951a9c5e80ddfd6136919eef32310721aa4aacd4889a8d39124b026ab343"
dependencies = [
"aho-corasick",
"memchr",
"regex-automata",
"regex-syntax",
]
[[package]]
name = "regex-automata"
version = "0.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5f804c7828047e88b2d32e2d7fe5a105da8ee3264f01902f796c8e067dc2483f"
dependencies = [
"aho-corasick",
"memchr",
"regex-syntax",
]
[[package]]
name = "regex-syntax"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c08c74e62047bb2de4ff487b251e4a92e24f48745648451635cec7d591162d9f"
[[package]]
name = "rustc-demangle"
version = "0.1.23"

View file

@ -52,6 +52,7 @@ minedmap-types = { version = "0.1.2", path = "crates/types" }
num-integer = "0.1.45"
num_cpus = "1.16.0"
rayon = "1.7.0"
regex = "1.10.2"
rustc-hash = "1.1.0"
serde = { version = "1.0.152", features = ["rc", "derive"] }
serde_json = "1.0.99"

View file

@ -6,7 +6,9 @@ use std::{
path::{Path, PathBuf},
};
use anyhow::{Context, Result};
use indexmap::IndexSet;
use regex::RegexSet;
use serde::{Deserialize, Serialize};
use crate::{
@ -147,11 +149,13 @@ pub struct Config {
pub viewer_info_path: PathBuf,
/// Path of viewer entities file
pub viewer_entities_path: PathBuf,
/// Sign text filter patterns
pub sign_patterns: RegexSet,
}
impl Config {
/// Crates a new [Config] from [command line arguments](super::Args)
pub fn new(args: &super::Args) -> Self {
pub fn new(args: &super::Args) -> Result<Self> {
let num_threads = match args.jobs {
Some(0) => num_cpus::get(),
Some(threads) => threads,
@ -168,7 +172,9 @@ impl Config {
.iter()
.collect();
Config {
let sign_patterns = Self::sign_patterns(args).context("Failed to parse sign patterns")?;
Ok(Config {
num_threads,
region_dir,
level_dat_path,
@ -178,7 +184,20 @@ impl Config {
entities_path_final,
viewer_info_path,
viewer_entities_path,
}
sign_patterns,
})
}
/// Parses the sign prefixes and sign filters into a [RegexSet]
fn sign_patterns(args: &super::Args) -> Result<RegexSet> {
let prefix_patterns: Vec<_> = args
.sign_prefix
.iter()
.map(|prefix| format!("^{}", regex::escape(prefix)))
.collect();
Ok(RegexSet::new(
prefix_patterns.iter().chain(args.sign_filter.iter()),
)?)
}
/// Constructs the path to an input region file

View file

@ -47,6 +47,18 @@ pub struct Args {
/// Enable verbose messages
#[arg(short, long)]
pub verbose: bool,
/// Prefix for text of signs to show on the map
#[arg(long)]
pub sign_prefix: Vec<String>,
/// Regular expression for text of signs to show on the map
///
/// --sign-prefix and --sign-filter allow to filter for signs to display;
/// by default, none are visible. The options may be passed multiple times,
/// and a sign will be visible if it matches any pattern.
///
/// To make all signs visible, pass an empty string to either option.
#[arg(long)]
pub sign_filter: Vec<String>,
/// Minecraft save directory
pub input_dir: PathBuf,
/// MinedMap data directory
@ -64,7 +76,7 @@ fn setup_threads(num_threads: usize) -> Result<()> {
/// MinedMap CLI main function
pub fn cli() -> Result<()> {
let args = Args::parse();
let config = Config::new(&args);
let config = Config::new(&args)?;
tracing_subscriber::fmt()
.with_max_level(if args.verbose {