ripgrep-all/src/recurse.rs

63 lines
2.0 KiB
Rust
Raw Normal View History

2022-10-29 18:54:05 +00:00
use tokio_util::io::{ReaderStream, StreamReader};
2020-09-30 15:26:42 +00:00
use crate::{adapted_iter::AdaptedFilesIterBox, adapters::*};
2022-10-29 18:54:05 +00:00
use async_stream::stream;
use tokio_stream::StreamExt;
2020-09-30 14:22:54 +00:00
2022-10-29 21:56:25 +00:00
pub struct RecursingConcattyReader {
inp: AdaptedFilesIterBox,
cur: Option<ReadBox>,
2020-09-30 14:22:54 +00:00
}
2022-10-29 18:54:05 +00:00
pub fn concat_read_streams(
2022-10-29 21:56:25 +00:00
mut input: AdaptedFilesIterBox,
) -> ReadBox {
2022-10-29 18:54:05 +00:00
let s = stream! {
while let Some(output) = input.next() {
let mut stream = ReaderStream::new(output.inp);
while let Some(bytes) = stream.next().await {
yield bytes;
}
}
};
Box::pin(StreamReader::new(s))
}
/*
2020-09-30 14:22:54 +00:00
impl<'a> RecursingConcattyReader<'a> {
2022-10-29 18:54:05 +00:00
pub fn concat(inp: AdaptedFilesIterBox<'a>) -> anyhow::Result<ReadBox<'a>> {
2020-09-30 14:22:54 +00:00
let mut r = RecursingConcattyReader { inp, cur: None };
r.ascend()?;
Ok(Box::new(r))
}
2022-10-26 15:21:39 +00:00
pub fn ascend(&mut self) -> anyhow::Result<()> {
2020-09-30 14:22:54 +00:00
let inp = &mut self.inp;
// get next inner file from inp
// we only need to access the inp: ReadIter when the inner reader is done, so this should be safe
let ai = unsafe {
// would love to make this safe, but how? something like OwnedRef<inp, cur>
2020-09-30 15:26:42 +00:00
(*(inp as *mut AdaptedFilesIterBox<'a>)).next()
2020-09-30 14:22:54 +00:00
};
self.cur = match ai {
Some(ai) => Some(rga_preproc(ai)?),
None => None,
};
Ok(())
}
}
2022-10-29 18:54:05 +00:00
impl<'a> AsyncRead for RecursingConcattyReader<'a> {
2020-09-30 14:22:54 +00:00
fn read(&mut self, buf: &mut [u8]) -> std::io::Result<usize> {
match &mut self.cur {
None => Ok(0), // last file ended
Some(cur) => match cur.read(buf) {
Err(e) => Err(e),
Ok(0) => {
// current file ended, go to next file
self.ascend()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::Other, e))?;
self.read(buf)
}
Ok(n) => Ok(n),
},
}
}
}
2022-10-29 18:54:05 +00:00
*/