virtual_io

Trait VirtualIo

Source
pub trait VirtualIo {
    // Required methods
    fn print<S: Into<String>>(&mut self, message: S) -> &mut Self;
    fn println<S: Into<String>>(&mut self, message: S) -> &mut Self;
    fn read_line(&mut self) -> String;
    fn get_environment_var<S: Into<String>>(
        &self,
        variable: S,
    ) -> Result<String, VarError>;
    fn get_environment_vars(&self) -> Vec<(String, String)>;
}
Expand description

Virtual IO is a rust library for easily implementing stdin and stdout in a testable way. It replaces all println! and print! macro calls.

use virtual_io::{VirtualIo, Vio};

fn get_name() -> String {
    get_name_base(&mut virtual_io::new())
}

fn get_name_base(vio: &mut impl VirtualIo) -> String {
    vio.print("What is your name? ");
    let name = vio.read_line();
    vio.println(format!("Hello, {}!", name));
    name
}

Required Methods§

Source

fn print<S: Into<String>>(&mut self, message: S) -> &mut Self

Prints a message to the console. Is close to the print! macro with one difference: output is not buffered and is instead immediately flushed.

Source

fn println<S: Into<String>>(&mut self, message: S) -> &mut Self

Prints a message to the console with a new line at the end. Is equivalent to the println! macro.

Source

fn read_line(&mut self) -> String

Get user input from the console. The input ends when the user types a new line.

Source

fn get_environment_var<S: Into<String>>( &self, variable: S, ) -> Result<String, VarError>

Get an environment variable. Is equivalent to std::env::var.

§Errors

Returns an error if the environment variable is not present.

Source

fn get_environment_vars(&self) -> Vec<(String, String)>

Get an environment variables as a vector. Each item in the vector is a (key, value) tuple.

Dyn Compatibility§

This trait is not dyn compatible.

In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.

Implementors§