-
Notifications
You must be signed in to change notification settings - Fork 39
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor: use derive apis to parse command line arguments
- Loading branch information
1 parent
f29d8b0
commit 5d084ea
Showing
9 changed files
with
336 additions
and
222 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
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
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 @@ | ||
pub(crate) mod run; |
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,59 @@ | ||
use std::ffi::OsStr; | ||
|
||
use clap::{builder::TypedValueParser, Parser}; | ||
|
||
use common_config_parser::types::{Config, JsonValueParser}; | ||
use common_version::Version; | ||
use core_run::{Axon, KeyProvider}; | ||
use protocol::types::RichBlock; | ||
|
||
use crate::{ | ||
error::{Error, Result}, | ||
utils, | ||
}; | ||
|
||
#[derive(Parser, Debug)] | ||
#[command(about = "Run axon process")] | ||
pub struct RunArgs { | ||
#[arg(short = 'c', long = "config", help = "Axon config path")] | ||
pub config: Config, | ||
#[arg(short = 'g', long = "genesis", help = "Axon genesis path")] | ||
#[arg(value_parser=RichBlockValueParser)] | ||
pub genesis: RichBlock, | ||
} | ||
|
||
#[derive(Clone, Debug)] | ||
struct RichBlockValueParser; | ||
|
||
impl TypedValueParser for RichBlockValueParser { | ||
type Value = RichBlock; | ||
|
||
fn parse_ref( | ||
&self, | ||
cmd: &clap::Command, | ||
arg: Option<&clap::Arg>, | ||
value: &OsStr, | ||
) -> Result<Self::Value, clap::Error> { | ||
JsonValueParser::<RichBlock>::default().parse_ref(cmd, arg, value) | ||
} | ||
} | ||
|
||
impl RunArgs { | ||
pub(crate) fn execute<K: KeyProvider>( | ||
self, | ||
application_version: Version, | ||
kernel_version: Version, | ||
key_provider: Option<K>, | ||
) -> Result<()> { | ||
let Self { config, genesis } = self; | ||
utils::check_version( | ||
&config.data_path_for_version(), | ||
&kernel_version, | ||
utils::latest_compatible_version(), | ||
)?; | ||
utils::register_log(&config); | ||
Axon::new(application_version.to_string(), config, genesis) | ||
.run(key_provider) | ||
.map_err(Error::Running) | ||
} | ||
} |
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,33 @@ | ||
use std::io; | ||
|
||
use common_version::Version; | ||
use thiserror::Error; | ||
|
||
use protocol::ProtocolError; | ||
|
||
#[non_exhaustive] | ||
#[derive(Error, Debug)] | ||
pub enum Error { | ||
// Boxing so the error type isn't too large (clippy::result-large-err). | ||
#[error(transparent)] | ||
CheckingVersion(Box<CheckingVersionError>), | ||
#[error("reading data version: {0}")] | ||
ReadingVersion(#[source] io::Error), | ||
#[error("writing data version: {0}")] | ||
WritingVersion(#[source] io::Error), | ||
|
||
#[error(transparent)] | ||
Running(ProtocolError), | ||
} | ||
|
||
#[non_exhaustive] | ||
#[derive(Error, Debug)] | ||
#[cfg_attr(test, derive(PartialEq, Eq))] | ||
#[error("data version({data}) is not compatible with the current axon version({current}), version >= {least_compatible} is supported")] | ||
pub struct CheckingVersionError { | ||
pub current: Version, | ||
pub data: Version, | ||
pub least_compatible: Version, | ||
} | ||
|
||
pub type Result<T, E = Error> = std::result::Result<T, E>; |
Oops, something went wrong.