Skip to main content

ValueField

Struct ValueField 

Source
pub struct ValueField<'a> { /* private fields */ }
Expand description

Defines behavior for a value-menu field.

It manages

  • its message
  • its formatting
  • the return value type
  • the default value

§Examples

For a make-license CLI program for example, you can use

use ezmenulib::ValueField;
let author: String = ValueField::from("Give the author of the license")
    .build_init()
    .unwrap();

Implementations§

Source§

impl<'a> ValueField<'a>

Constructor methods defining how the field behaves

Source

pub fn fmt(self, fmt: ValueFieldFormatting<'a>) -> ValueField<'a>

Give a custom formatting for the field.

Source

pub fn default_value(self, default: &'a str) -> ValueField<'a>

Give the default value accepted by the field.

If the value type is incorrect, the ValueField::build or ValueField::build_init method will return an Err value emphasizing that the default value type is incorrect.

So when instantiating the field with a provided default value, it will not panic and it will print the default value even if it is incorrect (if the format rule default is set to true).

Source

pub fn default_env(self, var: &'a str) -> Result<ValueField<'a>, MenuError>

Give the default value of the field, passing by an environment variable.

If the provided environment variable is incorrect, it will return an error (See MenuError::EnvVar variant).

If the value type of the variable is incorrect, the ValueField::build or ValueField::build_init method will return an Err value emphasizing that the default value type is incorrect.

So when instantiating the field with a provided default value, it will not panic and it will print the default value even if it is incorrect (if the format rule default is set to true).

Source

pub fn build_init<T>(&self) -> Result<T, MenuError>
where T: FromStr, <T as FromStr>::Err: 'static + Debug,

Builds the field without specifying standard input and output files.

It initializes instance of Stdin and Stdout.

§Example
use ezmenulib::prelude::*;
let age: MenuResult<u8> = ValueField::from("How old are you")
    .build_init();
Source

pub fn build<T>( &self, reader: &mut BufReader<Stdin>, writer: &mut Stdout, ) -> Result<T, MenuError>
where T: FromStr, <T as FromStr>::Err: 'static + Debug,

Builds the field. It prints the message according to its formatting, then returns the corresponding value.

You need to provide the reader for the input, and the writer for the output. It returns a MenuResult if there was an IO error, or if the default value type is incorrect.

§Example

Supposing you have declared your own Stdin and Stdout in your program, you can do so:

use std::io::{stdin, stdout};

let stdin = stdin();
let mut stdout = stdout();

let author: String = ValueField::from("Author")
    .build(&stdin, &mut stdout)
    .unwrap();
Source

pub fn build_with<T, R, W>( &self, stream: &mut MenuStream<'_, R, W>, ) -> Result<T, MenuError>
where T: FromStr, <T as FromStr>::Err: 'static + Debug, R: BufRead, W: Write,

Builds the fields with a given menu stream. It prints out the message to the stream according to its formatting, then returns the corresponding value.

You need to instantiate beforehand the MenuStream to use this method.

Source

pub fn build_or_default<T>( &self, reader: &mut BufReader<Stdin>, writer: &mut Stdout, ) -> T
where T: FromStr + Default, <T as FromStr>::Err: 'static + Debug,

Builds the field, or returns the default value from the type.

Source

pub fn build_or_default_with<T, R, W>( &self, stream: &mut MenuStream<'_, R, W>, ) -> T
where T: FromStr + Default, <T as FromStr>::Err: 'static + Debug, R: BufRead, W: Write,

Builds the field with a given menu stream, or returns the default value from the type.

Trait Implementations§

Source§

impl<'a> Display for ValueField<'a>

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl<'a> From<&'a str> for ValueField<'a>

Source§

fn from(msg: &'a str) -> ValueField<'a>

Converts to this type from the input type.

Auto Trait Implementations§

§

impl<'a> !Send for ValueField<'a>

§

impl<'a> !Sync for ValueField<'a>

§

impl<'a> Freeze for ValueField<'a>

§

impl<'a> RefUnwindSafe for ValueField<'a>

§

impl<'a> Unpin for ValueField<'a>

§

impl<'a> UnsafeUnpin for ValueField<'a>

§

impl<'a> UnwindSafe for ValueField<'a>

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> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
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.