noodles_cram/crai/async/writer.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
use async_compression::tokio::write::GzipEncoder;
use tokio::io::{self, AsyncWrite, AsyncWriteExt};
use crate::crai::Record;
/// An async CRAM index writer.
pub struct Writer<W> {
inner: GzipEncoder<W>,
}
impl<W> Writer<W>
where
W: AsyncWrite + Unpin,
{
/// Creates an async CRAM index writer.
///
/// # Examples
///
/// ```
/// use noodles_cram::crai;
/// let writer = crai::AsyncWriter::new(Vec::new());
/// ```
pub fn new(inner: W) -> Self {
Self {
inner: GzipEncoder::new(inner),
}
}
/// Returns the underlying writer.
///
/// # Examples
///
/// ```
/// use noodles_cram::crai;
/// let writer = crai::AsyncWriter::new(Vec::new());
/// assert!(writer.into_inner().is_empty());
/// ```
pub fn into_inner(self) -> W {
self.inner.into_inner()
}
/// Shuts down the output stream.
///
/// # Examples
///
/// ```
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_cram::crai;
/// let mut writer = crai::AsyncWriter::new(Vec::new());
/// writer.shutdown().await?;
/// # Ok(())
/// # }
/// ```
pub async fn shutdown(&mut self) -> io::Result<()> {
self.inner.shutdown().await
}
/// Writes a CRAM index.
///
/// # Examples
///
/// ```
/// # use std::io;
/// #
/// # #[tokio::main]
/// # async fn main() -> io::Result<()> {
/// use noodles_core::Position;
/// use noodles_cram::crai;
///
/// let mut writer = crai::AsyncWriter::new(Vec::new());
///
/// let index = vec![crai::Record::new(
/// Some(0),
/// Position::new(10946),
/// 6765,
/// 17711,
/// 233,
/// 317811,
/// )];
///
/// writer.write_index(&index).await?;
/// # Ok(())
/// # }
/// ```
pub async fn write_index(&mut self, index: &[Record]) -> io::Result<()> {
write_index(&mut self.inner, index).await
}
}
async fn write_index<W>(writer: &mut W, index: &[Record]) -> io::Result<()>
where
W: AsyncWrite + Unpin,
{
for record in index {
write_record(writer, record).await?;
}
Ok(())
}
async fn write_record<W>(writer: &mut W, record: &Record) -> io::Result<()>
where
W: AsyncWrite + Unpin,
{
const LINE_FEED: u8 = b'\n';
writer.write_all(record.to_string().as_bytes()).await?;
writer.write_all(&[LINE_FEED]).await?;
Ok(())
}
#[cfg(test)]
mod tests {
use super::*;
#[tokio::test]
async fn test_write_record() -> Result<(), Box<dyn std::error::Error>> {
use noodles_core::Position;
let mut buf = Vec::new();
let record = Record::new(Some(0), Position::new(10946), 6765, 17711, 233, 317811);
write_record(&mut buf, &record).await?;
let expected = b"0\t10946\t6765\t17711\t233\t317811\n";
assert_eq!(buf, expected);
Ok(())
}
}