use core::marker::PhantomData;
use crate::{
async_editor,
error::NolineError,
history::{History, NoHistory, SliceHistory},
line_buffer::{Buffer, LineBuffer, NoBuffer, SliceBuffer},
sync_editor,
};
#[cfg(any(test, doc, feature = "alloc", feature = "std"))]
use crate::{history::UnboundedHistory, line_buffer::UnboundedBuffer};
pub struct EditorBuilder<B: Buffer, H: History> {
line_buffer: LineBuffer<B>,
history: H,
_marker: PhantomData<(B, H)>,
}
impl EditorBuilder<NoBuffer, NoHistory> {
pub fn from_slice(buffer: &mut [u8]) -> EditorBuilder<SliceBuffer<'_>, NoHistory> {
EditorBuilder {
line_buffer: LineBuffer::from_slice(buffer),
history: NoHistory {},
_marker: PhantomData,
}
}
#[cfg(any(test, doc, feature = "alloc", feature = "std"))]
pub fn new_unbounded() -> EditorBuilder<UnboundedBuffer, NoHistory> {
EditorBuilder {
line_buffer: LineBuffer::new_unbounded(),
history: NoHistory {},
_marker: PhantomData,
}
}
}
impl<B: Buffer, H: History> EditorBuilder<B, H> {
pub fn with_slice_history(self, buffer: &mut [u8]) -> EditorBuilder<B, SliceHistory<'_>> {
EditorBuilder {
line_buffer: self.line_buffer,
history: SliceHistory::new(buffer),
_marker: PhantomData,
}
}
#[cfg(any(test, feature = "alloc", feature = "std"))]
pub fn with_unbounded_history(self) -> EditorBuilder<B, UnboundedHistory> {
EditorBuilder {
line_buffer: self.line_buffer,
history: UnboundedHistory::new(),
_marker: PhantomData,
}
}
pub fn build_sync<IO: embedded_io::Read + embedded_io::Write>(
self,
io: &mut IO,
) -> Result<sync_editor::Editor<B, H>, NolineError> {
sync_editor::Editor::new(self.line_buffer, self.history, io)
}
pub async fn build_async<IO: embedded_io_async::Read + embedded_io_async::Write>(
self,
io: &mut IO,
) -> Result<async_editor::Editor<B, H>, NolineError> {
async_editor::Editor::new(self.line_buffer, self.history, io).await
}
}