From 374237972c7eacdd881abb07496417da64bc078a Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Wed, 29 Apr 2020 18:21:53 -0500 Subject: [PATCH] fasta/fai: Add writer --- noodles-fasta/src/fai.rs | 3 +- noodles-fasta/src/fai/writer.rs | 50 +++++++++++++++++++++++++++++++++ 2 files changed, 52 insertions(+), 1 deletion(-) create mode 100644 noodles-fasta/src/fai/writer.rs diff --git a/noodles-fasta/src/fai.rs b/noodles-fasta/src/fai.rs index 8dda1dcdc..0d4fd359d 100644 --- a/noodles-fasta/src/fai.rs +++ b/noodles-fasta/src/fai.rs @@ -1,4 +1,5 @@ mod reader; mod record; +mod writer; -pub use self::{reader::Reader, record::Record}; +pub use self::{reader::Reader, record::Record, writer::Writer}; diff --git a/noodles-fasta/src/fai/writer.rs b/noodles-fasta/src/fai/writer.rs new file mode 100644 index 000000000..a9b810520 --- /dev/null +++ b/noodles-fasta/src/fai/writer.rs @@ -0,0 +1,50 @@ +use std::io::{self, Write}; + +use super::Record; + +pub struct Writer { + inner: W, +} + +impl Writer +where + W: Write, +{ + pub fn new(inner: W) -> Self { + Self { inner } + } + + pub fn get_ref(&self) -> &W { + &self.inner + } + + pub fn write_record(&mut self, record: &Record) -> io::Result<()> { + writeln!( + self.inner, + "{name}\t{len}\t{offset}\t{line_bases}\t{line_width}", + name = record.name(), + len = record.len(), + offset = record.offset(), + line_bases = record.line_bases(), + line_width = record.line_width(), + ) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_write_record() -> io::Result<()> { + let mut writer = Writer::new(Vec::new()); + + let record = Record::new(String::from("sq0"), 10946, 4, 80, 81); + writer.write_record(&record)?; + + let expected = b"sq0\t10946\t4\t80\t81\n"; + assert_eq!(writer.get_ref(), expected); + + Ok(()) + } +}