From 818de6be47073d2b53bc7bbb43675c11b02e4b4a Mon Sep 17 00:00:00 2001 From: Chaoscaot Date: Sat, 1 Apr 2023 10:30:25 +0200 Subject: [PATCH] Abstractions --- schemsearch-cli/src/json_output.rs | 8 +++----- schemsearch-cli/src/main.rs | 10 +++++----- schemsearch-cli/src/sinks.rs | 13 +++++-------- schemsearch-cli/src/types.rs | 1 + schemsearch-java/src/lib.rs | 4 ++-- schemsearch-lib/src/lib.rs | 30 +++++++++++++++++++++++++++--- 6 files changed, 43 insertions(+), 23 deletions(-) diff --git a/schemsearch-cli/src/json_output.rs b/schemsearch-cli/src/json_output.rs index 79f963f..a3d4776 100644 --- a/schemsearch-cli/src/json_output.rs +++ b/schemsearch-cli/src/json_output.rs @@ -1,5 +1,5 @@ use serde::{Deserialize, Serialize}; -use schemsearch_lib::SearchBehavior; +use schemsearch_lib::{Match, SearchBehavior}; #[derive(Serialize, Deserialize, Debug)] #[serde(tag = "event")] @@ -12,10 +12,8 @@ pub enum JsonEvent { #[derive(Serialize, Deserialize, Debug)] pub struct FoundEvent { pub name: String, - pub x: u16, - pub y: u16, - pub z: u16, - pub percent: f32, + #[serde(flatten, rename = "match")] + pub match_: Match, } #[derive(Serialize, Deserialize, Debug)] diff --git a/schemsearch-cli/src/main.rs b/schemsearch-cli/src/main.rs index 2faa752..143bdb1 100644 --- a/schemsearch-cli/src/main.rs +++ b/schemsearch-cli/src/main.rs @@ -25,7 +25,7 @@ use clap::{command, Arg, ArgAction, ValueHint}; use std::path::PathBuf; use std::str::FromStr; use clap::error::ErrorKind; -use schemsearch_lib::{search, SearchBehavior}; +use schemsearch_lib::{Match, search, SearchBehavior}; use crate::types::{PathSchematicSupplier, SchematicSupplierType}; #[cfg(feature = "sql")] use futures::executor::block_on; @@ -37,7 +37,7 @@ use schemsearch_sql::filter::SchematicFilter; use schemsearch_sql::load_all_schematics; #[cfg(feature = "sql")] use crate::types::SqlSchematicSupplier; -use indicatif::{ParallelProgressIterator, ProgressStyle}; +use indicatif::*; use schemsearch_files::Schematic; use crate::sinks::{OutputFormat, OutputSink}; @@ -253,7 +253,7 @@ fn main() { Some(x) => x, None => return SearchResult { name: schem.get_name(), - matches: vec![] + matches: Vec::default() } }; SearchResult { @@ -274,7 +274,7 @@ fn main() { println!("Error while loading schematic ({}): {}", schem.get_name(), e.to_string()); SearchResult { name: schem.get_name(), - matches: vec![] + matches: Vec::default() } } } @@ -312,6 +312,6 @@ fn load_schem(schem_path: &PathBuf) -> Option { #[derive(Debug, Clone)] struct SearchResult { name: String, - matches: Vec<(u16, u16, u16, f32)>, + matches: Vec, } diff --git a/schemsearch-cli/src/sinks.rs b/schemsearch-cli/src/sinks.rs index c6a6478..c6f09a9 100644 --- a/schemsearch-cli/src/sinks.rs +++ b/schemsearch-cli/src/sinks.rs @@ -2,7 +2,7 @@ use std::fs::File; use std::io::BufWriter; use std::str::FromStr; use std::io::Write; -use schemsearch_lib::SearchBehavior; +use schemsearch_lib::{Match, SearchBehavior}; use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent}; #[derive(Debug, Clone)] @@ -52,16 +52,13 @@ impl OutputSink { } impl OutputFormat { - pub fn found_match(&self, name: &String, pos: (u16, u16, u16, f32)) -> String { + pub fn found_match(&self, name: &String, pos: Match) -> String { match self { - OutputFormat::Text => format!("Found match in '{}' at x: {}, y: {}, z: {}, % = {}\n", name, pos.0, pos.1, pos.2, pos.3), - OutputFormat::CSV => format!("{},{},{},{},{}\n", name, pos.0, pos.1, pos.2, pos.3), + OutputFormat::Text => format!("Found match in '{}' at x: {}, y: {}, z: {}, % = {}\n", name, pos.x, pos.y, pos.z, pos.percent), + OutputFormat::CSV => format!("{},{},{},{},{}\n", name, pos.x, pos.y, pos.z, pos.percent), OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::Found(FoundEvent { name: name.clone(), - x: pos.0, - y: pos.1, - z: pos.2, - percent: pos.3, + match_: pos, })).unwrap()) } } diff --git a/schemsearch-cli/src/types.rs b/schemsearch-cli/src/types.rs index c2127c7..5dc7ca5 100644 --- a/schemsearch-cli/src/types.rs +++ b/schemsearch-cli/src/types.rs @@ -18,6 +18,7 @@ use std::path::PathBuf; #[cfg(feature = "sql")] use futures::executor::block_on; +#[allow(unused_imports)] use schemsearch_files::Schematic; #[cfg(feature = "sql")] use schemsearch_sql::{load_schemdata, SchematicNode}; diff --git a/schemsearch-java/src/lib.rs b/schemsearch-java/src/lib.rs index 4323c55..e9a2d96 100644 --- a/schemsearch-java/src/lib.rs +++ b/schemsearch-java/src/lib.rs @@ -45,8 +45,8 @@ pub extern "system" fn Java_SchemSearch_search<'local>(mut env: JNIEnv<'local>, }); let mut result = String::new(); - for (x, y, z, p) in matches { - result.push_str(&format!("{}, {}, {}, {};", x, y, z, p)); + for m in matches { + result.push_str(&format!("{}, {}, {}, {};", m.x, m.y, m.z, m.percent)); } result.remove(result.len() - 1); let output = env.new_string(result).expect("Couldn't create java string!"); diff --git a/schemsearch-lib/src/lib.rs b/schemsearch-lib/src/lib.rs index 68b5c6c..623c508 100644 --- a/schemsearch-lib/src/lib.rs +++ b/schemsearch-lib/src/lib.rs @@ -36,7 +36,7 @@ pub fn search( schem: Schematic, pattern_schem: &Schematic, search_behavior: SearchBehavior, -) -> Vec<(u16, u16, u16, f32)> { +) -> Vec { if schem.width < pattern_schem.width || schem.height < pattern_schem.height || schem.length < pattern_schem.length { return vec![]; } @@ -47,7 +47,7 @@ pub fn search( let pattern_schem = match_palette(&schem, &pattern_schem, search_behavior.ignore_block_data); - let mut matches: Vec<(u16, u16, u16, f32)> = Vec::new(); + let mut matches: Vec = Vec::new(); let pattern_data = pattern_schem.block_data.as_slice(); @@ -90,7 +90,12 @@ pub fn search( } let matching_percent = matching as f32 / pattern_blocks; if matching_percent >= search_behavior.threshold { - matches.push((x as u16, y as u16, z as u16, matching_percent)); + matches.push(Match { + x: x as u16, + y: y as u16, + z: z as u16, + percent: matching_percent, + }); } } } @@ -99,6 +104,25 @@ pub fn search( return matches; } +#[derive(Debug, Clone, Copy, Deserialize, Serialize)] +pub struct Match { + pub x: u16, + pub y: u16, + pub z: u16, + pub percent: f32, +} + +impl Default for Match { + fn default() -> Self { + Self { + x: 0, + y: 0, + z: 0, + percent: 0.0, + } + } +} + #[inline] pub fn normalize_data(data: &str, ignore_data: bool) -> &str { if ignore_data {