Refactor code structure and improve performance by optimizing OpenCL kernel and adding timing macros; update Cargo.toml for release profile settings; enhance main.rs and sinks.rs for better readability and organization.

This commit is contained in:
2025-04-11 16:04:10 +02:00
parent c554b1f164
commit b04c01e737
11 changed files with 395 additions and 184 deletions

View File

@@ -1,11 +1,11 @@
use std::fs::File;
use std::io::BufWriter;
use std::str::FromStr;
use std::io::Write;
use std::time::Duration;
use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent};
use indicatif::HumanDuration;
use schemsearch_common::{Match, SearchBehavior};
use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent};
use std::fs::File;
use std::io::BufWriter;
use std::io::Write;
use std::str::FromStr;
use std::time::Duration;
#[derive(Debug, Clone)]
pub enum OutputSink {
@@ -18,7 +18,7 @@ pub enum OutputSink {
pub enum OutputFormat {
Text,
CSV,
JSON
JSON,
}
impl FromStr for OutputFormat {
@@ -29,7 +29,7 @@ impl FromStr for OutputFormat {
"text" => Ok(OutputFormat::Text),
"csv" => Ok(OutputFormat::CSV),
"json" => Ok(OutputFormat::JSON),
_ => Err(format!("'{}' is not a valid output format", s))
_ => Err(format!("'{}' is not a valid output format", s)),
}
}
}
@@ -41,7 +41,7 @@ impl FromStr for OutputSink {
match s {
"std" => Ok(OutputSink::Stdout),
"err" => Ok(OutputSink::Stderr),
_ => Ok(OutputSink::File(s.to_string()))
_ => Ok(OutputSink::File(s.to_string())),
}
}
}
@@ -51,7 +51,7 @@ impl OutputSink {
match self {
OutputSink::Stdout => Box::new(std::io::stdout()),
OutputSink::Stderr => Box::new(std::io::stderr()),
OutputSink::File(path) => Box::new(BufWriter::new(File::create(path).unwrap()))
OutputSink::File(path) => Box::new(BufWriter::new(File::create(path).unwrap())),
}
}
}
@@ -59,12 +59,21 @@ impl OutputSink {
impl OutputFormat {
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.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(),
match_: pos,
})).unwrap())
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(),
match_: pos,
}))
.unwrap()
),
}
}
@@ -72,19 +81,29 @@ impl OutputFormat {
match self {
OutputFormat::Text => format!("Starting search in {} schematics\n", total),
OutputFormat::CSV => "Name,X,Y,Z,Percent\n".to_owned(),
OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::Init(InitEvent {
total,
search_behavior: search_behavior.clone(),
start_time,
})).unwrap())
OutputFormat::JSON => format!(
"{}\n",
serde_json::to_string(&JsonEvent::Init(InitEvent {
total,
search_behavior: search_behavior.clone(),
start_time,
}))
.unwrap()
),
}
}
pub fn end(&self, end_time: u128) -> String {
pub fn end(&self, end_time: Duration) -> String {
match self {
OutputFormat::Text => format!("Search complete in {}\n", HumanDuration(Duration::from_millis(end_time as u64))),
OutputFormat::CSV => format!("{}\n", end_time),
OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::End(EndEvent{ end_time })).unwrap())
OutputFormat::Text => format!("Search complete in {:?}\n", end_time),
OutputFormat::CSV => format!("{:?}\n", end_time),
OutputFormat::JSON => format!(
"{}\n",
serde_json::to_string(&JsonEvent::End(EndEvent {
end_time: end_time.as_millis()
}))
.unwrap()
),
}
}
}
}