mut_state/
mut_state.rs

1use easy_repl::{Repl, CommandStatus, command, validator};
2use anyhow::{self, Context};
3
4fn main() -> anyhow::Result<()> {
5    let mut outside_x = String::from("Out x");
6    let mut outside_y = String::from("Out y");
7
8    let mut repl = Repl::builder()
9        .description("Example REPL")
10        .prompt("=> ")
11        .text_width(60 as usize)
12        .add("count", command! {
13            "Count from X to Y",
14            (X:i32, Y:i32) => |x, y| {
15                for i in x..=y {
16                    print!(" {}", i);
17                }
18                println!();
19                Ok(CommandStatus::Done)
20            }
21        })
22        .add("say", command! {
23            "Say X",
24            (:f32) => |x| {
25                println!("x is equal to {}", x);
26                Ok(CommandStatus::Done)
27            },
28        })
29        .add("outx", command! {
30            "Use mutably outside var x. This command has a really long description so we need to wrap it somehow, it is interesting how actually the wrapping will be performed.",
31            () => || {
32                outside_x += "x";
33                println!("{}", outside_x);
34                Ok(CommandStatus::Done)
35            },
36        })
37        // this shows how to create Command manually with the help of the validator! macro
38        // one could also implement arguments validation manually
39        .add("outy", easy_repl::Command {
40            description: "Use mutably outside var y".into(),
41            args_info: vec!["appended".into()],
42            handler: Box::new(|args| {
43                let validator = validator!(i32);
44                validator(args)?;
45                outside_y += args[0];
46                println!("{}", outside_y);
47                Ok(CommandStatus::Done)
48            }),
49        })
50        .build().context("Failed to create repl")?;
51
52    repl.run().context("Critical REPL error")?;
53
54    Ok(())
55}