Removes use of 'Default' in CLI

This commit is contained in:
howardwu 2021-02-24 18:41:36 -08:00
parent a74a141e33
commit c61cd3459a
16 changed files with 43 additions and 137 deletions

View File

@ -36,16 +36,10 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Compile and build program command
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Build {}
impl Build {
pub fn new() -> Build {
Build {}
}
}
impl Command for Build {
type Input = ();
type Output = Option<(Compiler<'static, Fq, EdwardsGroupType>, bool)>;

View File

@ -23,16 +23,10 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Clean outputs folder command
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Clean {}
impl Clean {
pub fn new() -> Clean {
Clean {}
}
}
impl Command for Clean {
type Input = ();
type Output = ();

View File

@ -21,16 +21,10 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Deploy Leo program to the network
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Deploy {}
impl Deploy {
pub fn new() -> Deploy {
Deploy {}
}
}
impl Command for Deploy {
type Input = ();
type Output = ();

View File

@ -23,16 +23,10 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Init Leo project command within current directory
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Init {}
impl Init {
pub fn new() -> Init {
Init {}
}
}
impl Command for Init {
type Input = ();
type Output = ();

View File

@ -21,16 +21,10 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Lint Leo code command
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Lint {}
impl Lint {
pub fn new() -> Lint {
Lint {}
}
}
impl Command for Lint {
type Input = ();
type Output = ();

View File

@ -60,57 +60,55 @@ pub use watch::Watch;
// Aleo PM related commands
pub mod package;
/// Base trait for Leo CLI, see methods and their documentation for details
/// Base trait for the Leo CLI, see methods and their documentation for details.
pub trait Command {
/// If current command requires running another command before
/// and needs its output results, this is the place to set.
/// If the current command requires running another command beforehand
/// and needs its output result, this is where the result type is defined.
/// Example: type Input: <CommandA as Command>::Out
type Input;
/// Define output of the command to be reused as an Input for another
/// command. If this command is not used as a prelude for another, keep empty
/// Defines the output of this command, which may be used as `Input` for another
/// command. If this command is not used as a prelude for another command,
/// this field may be left empty.
type Output;
/// Returns project context, currently keeping it simple but it is possible
/// that in the future leo will not depend on current directory, and we're keeping
/// option for extending current core
/// Returns the project context, which is defined as the current directory.
fn context(&self) -> Result<Context> {
get_context()
}
/// Add span to the logger tracing::span.
/// Due to specifics of macro implementation it is impossible to set
/// span name with non-literal i.e. dynamic variable even if this
/// variable is &'static str
/// Adds a span to the logger via `tracing::span`.
/// Because of the specifics of the macro implementation, it is not possible
/// to set the span name with a non-literal i.e. a dynamic variable even if this
/// variable is a &'static str.
fn log_span(&self) -> Span {
tracing::span!(tracing::Level::INFO, "Leo")
}
/// Run prelude and get Input for current command. As simple as that.
/// But due to inability to pass default implementation of a type, this
/// method must be present in every trait implementation.
/// Runs the prelude and returns the Input of the current command.
fn prelude(&self) -> Result<Self::Input>
where
Self: std::marker::Sized;
/// Core of the execution - do what is necessary. This function is run within
/// context of 'execute' function, which sets logging and timers
/// Runs the main operation of this command. This function is run within
/// context of 'execute' function, which sets logging and timers.
fn apply(self, context: Context, input: Self::Input) -> Result<Self::Output>
where
Self: std::marker::Sized;
/// Wrapper around apply function, sets up tracing, time tracking and context
/// A wrapper around the `apply` method.
/// This function sets up tracing, timing, and the context.
fn execute(self) -> Result<Self::Output>
where
Self: std::marker::Sized,
{
let input = self.prelude()?;
// create span for this command
// Create the span for this command.
let span = self.log_span();
let span = span.enter();
// calculate execution time for each run
// Calculate the execution time for this command.
let timer = Instant::now();
let context = self.context()?;
@ -118,7 +116,7 @@ pub trait Command {
drop(span);
// use done context to print time
// Use the done context to print the execution time for this command.
tracing::span!(tracing::Level::INFO, "Done").in_scope(|| {
tracing::info!("Finished in {} milliseconds \n", timer.elapsed().as_millis());
});
@ -126,7 +124,7 @@ pub trait Command {
out
}
/// Execute command but empty the result. Comes in handy where there's a
/// Executes command but empty the result. Comes in handy where there's a
/// need to make match arms compatible while keeping implementation-specific
/// output possible. Errors however are all of the type Error
fn try_execute(self) -> Result<()>

View File

@ -30,12 +30,6 @@ pub struct New {
name: String,
}
impl New {
pub fn new(name: String) -> New {
New { name }
}
}
impl Command for New {
type Input = ();
type Output = ();

View File

@ -22,16 +22,10 @@ use structopt::StructOpt;
use tracing::Span;
/// Remove credentials for Aleo PM from .leo directory
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Logout {}
impl Logout {
pub fn new() -> Logout {
Logout {}
}
}
impl Command for Logout {
type Input = ();
type Output = ();

View File

@ -37,23 +37,17 @@ struct ResponseJson {
}
/// Publish package to Aleo Package Manager
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Publish {}
impl Publish {
pub fn new() -> Publish {
Publish {}
}
}
impl Command for Publish {
type Input = <Build as Command>::Output;
type Output = Option<String>;
/// Build program before publishing
fn prelude(&self) -> Result<Self::Input> {
Build::new().execute()
(Build {}).execute()
}
fn apply(self, context: Context, _input: Self::Input) -> Result<Self::Output> {

View File

@ -22,19 +22,13 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Remove imported package
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Remove {
#[structopt(name = "PACKAGE")]
name: String,
}
impl Remove {
pub fn new(name: String) -> Remove {
Remove { name }
}
}
impl Command for Remove {
type Input = ();
type Output = ();

View File

@ -28,17 +28,11 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Run the program and produce a proof
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Prove {
#[structopt(long = "skip-key-check", help = "Skip key verification on Setup stage")]
skip_key_check: bool,
}
impl Prove {
pub fn new(skip_key_check: bool) -> Prove {
Prove { skip_key_check }
}
pub(super) skip_key_check: bool,
}
impl Command for Prove {
@ -50,7 +44,8 @@ impl Command for Prove {
}
fn prelude(&self) -> Result<Self::Input> {
Setup::new(self.skip_key_check).execute()
let skip_key_check = self.skip_key_check;
(Setup { skip_key_check }).execute()
}
fn apply(self, context: Context, input: Self::Input) -> Result<Self::Output> {

View File

@ -26,19 +26,13 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Build, Prove and Run Leo program with inputs
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Run {
#[structopt(long = "skip-key-check", help = "Skip key verification on Setup stage")]
skip_key_check: bool,
}
impl Run {
pub fn new(skip_key_check: bool) -> Run {
Run { skip_key_check }
}
}
impl Command for Run {
type Input = <Prove as Command>::Output;
type Output = ();
@ -48,7 +42,8 @@ impl Command for Run {
}
fn prelude(&self) -> Result<Self::Input> {
Prove::new(self.skip_key_check).execute()
let skip_key_check = self.skip_key_check;
(Prove { skip_key_check }).execute()
}
fn apply(self, _context: Context, input: Self::Input) -> Result<Self::Output> {

View File

@ -31,17 +31,11 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Executes the setup command for a Leo program
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Setup {
#[structopt(long = "skip-key-check", help = "Skip key verification")]
skip_key_check: bool,
}
impl Setup {
pub fn new(skip_key_check: bool) -> Setup {
Setup { skip_key_check }
}
pub(super) skip_key_check: bool,
}
impl Command for Setup {
@ -57,7 +51,7 @@ impl Command for Setup {
}
fn prelude(&self) -> Result<Self::Input> {
Build::new().execute()
(Build {}).execute()
}
fn apply(self, context: Context, input: Self::Input) -> Result<Self::Output> {

View File

@ -32,19 +32,13 @@ use structopt::StructOpt;
use tracing::span::Span;
/// Build program and run tests command
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Test {
#[structopt(short = "f", long = "file", name = "file")]
files: Vec<PathBuf>,
}
impl Test {
pub fn new(files: Vec<PathBuf>) -> Test {
Test { files }
}
}
impl Command for Test {
type Input = ();
type Output = ();

View File

@ -30,7 +30,7 @@ pub enum Automatic {
}
/// Update Leo to the latest version
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Update {
/// List all available versions of Leo
@ -46,16 +46,6 @@ pub struct Update {
automatic: Option<Automatic>,
}
impl Update {
pub fn new(list: bool, studio: bool, automatic: Option<Automatic>) -> Update {
Update {
list,
studio,
automatic,
}
}
}
impl Command for Update {
type Input = ();
type Output = ();

View File

@ -27,7 +27,7 @@ use tracing::span::Span;
const LEO_SOURCE_DIR: &str = "src/";
/// Watch file changes in src/ directory and run Build Command
#[derive(StructOpt, Debug, Default)]
#[derive(StructOpt, Debug)]
#[structopt(setting = structopt::clap::AppSettings::ColoredHelp)]
pub struct Watch {
/// Set up watch interval
@ -35,12 +35,6 @@ pub struct Watch {
interval: u64,
}
impl Watch {
pub fn new(interval: u64) -> Watch {
Watch { interval }
}
}
impl Command for Watch {
type Input = ();
type Output = ();
@ -70,7 +64,7 @@ impl Command for Watch {
match rx.recv() {
// See changes on the write event
Ok(DebouncedEvent::Write(_write)) => {
match Build::new().execute() {
match (Build {}).execute() {
Ok(_output) => {
tracing::info!("Built successfully");
}