pub trait IO {
// Required methods
fn run_command(
&mut self,
ui: &mut UILock<'_>,
command: String
) -> Result<(), IOError>;
fn run_read_command(
&mut self,
ui: &mut UILock<'_>,
command: String
) -> Result<String, IOError>;
fn run_write_command(
&mut self,
ui: &mut UILock<'_>,
command: String,
input: LinesIter<'_>
) -> Result<usize, IOError>;
fn run_transform_command(
&mut self,
ui: &mut UILock<'_>,
command: String,
input: LinesIter<'_>
) -> Result<String, IOError>;
fn write_file(
&mut self,
path: &str,
append: bool,
data: LinesIter<'_>
) -> Result<usize, IOError>;
fn read_file(
&mut self,
path: &str,
must_exist: bool
) -> Result<String, IOError>;
}
Expand description
Trait that abstracts file interactions and running shell commands
Intended to allow modifying how and where system interactions occur. Example cases for replacing this:
- Dummy IO to prevent filesystem modifications while testing.
- SSH forwarding to save to remote system and run commands remotely.
- Restricted IO to forbid command running and restrict file paths.
Required Methods§
sourcefn run_command(
&mut self,
ui: &mut UILock<'_>,
command: String
) -> Result<(), IOError>
fn run_command( &mut self, ui: &mut UILock<'_>, command: String ) -> Result<(), IOError>
Run a lone command (unrelated from the buffer)
Stdin, Stdout and Stderr passed through to UI
sourcefn run_read_command(
&mut self,
ui: &mut UILock<'_>,
command: String
) -> Result<String, IOError>
fn run_read_command( &mut self, ui: &mut UILock<'_>, command: String ) -> Result<String, IOError>
Run a read command, collecting stdout to add into buffer
Stdin and Stderr should be passed through to UI
The returned string will be split into lines and added into the buffer. All line endings are converted into ‘\n’ when adding into the buffer.
sourcefn run_write_command(
&mut self,
ui: &mut UILock<'_>,
command: String,
input: LinesIter<'_>
) -> Result<usize, IOError>
fn run_write_command( &mut self, ui: &mut UILock<'_>, command: String, input: LinesIter<'_> ) -> Result<usize, IOError>
Run a write command, receiving part of buffer via stdin
Stdout and Stderr should be passed through to UI Returns number of bytes written
The LinesIter contains string slices over ‘\n’ terminated lines. If you with to use “\r\n” line endings in the command input this should be handled in the IO implementation.
sourcefn run_transform_command(
&mut self,
ui: &mut UILock<'_>,
command: String,
input: LinesIter<'_>
) -> Result<String, IOError>
fn run_transform_command( &mut self, ui: &mut UILock<'_>, command: String, input: LinesIter<'_> ) -> Result<String, IOError>
Run a transform command, taking part of buffer via stdin and returning it via stdout.
Stderr should be passed through to UI
The LinesIter contains string slices over ‘\n’ terminated lines. If you with to use “\r\n” line endings in the command input this should be handled in the IO implementation.
The returned string will be split into lines and added into the buffer. All line endings are converted into ‘\n’ when adding into the buffer.
sourcefn write_file(
&mut self,
path: &str,
append: bool,
data: LinesIter<'_>
) -> Result<usize, IOError>
fn write_file( &mut self, path: &str, append: bool, data: LinesIter<'_> ) -> Result<usize, IOError>
Normal file write
Returns number of bytes written
The LinesIter contains string slices over ‘\n’ terminated lines. If you with to write “\r\n” line endings into the file this should be handled in the IO implementation.