From 471caeb9c9823b88ed0ca785ccb75d14efe21359 Mon Sep 17 00:00:00 2001 From: Zynh0722 Date: Sun, 25 Feb 2024 15:18:03 -0800 Subject: [PATCH] rust go brr --- src/main.rs | 9 +++++---- src/map_ok.rs | 33 +++++++++++++++++++++++++++++++++ 2 files changed, 38 insertions(+), 4 deletions(-) create mode 100644 src/map_ok.rs diff --git a/src/main.rs b/src/main.rs index e3c20ec..fc60099 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,6 +1,9 @@ mod compile_data; +mod map_ok; mod raw_data; +use map_ok::MapOkTrait; + use crate::compile_data::{DpsRole, JobStats, Role}; use crate::raw_data::RawJobStats; @@ -17,10 +20,8 @@ fn get_job_stats(lodestone_ids: &[usize]) -> anyhow::Result>> let raw_data = output.stdout.lines(); let data: Vec, _>> = raw_data .map_while(Result::ok) - .map(|raw| serde_json::from_str(&raw)) - .map(|raw_stats: Result, _>| { - raw_stats.map(|raw| raw.into_iter().map(|r| r.into()).collect()) - }) + .map(|buf| serde_json::from_str::>(&buf)) + .map_ok(|raw| raw.into_iter().map(|r| r.into()).collect()) .map(|stats| stats.map_err(|err| anyhow::anyhow!(err))) .collect(); diff --git a/src/map_ok.rs b/src/map_ok.rs new file mode 100644 index 0000000..67cd0fd --- /dev/null +++ b/src/map_ok.rs @@ -0,0 +1,33 @@ +#[derive(Clone)] +pub struct MapOkIterator { + iter: I, + f: F, +} + +impl Iterator for MapOkIterator +where + F: FnMut(A) -> B, + I: Iterator>, +{ + type Item = Result; + + #[inline] + fn next(&mut self) -> Option { + self.iter.next().map(|x| x.map(&mut self.f)) + } +} + +pub trait MapOkTrait { + fn map_ok(self, func: F) -> MapOkIterator + where + Self: Sized + Iterator>, + F: FnMut(A) -> B, + { + MapOkIterator { + iter: self, + f: func, + } + } +} + +impl MapOkTrait for I where I: Sized + Iterator> {}