Skip to content

Commit

Permalink
gff/io/writer/line/record: Add score writer
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Nov 25, 2024
1 parent 3c789e0 commit 73ca533
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 7 deletions.
10 changes: 3 additions & 7 deletions noodles-gff/src/io/writer/line/record.rs
Original file line number Diff line number Diff line change
@@ -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<W>(writer: &mut W, record: &RecordBuf) -> io::Result<()>
Expand All @@ -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())?;
Expand Down
34 changes: 34 additions & 0 deletions noodles-gff/src/io/writer/line/record/score.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
use std::io::{self, Write};

use super::write_missing;

pub(super) fn write_score<W>(writer: &mut W, score: Option<f32>) -> 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(())
}
}

0 comments on commit 73ca533

Please sign in to comment.