-
Notifications
You must be signed in to change notification settings - Fork 1
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
2 changed files
with
59 additions
and
25 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 |
---|---|---|
@@ -1,33 +1,63 @@ | ||
use std::io::Read; | ||
use std::io::{BufRead, BufReader, Read}; | ||
use std::marker::PhantomData; | ||
|
||
use oreo_errors::OreoError; | ||
use serde::de::DeserializeOwned; | ||
use ureq::Response; | ||
|
||
use crate::rpc_abi::RpcResponseStream; | ||
|
||
pub struct StreamReader<T> { | ||
reader: BufReader<Box<dyn Read>>, | ||
_marker: PhantomData<T>, | ||
} | ||
|
||
impl<T> StreamReader<T> { | ||
pub fn new(reader: Box<dyn Read>) -> Self { | ||
Self { | ||
reader: BufReader::new(reader), | ||
_marker: PhantomData, | ||
} | ||
} | ||
} | ||
|
||
impl<T> Iterator for StreamReader<T> | ||
where | ||
T: DeserializeOwned, | ||
{ | ||
type Item = Result<T, OreoError>; | ||
|
||
fn next(&mut self) -> Option<Self::Item> { | ||
let mut chunk = Vec::new(); | ||
match self.reader.read_until(b'\x0c', &mut chunk) { | ||
Ok(0) => None, // EOF reached | ||
Ok(_) => { | ||
if chunk.ends_with(&[b'\x0c']) { | ||
chunk.pop(); | ||
} | ||
|
||
if chunk.is_empty() { | ||
self.next() | ||
} else { | ||
// Deserialize the chunk | ||
let item_result = serde_json::from_slice::<RpcResponseStream<T>>(&chunk) | ||
.map(|item| item.data) | ||
.map_err(|e| OreoError::InternalRpcError(e.to_string())); | ||
Some(item_result) | ||
} | ||
} | ||
Err(e) => Some(Err(OreoError::InternalRpcError(e.to_string()))), | ||
} | ||
} | ||
} | ||
|
||
pub trait RequestExt { | ||
fn collect_stream<T: DeserializeOwned>(self) -> Result<Vec<T>, OreoError>; | ||
fn into_stream<T: DeserializeOwned>(self) -> StreamReader<T>; | ||
} | ||
|
||
impl RequestExt for Response { | ||
fn collect_stream<T: DeserializeOwned>(self) -> Result<Vec<T>, OreoError> { | ||
let reader = self.into_reader(); | ||
let mut buffered = std::io::BufReader::new(reader); | ||
let mut items = Vec::new(); | ||
let mut response_str = String::new(); | ||
buffered.read_to_string(&mut response_str).map_err(|e| OreoError::InternalRpcError(e.to_string()))?; | ||
let lines = response_str.split('\x0c').collect::<Vec<&str>>(); | ||
|
||
// Get rid of status code | ||
for line in lines[0..lines.len()-1].into_iter() { | ||
let line = *line; // Dereference to get &str | ||
if !line.trim().is_empty() { | ||
let item: RpcResponseStream<T> = serde_json::from_str(line) | ||
.map_err(|e| OreoError::InternalRpcError(e.to_string()))?; | ||
items.push(item.data); | ||
} | ||
} | ||
Ok(items) | ||
} | ||
} | ||
fn into_stream<T: DeserializeOwned>(self) -> StreamReader<T> { | ||
let reader = self.into_reader(); | ||
StreamReader::new(Box::new(reader)) | ||
} | ||
} |