2021-08-15 13:16:55 +02:00
|
|
|
use blocking::Unblock;
|
2021-08-26 18:42:54 +02:00
|
|
|
use futures_lite::{AsyncBufReadExt, AsyncRead, Stream, StreamExt};
|
2021-08-10 01:04:20 +02:00
|
|
|
use serde::Deserialize;
|
|
|
|
|
use std::io;
|
|
|
|
|
|
|
|
|
|
/// stdin with AsyncRead support
|
|
|
|
|
pub fn async_stdin() -> Unblock<io::Stdin> {
|
|
|
|
|
Unblock::new(io::stdin())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// stdout with AsyncWrite support
|
|
|
|
|
pub fn async_stdout() -> Unblock<io::Stdout> {
|
|
|
|
|
Unblock::new(io::stdout())
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/// Creates a stream that parses JSON input line-by-line
|
|
|
|
|
pub fn json_input_stream<I, S>(input: I) -> impl Stream<Item = serde_json::Result<S>> + Unpin + Send
|
|
|
|
|
where
|
|
|
|
|
I: AsyncRead + Unpin + Send,
|
|
|
|
|
S: for<'a> Deserialize<'a>,
|
|
|
|
|
{
|
2021-08-26 18:42:54 +02:00
|
|
|
futures_lite::io::BufReader::new(input)
|
|
|
|
|
.lines()
|
2021-08-10 01:04:20 +02:00
|
|
|
.take_while(Result::is_ok)
|
|
|
|
|
.map(Result::unwrap)
|
|
|
|
|
.map(|line| serde_json::from_str::<S>(&line))
|
|
|
|
|
}
|