noodles_fasta/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
mod sequence;
use tokio::io::{
self, AsyncBufRead, AsyncBufReadExt, AsyncRead, AsyncSeek, AsyncSeekExt, SeekFrom,
};
use self::sequence::read_sequence;
/// An async FASTA reader.
pub struct Reader<R> {
inner: R,
}
impl<R> Reader<R> {
/// Returns a reference to the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_fasta as fasta;
/// use tokio::io;
/// let reader = fasta::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_fasta as fasta;
/// use tokio::io;
/// let mut reader = fasta::r#async::io::Reader::new(io::empty());
/// let _inner = reader.get_mut();
/// ```
pub fn get_mut(&mut self) -> &mut R {
&mut self.inner
}
/// Returns the underlying reader.
///
/// # Examples
///
/// ```
/// use noodles_fasta as fasta;
/// use tokio::io;
/// let reader = fasta::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 FASTA reader.
///
/// # Examples
///
/// ```
/// use noodles_fasta as fasta;
/// let data = [];
/// let mut reader = fasta::r#async::io::Reader::new(&data[..]);
/// ```
pub fn new(inner: R) -> Self {
Self { inner }
}
/// Reads a raw definition line.
///
/// # Examples
///
/// ```
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_fasta as fasta;
///
/// let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
/// let mut reader = fasta::r#async::io::Reader::new(&data[..]);
///
/// let mut buf = String::new();
/// reader.read_definition(&mut buf).await?;
///
/// assert_eq!(buf, ">sq0");
/// # Ok(())
/// # }
/// ```
pub async fn read_definition(&mut self, buf: &mut String) -> io::Result<usize> {
read_line(&mut self.inner, buf).await
}
/// Reads a sequence.
///
/// # Examples
///
/// ```
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_fasta as fasta;
///
/// let data = b">sq0\nACGT\n>sq1\nNNNN\nNNNN\nNN\n";
/// let mut reader = fasta::r#async::io::Reader::new(&data[..]);
/// reader.read_definition(&mut String::new()).await?;
///
/// let mut buf = Vec::new();
/// reader.read_sequence(&mut buf).await?;
///
/// assert_eq!(buf, b"ACGT");
/// # Ok(())
/// # }
/// ```
pub async fn read_sequence(&mut self, buf: &mut Vec<u8>) -> io::Result<usize> {
read_sequence(&mut self.inner, buf).await
}
}
impl<R> Reader<R>
where
R: AsyncRead + AsyncSeek + Unpin,
{
/// Seeks the underlying stream to the given position.
pub async fn seek(&mut self, pos: SeekFrom) -> io::Result<u64> {
self.inner.seek(pos).await
}
}
pub(crate) 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? {
0 => Ok(0),
n => {
if buf.ends_with(LINE_FEED) {
buf.pop();
if buf.ends_with(CARRIAGE_RETURN) {
buf.pop();
}
}
Ok(n)
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_read_definition() -> io::Result<()> {
let data = b">sq0\nACGT\n";
let mut reader = Reader::new(&data[..]);
let mut buf = String::new();
reader.read_definition(&mut buf).await?;
assert_eq!(buf, ">sq0");
Ok(())
}
#[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(())
}
}