Struct TypedPrompt

Source
pub struct TypedPrompt<'a, T>
where T: FromStr, T::Err: Display,
{ /* private fields */ }
Expand description

Typed builder for extras: .default_val, .validate, .message.

Implementations§

Source§

impl<'a, T> TypedPrompt<'a, T>
where T: FromStr, T::Err: Display,

Source

pub fn default_val(self, val: T) -> Self

Provide a typed default (não precisa parsear).

Examples found in repository?
examples/default_typed.rs (line 7)
3fn main() -> Result<(), askit::Error> {
4    // Default tipado: não precisa parse
5    let port: u16 = prompt("Port [typed default=5432]: ")
6        .to()
7        .default_val(5432)
8        .get()?;
9
10    println!("Port = {port}");
11    Ok(())
12}
Source

pub fn validate<F>(self, f: F) -> Self
where F: Fn(&T) -> bool + 'static,

function with validation Fn(&T) -> bool.

Examples found in repository?
examples/validate.rs (line 7)
3fn main() -> Result<(), askit::Error> {
4    let port: u32 = prompt("Port: ")
5        .to()
6        .retries(2)
7        .validate(|p| (1..=65535).contains(p))
8        .message("Port must be in 1..=65535")
9        .get()?;
10
11    println!("Using port {port}");
12    Ok(())
13}
Source

pub fn message(self, msg: &str) -> Self

Message .validate return false.

Examples found in repository?
examples/validate.rs (line 8)
3fn main() -> Result<(), askit::Error> {
4    let port: u32 = prompt("Port: ")
5        .to()
6        .retries(2)
7        .validate(|p| (1..=65535).contains(p))
8        .message("Port must be in 1..=65535")
9        .get()?;
10
11    println!("Using port {port}");
12    Ok(())
13}
Source

pub fn retries(self, retries: usize) -> Self

Number of times to retry (aplica no Prompt base).

Examples found in repository?
examples/validate.rs (line 6)
3fn main() -> Result<(), askit::Error> {
4    let port: u32 = prompt("Port: ")
5        .to()
6        .retries(2)
7        .validate(|p| (1..=65535).contains(p))
8        .message("Port must be in 1..=65535")
9        .get()?;
10
11    println!("Using port {port}");
12    Ok(())
13}
Source

pub fn trim(self, yes: bool) -> Self

Whether to trim whitespace (default: true).

Source

pub fn get(self) -> Result<T, Error>

read and return T using stdin/stdout.

Examples found in repository?
examples/default_typed.rs (line 8)
3fn main() -> Result<(), askit::Error> {
4    // Default tipado: não precisa parse
5    let port: u16 = prompt("Port [typed default=5432]: ")
6        .to()
7        .default_val(5432)
8        .get()?;
9
10    println!("Port = {port}");
11    Ok(())
12}
More examples
Hide additional examples
examples/validate.rs (line 9)
3fn main() -> Result<(), askit::Error> {
4    let port: u32 = prompt("Port: ")
5        .to()
6        .retries(2)
7        .validate(|p| (1..=65535).contains(p))
8        .message("Port must be in 1..=65535")
9        .get()?;
10
11    println!("Using port {port}");
12    Ok(())
13}
Source

pub fn get_with_io<R, W>( self, reader: &mut R, writer: &mut W, ) -> Result<T, Error>
where R: BufRead, W: Write,

Auto Trait Implementations§

§

impl<'a, T> Freeze for TypedPrompt<'a, T>
where T: Freeze,

§

impl<'a, T> !RefUnwindSafe for TypedPrompt<'a, T>

§

impl<'a, T> !Send for TypedPrompt<'a, T>

§

impl<'a, T> !Sync for TypedPrompt<'a, T>

§

impl<'a, T> Unpin for TypedPrompt<'a, T>
where T: Unpin,

§

impl<'a, T> !UnwindSafe for TypedPrompt<'a, T>

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

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

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.