noline/
builder.rs

1//! Builder for editors
2
3use core::marker::PhantomData;
4
5use crate::{
6    async_editor,
7    error::NolineError,
8    history::{History, NoHistory, SliceHistory},
9    line_buffer::{Buffer, LineBuffer, NoBuffer, SliceBuffer},
10    sync_editor,
11};
12
13#[cfg(any(test, doc, feature = "alloc", feature = "std"))]
14use crate::{history::UnboundedHistory, line_buffer::UnboundedBuffer};
15
16/// Builder for [`sync_editor::Editor`] and [`async_editor::Editor`].
17///
18/// # Example
19/// ```no_run
20/// # use embedded_io::{Read, Write, ErrorType};
21/// # use core::convert::Infallible;
22/// # struct MyIO {}
23/// # impl ErrorType for MyIO {
24/// #     type Error = Infallible;
25/// # }
26/// # impl embedded_io::Write for MyIO {
27/// #     fn write(&mut self, buf: &[u8]) -> Result<usize, Self::Error> { unimplemented!() }
28/// #     fn flush(&mut self) -> Result<(), Self::Error> { unimplemented!() }
29/// # }
30/// # impl embedded_io::Read for MyIO {
31/// #     fn read(&mut self, buf: &mut[u8]) -> Result<usize, Self::Error> { unimplemented!() }
32/// # }
33/// # let mut io = MyIO {};
34/// use noline::builder::EditorBuilder;
35///
36/// let mut buffer = [0; 100];
37/// let mut history = [0; 200];
38/// let mut editor = EditorBuilder::from_slice(&mut buffer)
39///     .with_slice_history(&mut history)
40///     .build_sync(&mut io)
41///     .unwrap();
42/// ```
43pub struct EditorBuilder<B: Buffer, H: History> {
44    line_buffer: LineBuffer<B>,
45    history: H,
46    _marker: PhantomData<(B, H)>,
47}
48
49impl EditorBuilder<NoBuffer, NoHistory> {
50    /// Create builder for editor with static buffer
51    ///
52    /// # Example
53    /// ```
54    /// use noline::builder::EditorBuilder;
55    ///
56    /// let mut buffer = [0; 100];
57    /// let builder = EditorBuilder::from_slice(&mut buffer);
58    /// ```
59    pub fn from_slice(buffer: &mut [u8]) -> EditorBuilder<SliceBuffer<'_>, NoHistory> {
60        EditorBuilder {
61            line_buffer: LineBuffer::from_slice(buffer),
62            history: NoHistory {},
63            _marker: PhantomData,
64        }
65    }
66
67    #[cfg(any(test, doc, feature = "alloc", feature = "std"))]
68    /// Create builder for editor with unbounded buffer
69    ///
70    /// # Example
71    /// ```
72    /// use noline::builder::EditorBuilder;
73    ///
74    /// let builder = EditorBuilder::new_unbounded();
75    /// ```
76    pub fn new_unbounded() -> EditorBuilder<UnboundedBuffer, NoHistory> {
77        EditorBuilder {
78            line_buffer: LineBuffer::new_unbounded(),
79            history: NoHistory {},
80            _marker: PhantomData,
81        }
82    }
83}
84
85impl<B: Buffer, H: History> EditorBuilder<B, H> {
86    /// Add static history
87    pub fn with_slice_history(self, buffer: &mut [u8]) -> EditorBuilder<B, SliceHistory<'_>> {
88        EditorBuilder {
89            line_buffer: self.line_buffer,
90            history: SliceHistory::new(buffer),
91            _marker: PhantomData,
92        }
93    }
94
95    #[cfg(any(test, feature = "alloc", feature = "std"))]
96    /// Add unbounded history
97    pub fn with_unbounded_history(self) -> EditorBuilder<B, UnboundedHistory> {
98        EditorBuilder {
99            line_buffer: self.line_buffer,
100            history: UnboundedHistory::new(),
101            _marker: PhantomData,
102        }
103    }
104
105    /// Build [`sync_editor::Editor`]. Is equivalent of calling [`sync_editor::Editor::new()`].
106    pub fn build_sync<IO: embedded_io::Read + embedded_io::Write>(
107        self,
108        io: &mut IO,
109    ) -> Result<sync_editor::Editor<B, H>, NolineError> {
110        sync_editor::Editor::new(self.line_buffer, self.history, io)
111    }
112
113    /// Build [`async_editor::Editor`]. Is equivalent of calling [`async_editor::Editor::new()`].
114    pub async fn build_async<IO: embedded_io_async::Read + embedded_io_async::Write>(
115        self,
116        io: &mut IO,
117    ) -> Result<async_editor::Editor<B, H>, NolineError> {
118        async_editor::Editor::new(self.line_buffer, self.history, io).await
119    }
120}