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
impl<'a> ValueField<'a>
Constructor methods defining how the field behaves
Sourcepub fn fmt(self, fmt: ValueFieldFormatting<'a>) -> ValueField<'a>
pub fn fmt(self, fmt: ValueFieldFormatting<'a>) -> ValueField<'a>
Give a custom formatting for the field.
Sourcepub fn default_value(self, default: &'a str) -> ValueField<'a>
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).
Sourcepub fn default_env(self, var: &'a str) -> Result<ValueField<'a>, MenuError>
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).
Sourcepub fn build_init<T>(&self) -> Result<T, MenuError>
pub fn build_init<T>(&self) -> Result<T, MenuError>
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();Sourcepub fn build<T>(
&self,
reader: &mut BufReader<Stdin>,
writer: &mut Stdout,
) -> Result<T, MenuError>
pub fn build<T>( &self, reader: &mut BufReader<Stdin>, writer: &mut Stdout, ) -> Result<T, MenuError>
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();Sourcepub fn build_with<T, R, W>(
&self,
stream: &mut MenuStream<'_, R, W>,
) -> Result<T, MenuError>
pub fn build_with<T, R, W>( &self, stream: &mut MenuStream<'_, R, W>, ) -> Result<T, MenuError>
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.
Sourcepub fn build_or_default<T>(
&self,
reader: &mut BufReader<Stdin>,
writer: &mut Stdout,
) -> T
pub fn build_or_default<T>( &self, reader: &mut BufReader<Stdin>, writer: &mut Stdout, ) -> T
Builds the field, or returns the default value from the type.
Sourcepub fn build_or_default_with<T, R, W>(
&self,
stream: &mut MenuStream<'_, R, W>,
) -> T
pub fn build_or_default_with<T, R, W>( &self, stream: &mut MenuStream<'_, R, W>, ) -> T
Builds the field with a given menu stream, or returns the default value from the type.