| Crates.io | embedded_commands_rs |
| lib.rs | embedded_commands_rs |
| version | 0.1.7 |
| created_at | 2025-08-17 14:40:06.535423+00 |
| updated_at | 2025-08-20 00:09:37.391829+00 |
| description | Lightweight, embeddable command interpreter for Rust |
| homepage | |
| repository | |
| max_upload_size | |
| id | 1799442 |
| size | 13,246 |
Note: this documentation is currently incorrect and will be updated in the update to 0.1.8.
Embedded_commands_rs is a simple library for adding commands to rust applications.
This library contains two distinct types, the Interpreter and Token types.
The interpreter type represents an instance of a command interpreter and has the following methods:
The token Type represents an enum which contains any of 4 types:
command1(1,2,3) // this command takes three integers.
command2(1,1.15,true,"abc") // this command takes 4 arguments and expects them to be of the types Token::Int, Token::Float, Token::Str and Token::Bool.
use std::io::{self, Read};
use embedded_commands_rs::{Interpreter, Token};
#[derive(Debug)]
struct State {
counter: i64,
}
fn main() {
let mut state = State { counter: 0 };
let mut interp = Interpreter::new();
interp.register("print", print);
interp.register("increment", counter_inc);
interp.register("decrement", counter_dec);
interp.register("reveal_counter", counter_show);
let mut script = String::new();
io::stdin().read_to_string(&mut script).expect("Failed to read input");
interp.interpret(&mut state, &script);
}
// --- Commands ---
fn print(_state: &mut State, args: &[Token]) {
if let Some(Token::Str(content)) = args.get(0) {
println!("{}", content);
} else {
eprintln!("print expects a string argument in parentheses, e.g., print(\"Hello\")");
}
}
fn counter_inc(state: &mut State, _args: &[Token]) {
state.counter += 1;
}
fn counter_dec(state: &mut State, _args: &[Token]) {
state.counter -= 1;
}
fn counter_show(state: &mut State, _args: &[Token]) {
println!("counter.value = {}", state.counter);
}