altio 0.2.0

Automating command tools by simulating piped io in process.
Documentation
  • Coverage
  • 100%
    53 out of 53 items documented0 out of 50 items with examples
  • Size
  • Source code size: 41.14 kB This is the summed size of all the files inside the crates.io package for this release.
  • Documentation size: 5.18 MB This is the summed size of all files generated by rustdoc for all configured targets
  • Ø build duration
  • this release: 10s Average build duration of successful builds.
  • all releases: 10s Average build duration of successful builds in releases after 2024-10-23.
  • Links
  • oooutlk/altio
    1 0 0
  • crates.io
  • Dependencies
  • Versions
  • Owners
  • oooutlk

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.