From 9fd5689ebb1dab17398fb8866b337165cd0acdef Mon Sep 17 00:00:00 2001 From: Matthias Schiffer Date: Thu, 4 Jan 2024 14:21:12 +0100 Subject: [PATCH] core: add sign pattern command line argument handling --- Cargo.lock | 39 +++++++++++++++++++++++++++++++++++++++ Cargo.toml | 1 + src/core/common.rs | 25 ++++++++++++++++++++++--- src/core/mod.rs | 14 +++++++++++++- 4 files changed, 75 insertions(+), 4 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 7c96fdc..e50f306 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -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" diff --git a/Cargo.toml b/Cargo.toml index 28359f3..5442162 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -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" diff --git a/src/core/common.rs b/src/core/common.rs index ca6f424..92d0019 100644 --- a/src/core/common.rs +++ b/src/core/common.rs @@ -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 { 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 { + 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 diff --git a/src/core/mod.rs b/src/core/mod.rs index 1afdf91..274bae8 100644 --- a/src/core/mod.rs +++ b/src/core/mod.rs @@ -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, + /// 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, /// 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 {