Skip to content

Commit

Permalink
vcf/writer: Add builder
Browse files Browse the repository at this point in the history
  • Loading branch information
zaeleus committed Aug 29, 2023
1 parent 7dfd82f commit bbef74d
Show file tree
Hide file tree
Showing 4 changed files with 49 additions and 1 deletion.
4 changes: 4 additions & 0 deletions noodles-vcf/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,10 @@

## Unreleased

### Added

* vcf/writer: Add builder (`vcf::writer::Builder`).

### Fixed

* vcf/record/genotypes/keys/key: Hash inner key.
Expand Down
2 changes: 1 addition & 1 deletion noodles-vcf/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub mod reader;
pub mod record;
mod variant_reader;
mod variant_writer;
mod writer;
pub mod writer;

pub use self::{
header::Header, indexed_reader::IndexedReader, reader::Reader, record::Record,
Expand Down
4 changes: 4 additions & 0 deletions noodles-vcf/src/writer.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,11 @@
//! VCF writer.
mod builder;
mod record;

use std::io::{self, Write};

pub use self::builder::Builder;
use self::record::write_record;
use super::{Header, Record, VariantWriter};

Expand Down
40 changes: 40 additions & 0 deletions noodles-vcf/src/writer/builder.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,40 @@
use std::{
fs::File,
io::{self, BufWriter, Write},
path::Path,
};

use noodles_bgzf as bgzf;

use super::Writer;

/// A BAM writer builder.
#[derive(Debug, Default)]
pub struct Builder;

impl Builder {
/// Builds a VCF writer from a path.
///
/// # Examples
///
/// ```no_run
/// use noodles_vcf as vcf;
/// let writer = vcf::writer::Builder.build_from_path("out.vcf")?;
/// # Ok::<_, std::io::Error>(())
/// ```
pub fn build_from_path<P>(self, dst: P) -> io::Result<Writer<Box<dyn Write>>>
where
P: AsRef<Path>,
{
let dst = dst.as_ref();

let file = File::create(dst)?;

let writer: Box<dyn Write> = match dst.extension().and_then(|ext| ext.to_str()) {
Some("gz" | "bgz") => Box::new(bgzf::Writer::new(file)),
_ => Box::new(BufWriter::new(file)),
};

Ok(Writer::new(writer))
}
}

0 comments on commit bbef74d

Please sign in to comment.