mirror of
https://github.com/euzu/tuliprox.git
synced 2026-10-01 13:32:04 +02:00
SyncIOBridge caused tokio panic
This commit is contained in:
@@ -1,66 +1,7 @@
|
||||
use std::io::{self, Read};
|
||||
|
||||
use serde::de::DeserializeOwned;
|
||||
use serde::{Deserialize};
|
||||
use serde_json::{self, Deserializer, Value};
|
||||
use serde_json::{self, Value};
|
||||
use crate::utils::{humanize_snake_case};
|
||||
|
||||
fn read_skipping_ws(mut reader: impl Read) -> io::Result<u8> {
|
||||
loop {
|
||||
let mut byte = 0u8;
|
||||
reader.read_exact(std::slice::from_mut(&mut byte))?;
|
||||
if !byte.is_ascii_whitespace() {
|
||||
return Ok(byte);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn invalid_data(msg: &str) -> io::Error {
|
||||
io::Error::new(io::ErrorKind::InvalidData, msg)
|
||||
}
|
||||
|
||||
fn deserialize_single<T: DeserializeOwned, R: Read>(reader: R) -> io::Result<T> {
|
||||
let next_obj = Deserializer::from_reader(reader).into_iter::<T>().next();
|
||||
next_obj.map_or_else(
|
||||
|| Err(invalid_data("premature EOF")),
|
||||
|result| result.map_err(Into::into),
|
||||
)
|
||||
}
|
||||
|
||||
fn yield_next_obj<T: DeserializeOwned, R: Read>(
|
||||
mut reader: R,
|
||||
at_start: &mut bool,
|
||||
) -> io::Result<Option<T>> {
|
||||
if *at_start {
|
||||
match read_skipping_ws(&mut reader)? {
|
||||
b',' => deserialize_single(reader).map(Some),
|
||||
b']' => Ok(None),
|
||||
_ => Err(invalid_data("`,` or `]` not found")),
|
||||
}
|
||||
} else {
|
||||
*at_start = true;
|
||||
if read_skipping_ws(&mut reader)? == b'[' {
|
||||
// read the next char to see if the array is empty
|
||||
let peek = read_skipping_ws(&mut reader)?;
|
||||
if peek == b']' {
|
||||
Ok(None)
|
||||
} else {
|
||||
deserialize_single(io::Cursor::new([peek]).chain(reader)).map(Some)
|
||||
}
|
||||
} else {
|
||||
Err(invalid_data("`[` not found"))
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// https://stackoverflow.com/questions/68641157/how-can-i-stream-elements-from-inside-a-json-array-using-serde-json
|
||||
pub fn json_iter_array<T: DeserializeOwned, R: Read>(
|
||||
mut reader: R,
|
||||
) -> impl Iterator<Item = Result<T, io::Error>> {
|
||||
let mut at_start = false;
|
||||
std::iter::from_fn(move || yield_next_obj(&mut reader, &mut at_start).transpose())
|
||||
}
|
||||
|
||||
pub fn string_or_number_u32<'de, D>(deserializer: D) -> Result<u32, D::Error>
|
||||
where
|
||||
D: serde::Deserializer<'de>,
|
||||
|
||||
Reference in New Issue
Block a user