Runtime

Struct Runtime 

Source
pub struct Runtime<'a, N, F = CliFrontend>
where N: Node, F: Frontend,
{ /* private fields */ }
Expand description

Wraps the basic state with extra information and functionality

Implementations§

Source§

impl<'a, N, F> Runtime<'a, N, F>
where N: Node, F: Frontend,

Source

pub fn push<M>(&mut self) -> Interrupt<N>
where M: Node<State = N::State> + Default,

Push a Node onto the stack using its default

Source

pub fn push_node<M>(&mut self, node: M) -> Interrupt<N>
where M: Node<State = N::State>,

Push a Node onto the stack

Examples found in repository?
examples/rooms.rs (line 124)
104fn main() {
105    run(
106        Room::Start,
107        &mut State { has_key: false },
108        &mut CliFrontend::new(),
109    )
110    .command("save", |rt| {
111        match rt.save("rooms.yaml") {
112            Ok(_) => rt.println("Save complete"),
113            Err(e) => rt.println(format!("Error: {}", e)),
114        }
115        Continue
116    })
117    .command("load", |rt| {
118        match rt.load("rooms.yaml") {
119            Ok(_) => rt.println("Load complete"),
120            Err(e) => rt.println(format!("Error: {}", e)),
121        }
122        Skip
123    })
124    .command("inv, inventory", |rt| rt.push_node(Inventory));
125}
Source

pub fn save<P: AsRef<Path>>(&self, path: P) -> RuntimeResult<()>
where N: Serialize, N::State: Serialize,

Save the Runtime to the given path

Examples found in repository?
examples/rooms.rs (line 111)
104fn main() {
105    run(
106        Room::Start,
107        &mut State { has_key: false },
108        &mut CliFrontend::new(),
109    )
110    .command("save", |rt| {
111        match rt.save("rooms.yaml") {
112            Ok(_) => rt.println("Save complete"),
113            Err(e) => rt.println(format!("Error: {}", e)),
114        }
115        Continue
116    })
117    .command("load", |rt| {
118        match rt.load("rooms.yaml") {
119            Ok(_) => rt.println("Load complete"),
120            Err(e) => rt.println(format!("Error: {}", e)),
121        }
122        Skip
123    })
124    .command("inv, inventory", |rt| rt.push_node(Inventory));
125}
Source

pub fn load<P: AsRef<Path>>(&mut self, path: P) -> RuntimeResult<()>

Load the Runtime from the given path

Examples found in repository?
examples/rooms.rs (line 118)
104fn main() {
105    run(
106        Room::Start,
107        &mut State { has_key: false },
108        &mut CliFrontend::new(),
109    )
110    .command("save", |rt| {
111        match rt.save("rooms.yaml") {
112            Ok(_) => rt.println("Save complete"),
113            Err(e) => rt.println(format!("Error: {}", e)),
114        }
115        Continue
116    })
117    .command("load", |rt| {
118        match rt.load("rooms.yaml") {
119            Ok(_) => rt.println("Load complete"),
120            Err(e) => rt.println(format!("Error: {}", e)),
121        }
122        Skip
123    })
124    .command("inv, inventory", |rt| rt.push_node(Inventory));
125}
Source

pub fn prompt<S: Display>(&mut self, prompt: S) -> ControlResult<String, N>

Prompt the user to enter text

Examples found in repository?
examples/counter.rs (line 34)
15    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
16    where
17        F: Frontend,
18    {
19        rt.clear();
20        rt.println(format!("count: {}\n", rt.count));
21        match self {
22            MyNode::Small => {
23                // `Runtime::multiple_choice` prompts a user to choose
24                // from one of several choices
25                match rt
26                    .multiple_choice("How many do you want to add?", &["1", "2", "3", "more"])?
27                {
28                    n if n <= 2 => rt.count += n + 1,
29                    _ => return next(MyNode::Large),
30                }
31            }
32            MyNode::Large => {
33                // `Runtime::prompt` prompts a user to enter text
34                let input = rt.prompt("Enter a number to add")?;
35                match input.parse::<usize>() {
36                    Ok(i) => rt.count += i,
37                    Err(_) => rt.println("Invalid number"),
38                }
39            }
40        }
41        next(MyNode::Small)
42    }
Source

