[][src]Struct promptor::promptor::Promptor

pub struct Promptor<R, W> {
    pub reader: R,
    pub writer: W,
}

Promptor

Holds the input and output handles and redirects input and output to them.

Example

To use this with stdio:

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

Fields

reader: Rwriter: W

Implementations

impl<R, W> Promptor<R, W> where
    R: BufRead,
    W: Write
[src]

pub fn get_line(&mut self, msg: &str) -> Option<String>[src]

Get a newline-terminated string from stdin, returning None if std::io::stdout.flush() fails or if std::io::stdin().read_line() fails.

Arguments

  • msg – a message to display to the user.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.get_line("What's your name?");

match res {
    Some(s) => println!("Nice to meet you, {}!", s),
    None    => println!("I'm sorry!")
}

The Haskell spec for this function is:

promptLine :: String -> IO String
promptLine msg = do
    putStr msg
    hFlush stdout
    getLine

pub fn rget_line(&mut self, msg: &str) -> Result<String, PromptError>[src]

Same as get_line(), but returns a Result<String, PromptError>. Use this version if you need control over the errors. Returns PromptError::StdinError if:

  • write!() fails
  • self.writer.flush() fails
  • self.reader.read_line() fails

Arguments

  • msg – a message to display to the user.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.rget_line("What's your name?");

match res {
    Ok(s)  => println!("Nice to meet you, {}!", s),
    Err(e) => eprintln!("I'm sorry! I got an error: {}", e)
}

pub fn read<T>(&mut self, arg: &str) -> Option<T> where
    T: FromStr
[src]

Attempts to convert the contents of a string to a type that implements std::str::FromStr. Returns None if conversion failed. More or less a wrapper around parse.

Arguments

  • arg – string to attempt to convert.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.read::<i32>("32").map(|x| x * 2).unwrap();

println!("Value of res: {}.", res);

The Haskell spec for this function is:

readMaybe :: Read a => String -> Maybe a

pub fn rread<T>(&mut self, arg: &str) -> Result<T, PromptError> where
    T: FromStr
[src]

Same as read(), but returns a Result<T, PromptError>. Use this version if you need control over the errors. Returns PromptError::ReadError if:

  • T::from_str(arg) fails

Arguments

  • arg – string to attempt to convert.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.rread::<i32>("32").map(|x| x * 2).unwrap();

println!("Value of res: {}.", res);

pub fn input<T>(&mut self, msg: &str) -> Option<T> where
    T: SafeParsable
[src]

Gets a value of type T from the user, where T defines a default value and implements std::str::FromStr. This function returns None if it is not able to parse the user's input into T.

Arguments

msg – a message to display to the user.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.input::<i32>("Please enter a number: ");

match res {
    Some(x) => println!("Got {}.", x),
    None => println!("Got invalid input!")
}

I designed this function as a type-safe analogue of Python's input function. However, this function returns an Option because it has no way to validate the user's input.

The Haskell spec for this function is:

getLine >>= pure . (Text.Read.readMaybe :: String -> Maybe Int)

pub fn rinput<T>(&mut self, msg: &str) -> Result<T, PromptError> where
    T: SafeParsable
[src]

Same as input(), but returns a Result<T, PromptError>. Use this version if you need control over the errors. Returns PromptError if:

  • rget_line() fails
  • rread() fails

Arguments

msg – a message to display to the user.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res = promptor.rinput::<i32>("Please enter a number: ");

match res {
    Ok(x)  => println!("Got {}.", x),
    Err(_) => println!("Got invalid input!")
}

pub fn prompt<T, F>(&mut self, msg: &str, validator: F) -> T where
    T: SafeParsable,
    F: Fn(T) -> bool
[src]

Prompts the user for a value of type T and validates it against validator. If input or validation fails, this function re-prompts the user.

Arguments

  • msg – a message to display to the user.
  • validator – a function which immutably borrows a single argument of type T and returns a bool.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res: u32 = promptor.prompt("Please enter a number between 1 and 100: ", |x| 1 <= x && x <= 100);

pub fn rprompt<T, F>(&mut self, msg: &str, validator: F) -> T where
    T: SafeParsable,
    F: Fn(T) -> bool
[src]

Same as prompt(), but internally uses the Result versions. This function is essentially the same as the Option version, but I have added it for completeness, and in case the emitted Results are more useful for debugging.

Warning: this function will panic if writeln() fails when:

  • write!() succeeds in rget_line() but writeln!() fails in this function.

Arguments

  • msg – a message to display to the user.
  • validator – a function which immutably borrows a single argument of type T and returns a bool.

Example

use promptor::Promptor;

let stdio = std::io::stdin();
let input = stdio.lock();
let output = std::io::stdout();

let mut promptor = Promptor {
    reader: input,
    writer: output
};

let res: u32 = promptor.rprompt("Please enter a number between 1 and 100: ", |x| 1 <= x && x <= 100);

Panics

If write!() succeeds in rget_line(), but writeln!() in this function somehow does not, this function panics with the message: "writeln!() failed, even though write!() succeeded earlier"

I

Auto Trait Implementations

impl<R, W> RefUnwindSafe for Promptor<R, W> where
    R: RefUnwindSafe,
    W: RefUnwindSafe

impl<R, W> Send for Promptor<R, W> where
    R: Send,
    W: Send

impl<R, W> Sync for Promptor<R, W> where
    R: Sync,
    W: Sync

impl<R, W> Unpin for Promptor<R, W> where
    R: Unpin,
    W: Unpin

impl<R, W> UnwindSafe for Promptor<R, W> where
    R: UnwindSafe,
    W: UnwindSafe

Blanket Implementations

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> Borrow<T> for T where
    T: ?Sized
[src]

impl<T> BorrowMut<T> for T where
    T: ?Sized
[src]

impl<T> From<T> for T[src]

impl<T, U> Into<U> for T where
    U: From<T>, 
[src]

impl<T, U> TryFrom<U> for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T, U> TryInto<U> for T where
    U: TryFrom<T>, 
[src]

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.