Crates.io | krush-engine |
lib.rs | krush-engine |
version | 0.3.0 |
source | src |
created_at | 2023-10-13 03:18:00.584204 |
updated_at | 2023-10-16 14:03:20.458103 |
description | The engine that powers krush (the Kreative Rust Shell) |
homepage | |
repository | https://github.com/OJarrisonn/krush-engine |
max_upload_size | |
id | 1001949 |
size | 12,034 |
The krush-engine is a library that has a simple command parser and interpreter that is used for the krush shell interface, but can be used standalone.
Once you've defined an Engine
with all the commands you want using the register_command
method. You can call the method evaluate
with a String
which returns a Result
. The first word of this string is the command name, if you've registered a command with that name, the Engine
will parse the rest of the input to get the arguments you've setted. If everything goes right, your callback function will be called.
This project were written in a day to be used for my homework so there are several unimplemented features and potential bugs (actually, krush
doesn't even exists at the moment).
move
closureCell
or RefCell
(for types who doesn't implement the Copy
trait)definition!
macro which creates a new Definition
from an array of Type
and a closure.use std::cell::{Cell, RefCell};
use krush_engine::{Engine, Definition, Type, definition};
fn main() {
let last = RefCell::new(String::new());
let count = Cell::new(0);
let mut engine = Engine::new();
engine.register_command("print", definition!([Type::Str], |args| {
let text = args[0].unwrap_str().unwrap();
println!("{}", text);
*last.borrow_mut() = text;
count.set(count.get() + 1);
None
}));
let _ = engine.evaluate("print \"Hello World!\"".to_string());
println!("Last is: {}", last.borrow());
let _ = engine.evaluate("print \"This is Krush Engine!\"".to_string());
println!("Last is: {}", last.borrow());
let _ = engine.evaluate("print \"Thank You!\"".to_string());
println!("Last is: {}\nCount is {}", last.borrow(), count.get());
}
Output:
Hello World!
Last is: Hello World!
This is Krush Engine!
Last is: This is Krush Engine!
Thank You!
Last is: Thank You!
Count is 3