rust go brr

main
Zynh0722 2024-02-25 15:18:03 -08:00
parent 7997857d65
commit 471caeb9c9
2 changed files with 38 additions and 4 deletions

View File

@ -1,6 +1,9 @@
mod compile_data; mod compile_data;
mod map_ok;
mod raw_data; mod raw_data;
use map_ok::MapOkTrait;
use crate::compile_data::{DpsRole, JobStats, Role}; use crate::compile_data::{DpsRole, JobStats, Role};
use crate::raw_data::RawJobStats; use crate::raw_data::RawJobStats;
@ -17,10 +20,8 @@ fn get_job_stats(lodestone_ids: &[usize]) -> anyhow::Result<Vec<Vec<JobStats>>>
let raw_data = output.stdout.lines(); let raw_data = output.stdout.lines();
let data: Vec<Result<Vec<JobStats>, _>> = raw_data let data: Vec<Result<Vec<JobStats>, _>> = raw_data
.map_while(Result::ok) .map_while(Result::ok)
.map(|raw| serde_json::from_str(&raw)) .map(|buf| serde_json::from_str::<Vec<RawJobStats>>(&buf))
.map(|raw_stats: Result<Vec<RawJobStats>, _>| { .map_ok(|raw| raw.into_iter().map(|r| r.into()).collect())
raw_stats.map(|raw| raw.into_iter().map(|r| r.into()).collect())
})
.map(|stats| stats.map_err(|err| anyhow::anyhow!(err))) .map(|stats| stats.map_err(|err| anyhow::anyhow!(err)))
.collect(); .collect();

33
src/map_ok.rs Normal file
View File

@ -0,0 +1,33 @@
#[derive(Clone)]
pub struct MapOkIterator<I, F> {
iter: I,
f: F,
}
impl<A, B, E, I, F> Iterator for MapOkIterator<I, F>
where
F: FnMut(A) -> B,
I: Iterator<Item = Result<A, E>>,
{
type Item = Result<B, E>;
#[inline]
fn next(&mut self) -> Option<Self::Item> {
self.iter.next().map(|x| x.map(&mut self.f))
}
}
pub trait MapOkTrait {
fn map_ok<F, A, B, E>(self, func: F) -> MapOkIterator<Self, F>
where
Self: Sized + Iterator<Item = Result<A, E>>,
F: FnMut(A) -> B,
{
MapOkIterator {
iter: self,
f: func,
}
}
}
impl<I, T, E> MapOkTrait for I where I: Sized + Iterator<Item = Result<T, E>> {}