use crate::brief_display::BriefDisplay;
use anyhow::Context;
use cbor_data::Cbor;
use clap::Parser;
use std::{
fs::File,
io::{stdin, stdout},
};
mod brief_display;
#[derive(Parser, Debug)]
struct Args {
#[clap(short, long, default_value = "-")]
input: String,
#[clap(short, long, default_value = "-")]
output: String,
#[clap(short, long)]
string: bool,
#[clap(short, long)]
censored_properties: Vec<String>,
#[clap(short = 'd', long, default_value = "10")]
max_depth: usize,
#[clap(short, long, default_value = "10")]
array_length: usize,
#[clap(short, long, default_value = "10")]
text_length: usize,
#[clap(short, long)]
quiet: bool,
}
fn main() {
let args = Args::parse();
let mut input = if args.input == "-" {
Box::new(stdin()) as Box<dyn std::io::Read>
} else {
Box::new(
File::open(&args.input)
.context(format!("opening input file `{}`", args.input))
.unwrap(),
)
};
let mut output = if args.output == "-" {
Box::new(stdout()) as Box<dyn std::io::Write>
} else {
Box::new(
File::create(&args.output)
.context(format!("opening output file `{}`", args.output))
.unwrap(),
)
};
let mut read_buf = Vec::new();
read_buf.resize(1048576, 0u8);
let read_buf = read_buf.as_mut_slice();
let mut data_buf = Vec::new();
let mut count = 0;
while let Ok(n) = input.read(read_buf) {
if n == 0 {
break;
}
data_buf.extend_from_slice(&read_buf[..n]);
let mut rest = &data_buf[..];
while let Ok((cbor, r)) = Cbor::checked_prefix(rest) {
count += 1;
if args.string {
writeln!(
&mut output,
"{}",
BriefDisplay {
cbor,
max_depth: args.max_depth,
array_length: args.array_length,
censored_properties: &args.censored_properties,
text_length: args.text_length,
}
)
.unwrap();
}
rest = r;
}
if rest.len() < data_buf.len() {
let len = rest.len();
let start = data_buf.len() - len;
data_buf.copy_within(start.., 0);
data_buf.truncate(len);
}
}
if !args.quiet {
eprintln!("Processed {} items", count);
}
}