Skip to content

Commit

Permalink
Command-line hint argument
Browse files Browse the repository at this point in the history
  • Loading branch information
scottanderson committed Jan 21, 2024
1 parent 15bb632 commit da33e1e
Showing 1 changed file with 27 additions and 4 deletions.
31 changes: 27 additions & 4 deletions src/bin/gvas2json.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use clap::{Parser, ValueEnum};
use colored_json::{ColorMode, Output};
use gvas::GvasFile;
use minus::Pager;
use std::collections::HashMap;
use std::fs::File;
use std::io::{BufReader, BufWriter, Cursor, Read, Write};

Expand All @@ -17,6 +18,10 @@ struct Args {
#[clap(short, long, id = "OUTPUT_FILE")]
output: Option<String>,

/// Type hints as key-value pairs.
#[clap(short = 't', long = "type")]
types: Vec<String>,

/// Pretty-print JSON output.
#[clap(short, long, id = "PRETTY", default_value_t = true)]
pretty: bool,
Expand All @@ -30,6 +35,21 @@ struct Args {
no_pager: bool,
}

fn parse_types(args: Vec<String>) -> HashMap<String, String> {
let mut result = HashMap::<String, String>::new();
for arg in args {
let parts: Vec<&str> = arg.split('=').collect();
if parts.len() == 2 {
let k = String::from(parts[0]);
let v = String::from(parts[1]);
result.insert(k, v);
} else {
panic!("Hint should be in the format key=value")
}
}
result
}

#[derive(ValueEnum, Clone, Debug)]
enum WhenValues {
/// Always show colored output.
Expand Down Expand Up @@ -60,10 +80,13 @@ fn main() -> Result<()> {
WhenValues::Never => ColorMode::Off,
};

// Parse type hint arguments
let types = parse_types(args.types);

// Read from input
let gvas = match args.input {
None => from_reader(std::io::stdin()),
Some(input) => from_reader(File::open(input)?),
None => from_reader(std::io::stdin(), &types),
Some(input) => from_reader(File::open(input)?, &types),
}?;

// Transcode the data
Expand Down Expand Up @@ -99,14 +122,14 @@ fn format_json(gvas: &GvasFile, color_mode: ColorMode, pretty: bool) -> Result<S
Ok(json)
}

fn from_reader<R: Read>(input: R) -> Result<GvasFile> {
fn from_reader<R: Read>(input: R, types: &HashMap<String, String>) -> Result<GvasFile> {
let mut input = BufReader::new(input);
// WORKAROUND: GvasFile requires Seek attribute
let mut buf = Vec::new();
input.read_to_end(&mut buf)?;
let mut input = Cursor::new(buf);
// END WORKAROUND
Ok(GvasFile::read(&mut input)?)
Ok(GvasFile::read_with_hints(&mut input, types)?)
}

fn to_writer<W: Write>(writer: W, output: &[u8]) -> Result<()> {
Expand Down

0 comments on commit da33e1e

Please sign in to comment.