Skip to content

Commit

Permalink
fasta/fai: Add writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Apr 29, 2020
1 parent 3b964c4 commit 3742379
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 1 deletion.
3 changes: 2 additions & 1 deletion noodles-fasta/src/fai.rs
Original file line number Diff line number Diff line change
@@ -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};
50 changes: 50 additions & 0 deletions noodles-fasta/src/fai/writer.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use std::io::{self, Write};

use super::Record;

pub struct Writer<W> {
inner: W,
}

impl<W> Writer<W>
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(())
}
}

0 comments on commit 3742379

Please sign in to comment.