-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
169 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,31 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use schemsearch_lib::SearchBehavior; | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
#[serde(tag = "event")] | ||
pub enum JsonEvent { | ||
Found(FoundEvent), | ||
Init(InitEvent), | ||
End(EndEvent), | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct FoundEvent { | ||
pub name: String, | ||
pub x: u16, | ||
pub y: u16, | ||
pub z: u16, | ||
pub percent: f32, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct InitEvent { | ||
pub total: u32, | ||
pub search_behavior: SearchBehavior, | ||
pub start_time: u128, | ||
} | ||
|
||
#[derive(Serialize, Deserialize, Debug)] | ||
pub struct EndEvent { | ||
pub end_time: u128, | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,88 @@ | ||
use std::fs::File; | ||
use std::io::BufWriter; | ||
use std::str::FromStr; | ||
use std::io::Write; | ||
use schemsearch_lib::SearchBehavior; | ||
use crate::json_output::{EndEvent, FoundEvent, InitEvent, JsonEvent}; | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum OutputSink { | ||
Stdout, | ||
File(String), | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
pub enum OutputFormat { | ||
Text, | ||
CSV, | ||
JSON | ||
} | ||
|
||
impl FromStr for OutputFormat { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"text" => Ok(OutputFormat::Text), | ||
"csv" => Ok(OutputFormat::CSV), | ||
"json" => Ok(OutputFormat::JSON), | ||
_ => Err(format!("'{}' is not a valid output format", s)) | ||
} | ||
} | ||
} | ||
|
||
impl FromStr for OutputSink { | ||
type Err = String; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
"std" => Ok(OutputSink::Stdout), | ||
_ => Ok(OutputSink::File(s.to_string())) | ||
} | ||
} | ||
} | ||
|
||
impl OutputSink { | ||
pub fn output(&self) -> Box<dyn Write> { | ||
match self { | ||
OutputSink::Stdout => Box::new(std::io::stdout().lock()), | ||
OutputSink::File(path) => Box::new(BufWriter::new(File::create(path).unwrap())) | ||
} | ||
} | ||
} | ||
|
||
impl OutputFormat { | ||
pub fn found_match(&self, name: &String, pos: (u16, u16, u16, f32)) -> 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::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, | ||
})).unwrap()) | ||
} | ||
} | ||
|
||
pub fn start(&self, total: u32, search_behavior: &SearchBehavior, start_time: u128) -> String { | ||
match self { | ||
OutputFormat::Text => format!("Starting search in {} schematics\n", total), | ||
OutputFormat::CSV => format!("Name,X,Y,Z,Percent\n"), | ||
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 { | ||
match self { | ||
OutputFormat::Text => format!("Search complete in {}s\n", end_time / 1000), | ||
OutputFormat::CSV => format!("{}\n", end_time), | ||
OutputFormat::JSON => format!("{}\n", serde_json::to_string(&JsonEvent::End(EndEvent{ end_time })).unwrap()) | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters