[][src]Struct rpick::Engine

pub struct Engine<I, O, R> { /* fields omitted */ }

The rpick Engine object allows you to write your own rpick interface.

Attributes

  • input - This must be an object that implements the BufRead 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 the Write trait. It is used to prompt the user to accept a choice.
  • rng - This must be a random number generator that implements the rand::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]

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 the BufRead 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 the Write trait. It is used to prompt the user to accept a choice.
  • rng - This must be a random number generator that implements the rand::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]

Pick an item from the ConfigCategory referenced by the given category.

Arguments

  • config - A mapping of category names to ConfigCategory 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

impl<I, O, R> Sync for Engine<I, O, R> where
    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]

impl<T, U> TryFrom for T where
    U: Into<T>, 
[src]

type Error = Infallible

The type returned in the event of a conversion error.

impl<T> Borrow for T where
    T: ?Sized
[src]

impl<T> Any for T where
    T: 'static + ?Sized
[src]

impl<T> BorrowMut for T where
    T: ?Sized
[src]

impl<T, U> TryInto for T where
    U: TryFrom<T>, 
[src]

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

The type returned in the event of a conversion error.