noodles_fasta/io/writer/builder.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83
use std::{
fs::File,
io::{self, Write},
path::Path,
};
use super::Writer;
pub(crate) const DEFAULT_LINE_BASE_COUNT: usize = 80;
/// A FASTA writer builder.
pub struct Builder {
line_base_count: usize,
}
impl Builder {
/// Sets the number of bases per line.
///
/// By default, this is set to 80.
///
/// # Examples
///
/// ```
/// use noodles_fasta::io::writer::Builder;
/// let builder = Builder::default().set_line_base_count(100);
/// ```
pub fn set_line_base_count(mut self, line_base_count: usize) -> Self {
self.line_base_count = line_base_count;
self
}
/// Builds a FASTA writer from a path.
///
/// # Examples
///
/// ```no_run
/// use noodles_fasta::io::writer::Builder;
/// let writer = Builder::default().build_from_path("out.fa")?;
/// # Ok::<_, std::io::Error>(())
/// ```
pub fn build_from_path<P>(self, dst: P) -> io::Result<Writer<File>>
where
P: AsRef<Path>,
{
File::create(dst).map(|file| self.build_from_writer(file))
}
/// Builds a FASTA writer from a writer.
///
/// # Examples
///
/// ```
/// # use std::io;
/// use noodles_fasta::io::writer::Builder;
/// let writer = Builder::default().build_from_writer(io::sink());
/// ```
pub fn build_from_writer<W>(self, writer: W) -> Writer<W>
where
W: Write,
{
Writer {
inner: writer,
line_base_count: self.line_base_count,
}
}
/// Builds a FASTA writer from a writer.
#[deprecated(since = "0.43.0", note = "Use `Builder::build_from_writer` instead.")]
pub fn build_with_writer<W>(self, writer: W) -> Writer<W>
where
W: Write,
{
self.build_from_writer(writer)
}
}
impl Default for Builder {
fn default() -> Self {
Self {
line_base_count: DEFAULT_LINE_BASE_COUNT,
}
}
}