aboutsummaryrefslogtreecommitdiff

Mingling

Mìng Lìng - 命令

/mɪŋ lɪŋ/

Macro magician in your CLI.

License GitHub stars Crate size Crates.io version CI

What is Mingling?

Mingling is a state-driven and data-driven CLI workflow orchestration framework built in Rust.

Its name comes from the Chinese pinyin "Mìng Lìng", which means "command".

WARNING

Mingling is currently usable at a basic level, but it is still under active development, so many APIs are not yet mature. Any changes to the public API will be documented in detail in the Changelog.

Additionally, the project is currently developed by me alone (Weicao-CatilGrass). If you are interested in this project, I warmly welcome your contributions. You can reach me directly via Github Issue.

About Mingling's Design

Mingling aims to organize and manage the architectural concerns of command-line programs through reasonable abstractions: it breaks a program down into the following concepts:

Concept Description
Command A combination of Dispatcher and Chain
Dispatcher Maps user input to entry types
Chain Provides behavioral logic for any type and returns the next type
Renderer Renders any type into output-ready text
Resource Provides global data for the program
Hook Provides global behavior for the program

Example

Below is a typical Mingling program that demonstrates how to implement a simple adder:

// Features: ["mini"]

#[derive(Grouped)]
struct ResultNumber(f32);

#[command]
fn sum(args: Entry) -> ResultNumber {
    let (a, b) = args
        .pick(&arg![f32])
        .pick(&arg![f32])
        .unwrap();
    ResultNumber(a + b)
}

#[renderer]
fn render_number(n: ResultNumber) -> String {
    format!("Result is {}", n.0)
}

Output:

~# my-cli sum 5 10
Result is 15

If we add full error handling, it would look like this:

// Features: ["mini"]

use mingling::macros::routeify;
use mingling::setup::ExitCodeSetup;
use mingling::res::ResExitCode;

fn main() {
    let mut program = ThisProgram::new();
    program.with_setup(ExitCodeSetup);
    program.exec_and_exit();
}

#[derive(Grouped)]
struct ResultNumber(f32);

#[derive(Grouped)]
struct ErrorNoNumber;

#[command(routeify)]
fn sum(args: Entry) -> Next {
    let (a, b) = args
        .pick_or_route(&arg![f32], || ErrorNoNumber.into())
        .pick_or_route(&arg![f32], || ErrorNoNumber.into())
        .to_result()?;
    ResultNumber(a + b).into()
}

#[renderer]
fn render_error_no_num(n: ErrorNoNumber, ec: &mut ResExitCode) -> String {
    ec.exit_code = 1;
    format!("Error: No number provided.")
}

#[renderer]
fn render_number(n: ResultNumber) -> String {
    format!("Result is {}", n.0)
}

Output:

~# my-cli sum 5 10
Result is 15

~# my-cli sum
Error: No number provided. << 1

See! By assembling ExitCodeSetup and modifying ResExitCode, we explicitly mark the side effect of changing the exit code on the render_error_no_num function. This is exactly the problem Mingling aims to solve: separating concerns by separating side effects through architecture.

Of course, by combining the concepts above, you can elegantly separate the side effects of your program into independent resources, keeping your execution functions pure.

Getting Started

Add Mingling to your Cargo.toml:

[dependencies.mingling]
version = "0.5.0"
features = []

Or use the github version

[dependencies.mingling]
git = "https://github.com/mingling-rs/mingling.git"
tag = "unreleased"
features = []

To learn more, check out Writing with Mingling

[!Note] You can also use the mling scaffolding tool to build, check, and manage your project Download | About

Unplanned Features

While Mingling has several common CLI features that are NOT PLANNED to be directly included in the framework. This is because the Rust ecosystem already has excellent and mature crates to handle these issues, and Mingling's design is intended to be used in combination with them.

  • Colored Output: To add color and styles (bold, italic, etc.) to terminal output, consider using crates like colored or owo-colors. You can integrate their types directly into your renderers.
  • I18n: To translate your CLI application, the rust-i18n crate provides a powerful internationalization solution that you can use in your command logic and renderers.
  • Progress Bars: To display progress indicators, the indicatif crate is the standard choice.
  • TUI: To build full-screen interactive terminal applications, it is recommended to use a framework like ratatui (formerly tui-rs).

Learn More

To learn more, check out the following links:

License

This project is licensed under the MIT License.

See LICENSE-MIT or LICENSE-APACHE file for details.