Struct smart_read::list_constraints::InputOption

source ·
pub struct InputOption<Data> {
    pub display_name: String,
    pub choose_names: Vec<String>,
    pub data: Data,
}
Expand description

Allows you to add more data to an option

Example:

// example data
let mut colors = vec!("Red", "green", "Blue");
 
// prepare options, only capitalized colors can be removed
let choosable_colors =
	colors.iter().enumerate()
	.filter_map(|(i, color_name)| {
		let first_char = color_name.chars().next()?;
		if first_char.is_lowercase() {return None;}
		Some(OptionWithData {name: color_name.to_string(), data: i})
	})
	.collect::<Vec<_>>();
 
// prompt
let OptionWithData {name: _, data: index_to_remove} = prompt!("Choose a color to remove: "; choosable_colors);
colors.remove(index_to_remove);

Fields§

§display_name: String

What’s shown to the user (minus the choose_name)

§choose_names: Vec<String>

What the user needs to enter to choose this option

§data: Data

What isn’t shown to the user

Implementations§

source§

impl<Data> InputOption<Data>

source

pub fn new( display: impl Into<String>, choose: Vec<impl Into<String>>, data: Data, ) -> Self

Basic initializer

Examples found in repository?
examples/read_lines.rs (line 55)
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
fn main() {
	
	read!(&["Lanercoast", "Windrip", "Redwick Bush", "Brickelwhyte", "Sirencester", "Conriston", "Inverness", "Norwich", "Elinmylly", "Murlayfield"]);
	
	
	
	println!("\n==== `read!()` ====");
	let input = read!(); // read a line of text
	println!("You entered: \"{input}\"");
	
	println!("\n==== `prompt!(\"Enter a string: \")` ====");
	let input = prompt!("Enter a string: "); // prompt a line of text
	println!("You entered: \"{input}\"");
	
	println!("\n==== `read!(UsizeInput)` ====");
	let input = read!(UsizeInput); // read specific types
	println!("You entered: \"{input}\"");
	println!("\n==== `read!(BoolInput)` ====");
	let input = read!(BoolInput);
	println!("You entered: \"{input}\"");
	println!("\n==== `read!(NonWhitespaceInput)` ====");
	let input = read!(NonWhitespaceInput);
	println!("You entered: \"{input}\"");
	println!("\n==== `read!(I32Input)` ====");
	let input = read!(I32Input);
	println!("You entered: \"{input}\"");
	
	println!("\n==== `read!(0. ..= 100.)` ====");
	let input = read!(0. ..= 100.); // read a number within a range
	println!("You entered: \"{input}\"");
	
	
	
	println!("\n==== `prompt!(\"Confirm input: \"; YesNoInput)` ====");
	let input = prompt!("Confirm input: "; YesNoInput); // read a bool
	println!("You entered: \"{input}\"");
	
	println!("\n==== `prompt!(\"Confirm input: \"; [true] YesNoInput)` ====");
	let input = prompt!("Confirm input: "; [true] YesNoInput); // set a default value
	println!("You entered: \"{input}\"");
	
	
	
	println!("\n==== `read!(&[\"red\", \"green\", \"blue\"])` ====");
	let input = read!(&["red", "green", "blue"]).1; // choose from a list of options
	println!("You entered: \"{input}\"");
	println!("\n==== `read!(= \"red\", \"green\", \"blue\")` ====");
	let input = read!(= "red", "green", "blue").1; // some inputs have special syntax
	println!("You entered: \"{input}\"");
	
	println!("\n==== `read!(&[InputOption::new(...), ...])` ====");
	let options = &[
		InputOption::new("red", vec!("1", "r"), ()),
		InputOption::new("green", vec!("2", "g"), ()),
		InputOption::new("blue", vec!("3", "b"), ()),
	];
	let input = read!(options);
	println!("You entered: index {}, \"{}\"", input.0, input.1.display_name);
	
	
	
	println!("\n==== `prompt!(\"Enter an even int: \"; TransformValidate(...));` ====");
	// one-time custom logic
	let input = prompt!("Enter an even int: "; TransformValidate (|x| {
		let Ok(x) = x.parse::<isize>() else {return Err(String::from("Could not parse input"));};
		if x % 2 != 0 {return Err(String::from("Input is not even."));}
		Ok(x)
	}));
	println!("You entered: \"{input}\"");
	
	
	
	println!("\n==== `prompt!(\"Enter an int: \"; [1usize] = 1, 2, 3, 4, 5)` ====");
	let input = prompt!("Enter an int: "; [1usize] = 1, 2, 3, 4, 5).1; // combine any features
	println!("You entered: \"{input}\"");
	
	
	
	println!();
	prompt!("read_lines finished, press enter to exit.");
	
}
source

pub fn get_display_string(&self, is_default: Option<bool>) -> String

Internal function

Auto Trait Implementations§

§

impl<Data> Freeze for InputOption<Data>
where Data: Freeze,

§

impl<Data> RefUnwindSafe for InputOption<Data>
where Data: RefUnwindSafe,

§

impl<Data> Send for InputOption<Data>
where Data: Send,

§

impl<Data> Sync for InputOption<Data>
where Data: Sync,

§

impl<Data> Unpin for InputOption<Data>
where Data: Unpin,

§

impl<Data> UnwindSafe for InputOption<Data>
where Data: UnwindSafe,

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>,

§

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>,

§

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.