From 73ca533d3d8a516d55c4f2f671d7d42798f72010 Mon Sep 17 00:00:00 2001 From: Michael Macias Date: Mon, 25 Nov 2024 11:48:46 -0600 Subject: [PATCH] gff/io/writer/line/record: Add score writer --- noodles-gff/src/io/writer/line/record.rs | 10 ++---- .../src/io/writer/line/record/score.rs | 34 +++++++++++++++++++ 2 files changed, 37 insertions(+), 7 deletions(-) create mode 100644 noodles-gff/src/io/writer/line/record/score.rs diff --git a/noodles-gff/src/io/writer/line/record.rs b/noodles-gff/src/io/writer/line/record.rs index ef44c92e7..988c23bee 100644 --- a/noodles-gff/src/io/writer/line/record.rs +++ b/noodles-gff/src/io/writer/line/record.rs @@ -1,8 +1,9 @@ mod position; +mod score; use std::io::{self, Write}; -use self::position::write_position; +use self::{position::write_position, score::write_score}; use crate::RecordBuf; pub(crate) fn write_record(writer: &mut W, record: &RecordBuf) -> io::Result<()> @@ -24,12 +25,7 @@ where write_position(writer, record.end())?; write_separator(writer)?; - if let Some(score) = record.score() { - write!(writer, "{score}")?; - } else { - write_missing(writer)?; - } - + write_score(writer, record.score())?; write_separator(writer)?; write!(writer, "{}", record.strand())?; diff --git a/noodles-gff/src/io/writer/line/record/score.rs b/noodles-gff/src/io/writer/line/record/score.rs new file mode 100644 index 000000000..5dbca6a71 --- /dev/null +++ b/noodles-gff/src/io/writer/line/record/score.rs @@ -0,0 +1,34 @@ +use std::io::{self, Write}; + +use super::write_missing; + +pub(super) fn write_score(writer: &mut W, score: Option) -> io::Result<()> +where + W: Write, +{ + if let Some(n) = score { + write!(writer, "{n}") + } else { + write_missing(writer) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_write_score() -> io::Result<()> { + let mut buf = Vec::new(); + + buf.clear(); + write_score(&mut buf, None)?; + assert_eq!(buf, b"."); + + buf.clear(); + write_score(&mut buf, Some(0.0))?; + assert_eq!(buf, b"0"); + + Ok(()) + } +}