altio

Crates.ioaltio
lib.rsaltio
version0.2.0
sourcesrc
created_at2023-12-26 06:00:04.520081
updated_at2024-01-31 17:44:48.895859
descriptionAutomating command tools by simulating piped io in process.
homepage
repositoryhttps://github.com/oooutlk/altio
max_upload_size
id1080649
size41,496
(oooutlk)

documentation

https://docs.rs/altio

README

This crate helps to automating command tools by simulating piped io in process.

Why this crate

Interactive command tools utilize stdin, stdout and stderr for communication. If you want to use command tools as libraries(no spawning processes) and tool authors agree, this crate can help to automating input/output, just 3 steps:

  1. Define an Altio variable e.g. let io = Altio::default();.

  2. Replace std APIs with altio's equivalents, e.g. replace println!(...) with writeln!( io.out(), ... ), replace std::io::stdin() with io.input().

  3. Keep main.rs as simple as possible, e.g. fn main() { the_tool::run( std::env::args_os() )}.

Example for tool authors

[dependencies]
altio = { version = "0.2", no_default_features = true }

[features]
altio = ["altio/altio"]
// lib.rs
pub struct TheTool {
    // fields omitted
    pub io: Altio,
}

impl_altio_output!( TheTool );

When building the tool as an application, the "altio" feature is disabled and altio falls back to stdio.

When building the tool as a library, the tool users can invoke send/recv methods to communicate with the tool, e.g. send_line(), try_recv_line().

Example for tool users

the_tool = { version = "1.0", features = ["altio"] }
let args = std::env::args_os(); // clap::Parser::parse_from()
let tool = the_tool::new();
let tool_io = tool.io.clone();

// `io.input().read_line()` called occasionally
std::thread::spawn( || tool.run( args ));

loop {
    if let Some( received ) = tool_io.try_recv_line() {
        if received == "Lorum" {
            tool_io.send_line( "Ipsum" );
        }
    }
}

License

Under Apache License 2.0 or MIT License, at your will.

Commit count: 4

cargo fmt