io-providers Build Status Latest Version Documentation License

Defines "provider" traits and implementations for different types of I/O operations, enabling dependency injection that's very helpful for testing.

A number of different I/O types are supported:

In addition to "native" implementations for each trait, "simulated" implementations are also built-in:

Each provider trait can be used independently, however there is also the all-encompassing Io which provides access to all of them. If you have a variety of I/O dependencies, it might be easiest to create and pass around a single &mut Io.

Documentation

https://pshendry.github.io/io-providers/io_providers/

Examples

Cargo.toml:

toml [dependencies] io-providers = "0.1"

src/main.rs:

```rust extern crate io_providers;

use std::io::Write; use std::path::Path; use io_providers::{Env, Io, NativeIo, SimulatedIo, StdStreams};

/// Gets the current working directory and prints it to stdout. fn dowork(io: &mut I) { let curdir = io.env().currentdir().unwrap(); let stdout = io.stdstreams().output(); writeln!(stdout, "The current directory is: {}", curdir.tostr().unwrap()).unwrap(); }

fn main() { // Test do_work() using a simulated I/O environment let mut simulatedio = SimulatedIo::new().unwrap(); simulatedio.env().setcurrentdir(Path::new("/foo/bar")).unwrap(); dowork(&mut simulatedio); asserteq!( "The current directory is: /foo/bar\n", ::std::str::fromutf8(simulatedio.stdstreams().read_output()).unwrap());

// Now use a native I/O provided to access the real system
let mut real_io = NativeIo::new();
do_work(&mut real_io);

} ```

License

io-providers is distributed under the MIT license.