[−][src]Struct promptor::promptor::Promptor
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: R
writer: W
Implementations
impl<R, W> Promptor<R, W> where
R: BufRead,
W: Write,
[src]
R: BufRead,
W: Write,
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!()
failsself.writer.flush()
failsself.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]
T: FromStr,
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]
T: FromStr,
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]
T: SafeParsable,
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]
T: SafeParsable,
Same as input()
, but returns a Result<T, PromptError>
.
Use this version if you need control over the errors.
Returns PromptError
if:
rget_line()
failsrread()
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]
T: SafeParsable,
F: Fn(T) -> bool,
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 typeT
and returns abool
.
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]
T: SafeParsable,
F: Fn(T) -> bool,
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 Result
s
are more useful for debugging.
Warning: this function will panic if writeln()
fails when:
write!()
succeeds inrget_line()
butwriteln!()
fails in this function.
Arguments
msg
– a message to display to the user.validator
– a function which immutably borrows a single argument of typeT
and returns abool
.
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,
R: RefUnwindSafe,
W: RefUnwindSafe,
impl<R, W> Send for Promptor<R, W> where
R: Send,
W: Send,
R: Send,
W: Send,
impl<R, W> Sync for Promptor<R, W> where
R: Sync,
W: Sync,
R: Sync,
W: Sync,
impl<R, W> Unpin for Promptor<R, W> where
R: Unpin,
W: Unpin,
R: Unpin,
W: Unpin,
impl<R, W> UnwindSafe for Promptor<R, W> where
R: UnwindSafe,
W: UnwindSafe,
R: UnwindSafe,
W: UnwindSafe,
Blanket Implementations
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> Borrow<T> for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> BorrowMut<T> for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T> From<T> for T
[src]
impl<T, U> Into<U> for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom<U> for T where
U: Into<T>,
[src]
U: Into<T>,
type Error = Infallible
The type returned in the event of a conversion error.
fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>
[src]
impl<T, U> TryInto<U> for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,