Factor StdinStream out of dump applet
This commit is contained in:
parent
9b1e61ff80
commit
e61244bce3
2 changed files with 50 additions and 19 deletions
|
@ -1,14 +1,12 @@
|
|||
use std::{
|
||||
error::Error,
|
||||
io::{self, prelude::*}
|
||||
io
|
||||
};
|
||||
|
||||
use clap::{App, AppSettings, ArgMatches, SubCommand};
|
||||
use futures::{
|
||||
Async,
|
||||
stream::poll_fn
|
||||
};
|
||||
use futures::Async;
|
||||
|
||||
use super::StdinStream;
|
||||
use webmetro::{
|
||||
stream_parser::StreamEbml,
|
||||
webm::{
|
||||
|
@ -26,20 +24,7 @@ pub fn options() -> App<'static, 'static> {
|
|||
pub fn run(_args: &ArgMatches) -> Result<(), Box<Error>> {
|
||||
|
||||
let stdin = io::stdin();
|
||||
let mut buf_reader = stdin.lock();
|
||||
let mut read_bytes = 0;
|
||||
|
||||
let mut events = poll_fn(|| {
|
||||
buf_reader.consume(read_bytes);
|
||||
buf_reader.fill_buf().map(|slice| {
|
||||
read_bytes = slice.len();
|
||||
if read_bytes > 0 {
|
||||
Async::Ready(Some(Into::<Vec<u8>>::into(slice)))
|
||||
} else {
|
||||
Async::Ready(None)
|
||||
}
|
||||
})
|
||||
}).parse_ebml();
|
||||
let mut events = StdinStream::new(stdin.lock()).parse_ebml();
|
||||
|
||||
// stdin is sync so Async::NotReady will never happen
|
||||
while let Ok(Async::Ready(Some(element))) = events.poll_event() {
|
||||
|
|
|
@ -1,2 +1,48 @@
|
|||
use std::io::{
|
||||
Error as IoError,
|
||||
StdinLock,
|
||||
prelude::*
|
||||
};
|
||||
|
||||
use futures::{
|
||||
Async,
|
||||
stream::Stream
|
||||
};
|
||||
|
||||
pub mod dump;
|
||||
pub mod relay;
|
||||
|
||||
/// A hackish adapter that makes chunks of bytes from stdin available as a Stream;
|
||||
/// is NOT actually async, and just uses blocking read. Buffers aren't optimized either
|
||||
/// and copy more than necessary.
|
||||
pub struct StdinStream<'a> {
|
||||
buf_reader: StdinLock<'a>,
|
||||
read_bytes: usize
|
||||
}
|
||||
|
||||
impl<'a> StdinStream<'a> {
|
||||
pub fn new(lock: StdinLock<'a>) -> Self {
|
||||
StdinStream {
|
||||
buf_reader: lock,
|
||||
read_bytes: 0
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> Stream for StdinStream<'a> {
|
||||
type Item = Vec<u8>;
|
||||
type Error = IoError;
|
||||
|
||||
fn poll(&mut self) -> Result<Async<Option<Self::Item>>, Self::Error> {
|
||||
self.buf_reader.consume(self.read_bytes);
|
||||
let read_bytes = &mut self.read_bytes;
|
||||
self.buf_reader.fill_buf().map(|slice| {
|
||||
*read_bytes = slice.len();
|
||||
if *read_bytes > 0 {
|
||||
Async::Ready(Some(Into::<Vec<u8>>::into(slice)))
|
||||
} else {
|
||||
Async::Ready(None)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue