noodles_gff/async/io/reader.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
mod lazy_line;
use futures::{stream, Stream, TryStreamExt};
use tokio::io::{self, AsyncBufRead, AsyncBufReadExt};
use self::lazy_line::read_lazy_line;
use crate::{lazy, Directive, Line, Record};
/// An async GFF reader.
pub struct Reader<R> {
inner: R,
buf: String,
}
impl<R> Reader<R> {
/// Returns a reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// use tokio::io;
/// let reader = gff::r#async::io::Reader::new(io::empty());
/// let _inner = reader.get_ref();
/// ```
pub fn get_ref(&self) -> &R {
&self.inner
}
/// Returns a mutable reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// use tokio::io;
/// let mut reader = gff::r#async::io::Reader::new(io::empty());
/// let _inner = reader.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Unwraps and returns the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// use tokio::io;
/// let reader = gff::r#async::io::Reader::new(io::empty());
/// let _inner = reader.into_inner();
/// ```
pub fn into_inner(self) -> R {
self.inner
}
}
impl<R> Reader<R>
where
R: AsyncBufRead + Unpin,
{
/// Creates an async GFF reader.
///
/// # Examples
///
/// ```
/// use noodles_gff as gff;
/// use tokio::io;
/// let reader = gff::r#async::io::Reader::new(io::empty());
/// ```
pub fn new(inner: R) -> Self {
Self {
inner,
buf: String::new(),
}
}
/// Reads a raw GFF line.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_gff as gff;
///
/// let data = b"##gff-version 3\n";
/// let mut reader = gff::r#async::io::Reader::new(&data[..]);
///
/// let mut buf = String::new();
/// reader.read_line(&mut buf).await?;
/// assert_eq!(buf, "##gff-version 3");
/// # Ok(())
/// # }
/// ```
pub async fn read_line(&mut self, buf: &mut String) -> io::Result<usize> {
read_line(&mut self.inner, buf).await
}
/// Reads a lazy line.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_gff::{self as gff, lazy};
///
/// let data = b"##gff-version 3\n";
/// let mut reader = gff::r#async::io::Reader::new(&data[..]);
///
/// let mut line = lazy::Line::default();
/// reader.read_lazy_line(&mut line).await?;
/// assert_eq!(line, lazy::Line::Directive(String::from("##gff-version 3")));
/// # Ok(())
/// # }
/// ```
pub async fn read_lazy_line(&mut self, line: &mut lazy::Line) -> io::Result<usize> {
read_lazy_line(&mut self.inner, &mut self.buf, line).await
}
/// Returns a stream over lines.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_gff as gff;
/// use futures::TryStreamExt;
///
/// let data = b"##gff-version 3\n";
/// let mut reader = gff::r#async::io::Reader::new(&data[..]);
/// let mut lines = reader.lines();
///
/// let line = lines.try_next().await?;
/// assert!(matches!(line, Some(gff::Line::Directive(_))));
///
/// assert!(lines.try_next().await?.is_none());
/// # Ok(())
/// # }
/// ```
pub fn lines(&mut self) -> impl Stream<Item = io::Result<Line>> + '_ {
Box::pin(stream::try_unfold(
(self, String::new()),
|(reader, mut buf)| async {
buf.clear();
reader.read_line(&mut buf).await.and_then(|n| match n {
0 => Ok(None),
_ => match buf.parse() {
Ok(line) => Ok(Some((line, (reader, buf)))),
Err(e) => Err(io::Error::new(io::ErrorKind::InvalidData, e)),
},
})
},
))
}
/// Returns a stream over records.
///
/// # Examples
///
/// ```
/// # #[tokio::main]
/// # async fn main() -> tokio::io::Result<()> {
/// use noodles_gff as gff;
/// use futures::TryStreamExt;
///
/// let data = b"##gff-version 3\n";
/// let mut reader = gff::r#async::io::Reader::new(&data[..]);
/// let mut records = reader.records();
///
/// assert!(records.try_next().await?.is_none());
/// # Ok(())
/// # }
/// ```
pub fn records(&mut self) -> impl Stream<Item = io::Result<Record>> + '_ {
Box::pin(stream::try_unfold(self.lines(), |mut lines| async {
loop {
match lines.try_next().await? {
None | Some(Line::Directive(Directive::StartOfFasta)) => return Ok(None),
Some(Line::Record(record)) => return Ok(Some((record, lines))),
_ => {}
}
}
}))
}
}
async fn read_line<R>(reader: &mut R, buf: &mut String) -> io::Result<usize>
where
R: AsyncBufRead + Unpin,
{
const LINE_FEED: char = '\n';
const CARRIAGE_RETURN: char = '\r';
match reader.read_line(buf).await {
Ok(0) => Ok(0),
Ok(n) => {
if buf.ends_with(LINE_FEED) {
buf.pop();
if buf.ends_with(CARRIAGE_RETURN) {
buf.pop();
}
}
Ok(n)
}
Err(e) => Err(e),
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_read_line() -> io::Result<()> {
async fn t(buf: &mut String, mut data: &[u8], expected: &str) -> io::Result<()> {
buf.clear();
read_line(&mut data, buf).await?;
assert_eq!(buf, expected);
Ok(())
}
let mut buf = String::new();
t(&mut buf, b"noodles\n", "noodles").await?;
t(&mut buf, b"noodles\r\n", "noodles").await?;
t(&mut buf, b"noodles", "noodles").await?;
Ok(())
}
}