-
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.
Prior to this PR, `to` and `from` were commands that each took a `format` flag that could be used to specify `json`. This patch promotes `to` and `from` into namespaces that each have a nested `json` command. This makes it easier to add support for other subcommands in `to` and `from`, and allows those subcommands to expose settings that only make sense for the corresponding format. (For example: configuring a catalog for Ion data, or a list of supported tags for CBOR.) Also fixes #36 by manually outputting a newline after writing any file in an Ion text format.
- Loading branch information
Showing
5 changed files
with
142 additions
and
117 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
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,42 @@ | ||
use crate::commands::CommandRunner; | ||
use anyhow::Result; | ||
use clap::{ArgMatches, Command}; | ||
|
||
pub mod json; | ||
|
||
// Creates a Vec of CLI configurations for all of the available built-in commands | ||
pub fn subcommands() -> Vec<Command> { | ||
vec![json::app()] | ||
} | ||
|
||
// Maps the given command name to the entry point for that command if it exists | ||
pub fn runner_for_from_command(command_name: &str) -> Option<CommandRunner> { | ||
let runner = match command_name { | ||
"json" => json::run, | ||
_ => return None, | ||
}; | ||
Some(runner) | ||
} | ||
|
||
// The functions below are used by the parent `beta` command when `to` is invoked. | ||
pub fn run(_command_name: &str, matches: &ArgMatches) -> Result<()> { | ||
// ^-- At this level of dispatch, this command will always be the text `to`. | ||
// We want to evaluate the name of the subcommand that was invoked --v | ||
let (command_name, command_args) = matches.subcommand().unwrap(); | ||
if let Some(runner) = runner_for_from_command(command_name) { | ||
runner(command_name, command_args)?; | ||
} else { | ||
let message = format!( | ||
"The requested `from` command ('{}') is not supported and clap did not generate an error message.", | ||
command_name | ||
); | ||
unreachable!("{}", message); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn app() -> Command { | ||
Command::new("from") | ||
.about("'from' is a namespace for commands that converts other data formats to Ion.") | ||
.subcommands(subcommands()) | ||
} |
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,42 @@ | ||
use crate::commands::CommandRunner; | ||
use anyhow::Result; | ||
use clap::{ArgMatches, Command}; | ||
|
||
pub mod json; | ||
|
||
// Creates a Vec of CLI configurations for all of the available built-in commands | ||
pub fn subcommands() -> Vec<Command> { | ||
vec![json::app()] | ||
} | ||
|
||
// Maps the given command name to the entry point for that command if it exists | ||
pub fn runner_for_to_command(command_name: &str) -> Option<CommandRunner> { | ||
let runner = match command_name { | ||
"json" => json::run, | ||
_ => return None, | ||
}; | ||
Some(runner) | ||
} | ||
|
||
// The functions below are used by the parent `beta` command when `to` is invoked. | ||
pub fn run(_command_name: &str, matches: &ArgMatches) -> Result<()> { | ||
// ^-- At this level of dispatch, this command will always be the text `to`. | ||
// We want to evaluate the name of the subcommand that was invoked --v | ||
let (command_name, command_args) = matches.subcommand().unwrap(); | ||
if let Some(runner) = runner_for_to_command(command_name) { | ||
runner(command_name, command_args)?; | ||
} else { | ||
let message = format!( | ||
"The requested `to` command ('{}') is not supported and clap did not generate an error message.", | ||
command_name | ||
); | ||
unreachable!("{}", message); | ||
} | ||
Ok(()) | ||
} | ||
|
||
pub fn app() -> Command { | ||
Command::new("to") | ||
.about("'to' is a namespace for commands that convert Ion to another data format.") | ||
.subcommands(subcommands()) | ||
} |
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