pub fn wait(&mut self) -> ControlResult<(), N>

Wait for the user to continue

Examples found in repository?
examples/paragraphs.rs (line 26)
21    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
22    where
23        F: Frontend,
24    {
25        rt.println(self.text());
26        rt.wait()?;
27        // The `next` method of the generated `LoremIpsum` struct
28        // gets the next paragraph
29        self.next()
30    }
More examples
Hide additional examples
examples/rooms.rs (line 25)
17    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
18    where
19        F: Frontend,
20    {
21        rt.clear();
22        match self {
23            Room::Start => {
24                rt.println("You are in a room.");
25                rt.wait()?;
26                let next_room = rt.multiple_choice_named(
27                    "Where do you want to go?",
28                    vec![
29                        ("The door with light behind it", Room::Exit),
30                        ("The door with no light behind it", Room::Side),
31                    ],
32                )?;
33                next(next_room)
34            }
35            Room::Side => {
36                rt.println(if rt.has_key {
37                    "There is nothing here."
38                } else {
39                    "There is a key on the floor."
40                });
41                rt.wait()?;
42                let pick_up_key = rt.multiple_choice_named(
43                    "What do you want to do?",
44                    Some(("Pick up the key", true))
45                        .filter(|_| !rt.has_key)
46                        .into_iter()
47                        .chain(Some(("Go back through the door", false))),
48                )?;
49                if pick_up_key {
50                    rt.has_key = true;
51                    next(Room::Side)
52                } else {
53                    next(Room::Start)
54                }
55            }
56            Room::Exit => {
57                rt.println("There is another door with a bright light behind it.");
58                rt.wait()?;
59                let try_to_leave = rt.multiple_choice_named(
60                    "Where do you want to go?",
61                    vec![
62                        ("Through the door ahead", true),
63                        ("Back through the door you came in", false),
64                    ],
65                )?;
66                if try_to_leave {
67                    if rt.has_key {
68                        rt.println("The door is locked. You unlock the door with your key.");
69                        rt.wait()?;
70                        rt.println("You escaped! You win!");
71                        rt.wait()?;
72                        exit()
73                    } else {
74                        rt.println("The door is locked.");
75                        rt.wait()?;
76                        next(Room::Exit)
77                    }
78                } else {
79                    next(Room::Start)
80                }
81            }
82        }
83    }
Source

pub fn multiple_choice<M, C>( &mut self, message: M, choices: &[C], ) -> ControlResult<usize, N>
where M: Display, C: Display,

Prompt the user with multiple choices

Examples found in repository?
examples/counter.rs (line 26)
15    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
16    where
17        F: Frontend,
18    {
19        rt.clear();
20        rt.println(format!("count: {}\n", rt.count));
21        match self {
22            MyNode::Small => {
23                // `Runtime::multiple_choice` prompts a user to choose
24                // from one of several choices
25                match rt
26                    .multiple_choice("How many do you want to add?", &["1", "2", "3", "more"])?
27                {
28                    n if n <= 2 => rt.count += n + 1,
29                    _ => return next(MyNode::Large),
30                }
31            }
32            MyNode::Large => {
33                // `Runtime::prompt` prompts a user to enter text
34                let input = rt.prompt("Enter a number to add")?;
35                match input.parse::<usize>() {
36                    Ok(i) => rt.count += i,
37                    Err(_) => rt.println("Invalid number"),
38                }
39            }
40        }
41        next(MyNode::Small)
42    }
Source

pub fn multiple_choice_named<M, S, C, I>( &mut self, message: M, choices: I, ) -> ControlResult<C, N>
where M: Display, S: Display, I: IntoIterator<Item = (S, C)>,

Prompt the user with custom-named multiple choices

