noodles_fastq/io/writer/
builder.rs

1use std::{
2    fs::File,
3    io::{self, BufWriter, Write},
4    path::Path,
5};
6
7use super::{Writer, DEFAULT_DEFINITION_SEPARATOR};
8
9/// A FASTQ writer builder.
10pub struct Builder {
11    definition_separator: u8,
12}
13
14impl Builder {
15    /// Sets the definition separator.
16    ///
17    /// By default, this is a space (` `).
18    ///
19    /// # Examples
20    ///
21    /// ```
22    /// use noodles_fastq::io::writer::Builder;
23    /// let builder = Builder::default().set_definition_separator(b'\t');
24    /// ```
25    pub fn set_definition_separator(mut self, definition_separator: u8) -> Self {
26        self.definition_separator = definition_separator;
27        self
28    }
29
30    /// Builds a FASTQ writer from a path.
31    ///
32    /// # Examples
33    ///
34    /// ```no_run
35    /// use noodles_fastq::io::writer::Builder;
36    /// let writer = Builder::default().build_from_path("out.fq")?;
37    /// # Ok::<_, std::io::Error>(())
38    /// ```
39    pub fn build_from_path<P>(self, dst: P) -> io::Result<Writer<Box<dyn Write>>>
40    where
41        P: AsRef<Path>,
42    {
43        let writer = File::create(dst).map(BufWriter::new)?;
44        Ok(self.build_from_writer(writer))
45    }
46
47    /// Builds a FASTQ writer from a writer.
48    ///
49    /// # Examples
50    ///
51    /// ```no_run
52    /// # use std::io;
53    /// use noodles_fastq::io::writer::Builder;
54    /// let writer = Builder::default().build_from_writer(io::sink());
55    /// # Ok::<_, io::Error>(())
56    /// ```
57    pub fn build_from_writer<W>(self, writer: W) -> Writer<Box<dyn Write>>
58    where
59        W: Write + 'static,
60    {
61        Writer {
62            inner: Box::new(writer),
63            definition_separator: self.definition_separator,
64        }
65    }
66}
67
68impl Default for Builder {
69    fn default() -> Self {
70        Self {
71            definition_separator: DEFAULT_DEFINITION_SEPARATOR,
72        }
73    }
74}
75
76#[cfg(test)]
77mod tests {
78    use super::*;
79
80    #[test]
81    fn test_default() {
82        let builder = Builder::default();
83        assert_eq!(builder.definition_separator, b' ');
84    }
85}