Initial Release

This commit is contained in:
Michael Aaron Murphy 2021-08-10 01:04:20 +02:00
commit 8b3b95aae8
54 changed files with 5601 additions and 0 deletions

27
src/codec.rs Normal file
View file

@ -0,0 +1,27 @@
use futures_codec::{FramedRead, LinesCodec};
use futures_lite::{AsyncRead, Stream, StreamExt};
use serde::Deserialize;
use smol::Unblock;
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>,
{
FramedRead::new(input, LinesCodec)
.take_while(Result::is_ok)
.map(Result::unwrap)
.map(|line| serde_json::from_str::<S>(&line))
}