-
Notifications
You must be signed in to change notification settings - Fork 15
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adds 'head' option to allow user get the first specific number of ion…
… values of the input file.
- Loading branch information
Showing
6 changed files
with
104 additions
and
10 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::io::{stdout, Write}; | ||
use clap::{Arg, ArgMatches, Command}; | ||
use ion_rs::*; | ||
use std::fs::File; | ||
use anyhow::{Context, Result}; | ||
use crate::commands::dump; | ||
|
||
pub fn app() -> Command { | ||
Command::new("head") | ||
.about("Prints the specified number of top-level values in the input stream.") | ||
.arg( | ||
Arg::new("number") | ||
.long("number") | ||
.short('n') | ||
.allow_negative_numbers(false) | ||
.help("Specifies the number of output top-level values.") | ||
) | ||
.arg( | ||
Arg::new("format") | ||
.long("format") | ||
.short('f') | ||
.default_value("lines") | ||
.value_parser(["binary", "text", "pretty", "lines"]) | ||
.help("Output format"), | ||
) | ||
.arg( | ||
Arg::new("input") | ||
.required(true) | ||
.help("Input file") | ||
) | ||
} | ||
|
||
pub fn run(_command_name: &str, matches: &ArgMatches) -> Result<()> { | ||
let input_file = matches | ||
.get_one::<String>("input") | ||
.unwrap(); | ||
let number: i32 = matches | ||
.get_one::<String>("number") | ||
.unwrap() | ||
.parse() | ||
.unwrap(); | ||
let format = matches | ||
.get_one::<String>("format") | ||
.expect("`format` did not have a value"); | ||
let file = File::open(input_file) | ||
.with_context(|| format!("Could not open file '{}'", input_file))?; | ||
if number == 0 { | ||
return Ok(()); | ||
} | ||
let mut reader = ReaderBuilder::new().build(file)?; | ||
let mut output: Box<dyn Write> = Box::new(stdout().lock()); | ||
dump::write_all_in_format(&mut reader, &mut output, format, number).expect("Could not process the input stream."); | ||
Ok(()) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,5 @@ | ||
use anyhow::Result; | ||
use clap::{ArgMatches, Command}; | ||
|
||
pub mod beta; | ||
pub mod dump; | ||
|
||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
foo: bar, | ||
abc: [123, 456] | ||
} | ||
{ | ||
foo: baz, | ||
abc: [42.0, 43e0] | ||
} | ||
{ | ||
foo: bar, | ||
test: data | ||
} | ||
{ | ||
foo: baz, | ||
type: struct | ||
} |