brc 0.1.0

Library providing a simple, quick console for Rust projects.
Documentation
  • Coverage
  • 59.09%
    13 out of 22 items documented1 out of 9 items with examples
  • Size
  • Source code size: 60.36 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.26 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Links
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • tduck973564
brc-0.1.0 has been yanked.

A fast and easy console library.

This provides a basic, minimal framework over making a console for applications.

Syntax

command string_argument "string argument with spaces" 33 4.8

Note that 33 and 4.8 are two different types, Integer and Float.

Examples

use brc::prelude::*;

fn main() {
    let mut command_table: HashMap<String, Command>= HashMap::new();
    command_table.insert("add".to_string(), add);
    let console = Console::new(command_table, ">> ");
    console.run_repl();
}

fn add(mut args: Arguments) -> Result<(), BrcError> {
    let mut sum = 0;
    for _ in 0..args.len() {
        let arg: i32 = args.pop_arg()?.try_into()?;
        sum += arg;
    }
    println!("{}", sum);
    Ok(())
}

Writing functions that work with the Console

Every function that you write to be used in the Console must follow this signature: fn(mut args: brc::lex::Arguments) -> Result<(), brc::error::Error>. To use user-inputted arguments in your function, you must continually pop the command arguments from the args parameter, and then convert them into the type you expect. Example below:

fn echo(mut args: Arguments) -> Result<(), BrcError> {
    let first_argument: String = args.pop_arg()?.try_into()?; // Expects a String, as per the type annotation. You can expect a f64, i32 or String.
    println!("{}", first_argument);
    Ok(())
}