Skip to main content

Crate take_bytes

Crate take_bytes 

Source
Expand description

§take_bytes

A lightweight utility for reading input from stdin or a file path, designed for seamless integration with clap’s derive API.

§Overview

take_bytes provides a TakeBytes type that implements FromStr, allowing it to be used directly as a clap argument type. When the user passes "-", input is read from stdin; otherwise, the argument is treated as a file path.

§Example

use clap::Parser;
use take_bytes::TakeBytes;

#[derive(Parser)]
struct Cli {
    /// Input file or "-" for stdin
    #[arg(default_value = "-")]
    input: TakeBytes,
}

fn main() -> Result<(), Box<dyn std::error::Error>> {
    let cli = Cli::parse();
    let contents = cli.input.try_into_bytes()?;
    println!("Read {} bytes", contents.len());
    Ok(())
}

Structs§

TakeBytes
A source of bytes from either stdin or a file path.

Enums§

DecodingError
Errors that can occur when decoding input.
Source
Represents the source of input data.

Functions§

decode_utf8_string
Read the entire contents from the input source and decode as UTF-8.
read_as_bytes
Read the entire contents from the input source as bytes.