[−][src]Struct rpick::Engine
The rpick Engine object allows you to write your own rpick interface.
Attributes
input
- This must be an object that implements theBufRead
trait, and is used to receive a y or n answer from a user, when prompted for whether they accept some input. The rpick CLI sets this to stdin, for example.output
- This must be an object that implements theWrite
trait. It is used to prompt the user to accept a choice.rng
- This must be a random number generator that implements therand::RngCore
trait.rejected_choices
- A list of choices the user has rejected. We maintain this so we don't ask again about a choice they've already declined.
Methods
impl<I, O, R> Engine<I, O, R> where
I: BufRead,
O: Write,
R: RngCore,
[src]
I: BufRead,
O: Write,
R: RngCore,
pub fn new(input: I, output: O, rng: R) -> Engine<I, O, R>
[src]
Instantiate an Engine.
Arguments
input
- This must be an object that implements theBufRead
trait, and is used to receive a y or n answer from a user, when prompted for whether they accept some input. The rpick CLI sets this to stdin, for example.output
- This must be an object that implements theWrite
trait. It is used to prompt the user to accept a choice.rng
- This must be a random number generator that implements therand::RngCore
trait.
Example
let stdio = std::io::stdin(); let input = stdio.lock(); let output = std::io::stdout(); let mut engine = rpick::Engine::new(input, output, rand::thread_rng());
pub fn pick(
&mut self,
config: &mut BTreeMap<String, ConfigCategory>,
category: String
) -> Result<String, Box<dyn Error>>
[src]
&mut self,
config: &mut BTreeMap<String, ConfigCategory>,
category: String
) -> Result<String, Box<dyn Error>>
Pick an item from the ConfigCategory
referenced by the given category
.
Arguments
config
- A mapping of category names toConfigCategory
objects, which contain the parameters which should be used for the pick.category
- The category you wish to choose from.
Returns
This will return the chosen item.
Example
use std::collections::BTreeMap; use rand::SeedableRng; let input = String::from("y"); let output = Vec::new(); let mut engine = rpick::Engine::new(input.as_bytes(), output, rand::rngs::SmallRng::seed_from_u64(42)); let choices = vec![String::from("this"), String::from("that"), String::from("the other")]; let category = rpick::ConfigCategory::Even{choices: choices}; let mut config = BTreeMap::new(); config.insert("things".to_string(), category); let choice = engine.pick(&mut config, "things".to_string()).expect("unexpected"); assert_eq!(choice, "this");
Auto Trait Implementations
impl<I, O, R> Send for Engine<I, O, R> where
I: Send,
O: Send,
R: Send,
I: Send,
O: Send,
R: Send,
impl<I, O, R> Sync for Engine<I, O, R> where
I: Sync,
O: Sync,
R: Sync,
I: Sync,
O: Sync,
R: Sync,
Blanket Implementations
impl<T> From for T
[src]
impl<T, U> Into for T where
U: From<T>,
[src]
U: From<T>,
impl<T, U> TryFrom 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> Borrow for T where
T: ?Sized,
[src]
T: ?Sized,
impl<T> Any for T where
T: 'static + ?Sized,
[src]
T: 'static + ?Sized,
impl<T> BorrowMut for T where
T: ?Sized,
[src]
T: ?Sized,
fn borrow_mut(&mut self) -> &mut T
[src]
impl<T, U> TryInto for T where
U: TryFrom<T>,
[src]
U: TryFrom<T>,