noodles_fasta/async/io/writer/
builder.rs

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