Skip to content

Commit

Permalink
vcf/io/reader: Add a header reader adapter
Browse files Browse the repository at this point in the history
This can be used to read the raw VCF header.

`header::Reader` existed previously, but this increases its visibility
and creates a public constructor for it.
  • Loading branch information
zaeleus committed Dec 18, 2024
1 parent 152be23 commit 1162cd6
Show file tree
Hide file tree
Showing 3 changed files with 51 additions and 3 deletions.
8 changes: 8 additions & 0 deletions noodles-vcf/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,13 @@
# Changelog

## Unreleased

### Added

* vcf/io/reader: Add a header reader adapter (`header::Reader`).

This can be used to read the raw VCF header.

## 0.69.0 - 2024-12-12

### Changed
Expand Down
37 changes: 36 additions & 1 deletion noodles-vcf/src/io/reader.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! VCF reader and iterators.
mod builder;
mod header;
pub mod header;
pub(crate) mod query;
pub(crate) mod record;
pub mod record_buf;
Expand Down Expand Up @@ -121,6 +121,41 @@ where
}
}

/// Returns a VCF header reader.
///
/// This creates an adapter that reads at most the length of the header, i.e., all lines
/// prefixed with a `#` (number sign).
///
/// It is more ergonomic to read and parse the header using [`Self::read_header`], but using
/// this adapter allows for control of how the header is read, e.g., to read the raw VCF
/// header.
///
/// The position of the stream is expected to be at the start.
///
/// # Examples
///
/// ```
/// # use std::io::Read;
/// use noodles_vcf as vcf;
///
/// let data = b"##fileformat=VCFv4.3
/// #CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO
/// sq0\t1\t.\tA\t.\t.\tPASS\t.
/// ";
///
/// let mut reader = vcf::io::Reader::new(&data[..]);
/// let mut header_reader = reader.header_reader();
///
/// let mut raw_header = String::new();
/// header_reader.read_to_string(&mut raw_header)?;
///
/// assert_eq!(raw_header, "##fileformat=VCFv4.3\n#CHROM\tPOS\tID\tREF\tALT\tQUAL\tFILTER\tINFO\n");
/// # Ok::<_, std::io::Error>(())
/// ```
pub fn header_reader(&mut self) -> header::Reader<'_, R> {
header::Reader::new(&mut self.inner)
}

/// Reads the VCF header.
///
/// This reads all header lines prefixed with a `#` (number sign), which includes the header
Expand Down
9 changes: 7 additions & 2 deletions noodles-vcf/src/io/reader/header.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,19 @@
//! VCF header reader.
use std::io::{self, BufRead, Read};

use crate::{header, Header};

struct Reader<'r, R> {
/// A VCF header reader.
///
/// This is created by calling [`super::Reader::header_reader`].
pub struct Reader<'r, R> {
inner: &'r mut R,
is_eol: bool,
}

impl<'r, R> Reader<'r, R> {
fn new(inner: &'r mut R) -> Self {
pub(super) fn new(inner: &'r mut R) -> Self {
Self {
inner,
is_eol: true,
Expand Down

0 comments on commit 1162cd6

Please sign in to comment.