Giter Site home page Giter Site logo

command-engine-rework's Introduction

Command Engine

Transform string instructions into code execution.

Engine

This crate provides a default engine, that is present if default features are enabled. It is simply a container for your Commands that can also execute them based on the input.

There are 2 versions of the default Engine:

  • sync - Default.
  • async - Enabled with async feature.

Instruction

The given input is deserialized into a specific format defined in Instruction structure.

It allows for one Command caller, multiple positional args, additional o_args which act like flags that can contain sub_args.

Format:

<caller> <arg> <arg> --<o_arg> <sub_arg> <sub_arg> --<o_arg>

Example:

example argument1 argument2 --flag2 child1 child2 --flag3 --flag1

Deserializes to:

Instruction {
    caller: "example",
    args: vec!["argument1", "argument2"],
    o_args: {
        let mut map = HashMap::new();
        map.insert("--flag2", Some(vec!["child1", "child2"]));
        map.insert("--flag3", None);
        map.insert("--flag1", None);
        map
    },
    input: "example argument1 argument2 --flag2 child1 child2 --flag3 --flag1",
};

To add spaces in arguments use double quotes ":

example "argument 1" "--flag 2" "child 1"

If there are double quotes in the argument it's suggested to use collector #:

example #"argument "quotes" 1"#

There are no escape characters to avoid any heap allocations. Each argument is a string slice taken from the input.

Example

Sync version:

use command_engine::*;

pub struct Example;

impl CommandInfo for Example {
    fn caller(&self) -> &'static str {
        "ex"
    }
}

impl Command for Example {
    type Output = String;

    fn on_execute(&self, ins: Instruction) -> Self::Output {
        format!("{:?}", ins)
    }
}

fn main() {
    let input = "ex arg --o_arg sub_arg";
    
    let mut engine = Engine::new();
    engine.insert(Example);
    
    // Will return formatted string of the Instruction.
    let output = engine.execute(input).unwrap();
    println!("{}", output);
}

Disclaimer

ToDo (in future):

  • Custom Instructions (just like the Outputs)
  • Integrated help command

Versioning:

  • *.*.* - Released.
  • *.*.*-rc.* - Release Candidate.
  • *.*.*-dev - Unreleased in production.
  • 0.*.* - Deprecated.

command-engine-rework's People

Contributors

fssay avatar

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.