Examples found in repository?
examples/rooms.rs (lines 26-32)
17    fn next<F>(self, rt: &mut Runtime<Self, F>) -> Control<Self>
18    where
19        F: Frontend,
20    {
21        rt.clear();
22        match self {
23            Room::Start => {
24                rt.println("You are in a room.");
25                rt.wait()?;
26                let next_room = rt.multiple_choice_named(
27                    "Where do you want to go?",
28                    vec![
29                        ("The door with light behind it", Room::Exit),
30                        ("The door with no light behind it", Room::Side),
31                    ],
32                )?;
33                next(next_room)
34            }
35            Room::Side => {
36                rt.println(if rt.has_key {
37                    "There is nothing here."
38                } else {
39                    "There is a key on the floor."
40                });
41                rt.wait()?;
42                let pick_up_key = rt.multiple_choice_named(
43                    "What do you want to do?",
44                    Some(("Pick up the key", true))
45                        .filter(|_| !rt.has_key)
46                        .into_iter()
47                        .chain(Some(("Go back through the door", false))),
48                )?;
49                if pick_up_key {
50                    rt.has_key = true;
51                    next(Room::Side)
52                } else {
53                    next(Room::Start)
54                }
55            }
56            Room::Exit => {
57                rt.println("There is another door with a bright light behind it.");
58                rt.wait()?;
59                let try_to_leave = rt.multiple_choice_named(
60                    "Where do you want to go?",
61                    vec![
62                        ("Through the door ahead", true),
63                        ("Back through the door you came in", false),
64                    ],
65                )?;
66                if try_to_leave {
67                    if rt.has_key {
68                        rt.println("The door is locked. You unlock the door with your key.");
69                        rt.wait()?;
70                        rt.println("You escaped! You win!");
71                        rt.wait()?;
72                        exit()
73                    } else {
74                        rt.println("The door is locked.");
75                        rt.wait()?;
76                        next(Room::Exit)
77                    }
78                } else {
79                    next(Room::Start)
80                }
81            }
82        }
83    }

Trait Implementations§

Source§

impl<'a, N, F> AsMut<<N as Node>::State> for Runtime<'a, N, F>
where N: Node, F: Frontend,

Source§

fn as_mut(&mut self) -> &mut N::State

Converts this type into a mutable reference of the (usually inferred) input type.
Source§

impl<'a, N, F> AsRef<<N as Node>::State> for Runtime<'a, N, F>
where N: Node, F: Frontend,

Source§

fn as_ref(&self) -> &N::State

Converts this type into a shared reference of the (usually inferred) input type.
Source§

impl<'a, N, F> Deref for Runtime<'a, N, F>
where N: Node, F: Frontend,

Source§

type Target = <N as Node>::State

The resulting type after dereferencing.
Source§

fn deref(&self) -> &Self::Target

Dereferences the value.
Source§

impl<'a, N, F> DerefMut for Runtime<'a, N, F>
where N: Node, F: Frontend,

Source§

fn deref_mut(&mut self) -> &mut Self::Target

Mutably dereferences the value.
Source§

impl<'a, N, F> Frontend for Runtime<'a, N, F>
where N: Node, F: Frontend,

Source§

fn background_color(&self) -> Color

Get the background color
Source§

fn set_background_color(&mut self, color: Color)

Set the background color
Source§

fn clear(&mut self)

Clear the display
Source§

fn print_colored<S: ToString>(&mut self, text: S, color: Color)

Print text to the display with the given color
Source§

fn newline(&mut self)

Print a newline to the display
Source§

fn input(&mut self) -> String

Get input from the user
Source§

fn print<S: ToString>(&mut self, text: S)

Print text to the display
Source§

fn println<S: ToString>(&mut self, text: S)

Print text and a newline to the display

Auto Trait Implementations§

§

impl<'a, N, F> Freeze for Runtime<'a, N, F>
where N: Freeze, <N as Node>::State: Freeze, F: Freeze,

§

impl<'a, N, F = CliFrontend> !RefUnwindSafe for Runtime<'a, N, F>

§

impl<'a, N, F = CliFrontend> !Send for Runtime<'a, N, F>

§

impl<'a, N, F = CliFrontend> !Sync for Runtime<'a, N, F>

§

impl<'a, N, F> Unpin for Runtime<'a, N, F>
where N: Unpin, <N as Node>::State: Unpin, F: Unpin,

§

impl<'a, N, F = CliFrontend> !UnwindSafe for Runtime<'a, N, F>

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<P, T> Receiver for P
where P: Deref<Target = T> + ?Sized, T: ?Sized,

Source§

type Target = T

🔬This is a nightly-only experimental API. (arbitrary_self_types)
The target type on which the method may be called.
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.