pop-launcher/src/codec.rs

28 lines
767 B
Rust
Raw Normal View History

use blocking::Unblock;
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>,
{
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))
}