init commit

This commit is contained in:
2020-06-17 08:21:04 +08:00
parent f81ec4a117
commit c58d9a8770
8 changed files with 1305 additions and 4 deletions

12
src/cmd.rs Normal file
View File

@@ -0,0 +1,12 @@
use clap::{ ArgMatches, App, };
pub type CommandError = Result<(), Box<dyn std::error::Error>>;
pub trait Command {
fn subcommand<'a>(&self) -> App<'a, 'a>;
fn name(&self) -> &str;
fn run(&self, arg_matches: &ArgMatches, _: &ArgMatches) -> CommandError;
}

24
src/cmd_default.rs Normal file
View File

@@ -0,0 +1,24 @@
use clap::{ App, Arg, ArgMatches, };
use crate::cmd::CommandError;
pub struct CommandDefault;
impl CommandDefault {
pub fn process_command<'a>(app: App<'a, 'a>) -> App<'a, 'a> {
app.arg(Arg::with_name("verbose")
.long("verbose")
.short("v")
.multiple(true)
.help("Show verbose info")
)
}
pub fn run(arg_matches: &ArgMatches) -> CommandError {
let verbose_count = arg_matches.occurrences_of("verbose");
println!("[INFO] Verbose count: {}", verbose_count);
println!("[INFO] This is default command cli ...");
// TODO ...
Ok(())
}
}

20
src/cmd_sample.rs Normal file
View File

@@ -0,0 +1,20 @@
use clap::{ ArgMatches, SubCommand, App, };
use crate::cmd::{ Command, CommandError, };
pub struct CommandSample;
impl Command for CommandSample {
fn subcommand<'a>(&self) -> App<'a, 'a> {
SubCommand::with_name(self.name()).about("Sample subcommand")
}
fn name(&self) -> &str {
"sample"
}
fn run(&self, _arg_matches: &ArgMatches, _sub_arg_matches: &ArgMatches) -> CommandError {
println!("This is test command!");
Ok(())
}
}

49
src/har.rs Normal file
View File

@@ -0,0 +1,49 @@
use serde::{Serialize, Deserialize};
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarCreator {
name: String,
version: String,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarPageTimings {
#[serde(rename = "onContentLoad")]
on_content_load: f64,
#[serde(rename = "onLoad")]
on_load: f64,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarPage {
#[serde(rename = "startedDateTime")]
started_date_time: String,
id: String,
title: String,
#[serde(rename = "pageTimings")]
page_timings: HarPageTimings,
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarEntry {
// startedDateTime
time: f64,
// request
// response
// cache
// timings
// serverIPAddress
// _initiator
// _priority
// _resourceType
// connection
pageref: String, // @see Harpage::id
}
#[derive(Clone, Debug, Serialize, Deserialize)]
pub struct HarLog {
version: String,
creator: HarCreator,
pages: Vec<HarPage>,
entries: Vec<HarEntry>,
}

32
src/main.rs Normal file
View File

@@ -0,0 +1,32 @@
use clap::App;
mod cmd;
mod cmd_sample;
mod cmd_default;
mod har;
use cmd::{ Command, CommandError, };
use cmd_sample::CommandSample;
use cmd_default::CommandDefault;
fn main() -> CommandError {
let commands = vec![
CommandSample{},
];
let mut app = App::new(env!("CARGO_PKG_NAME"))
.version(env!("CARGO_PKG_VERSION"))
.about(env!("CARGO_PKG_DESCRIPTION"));
app = CommandDefault::process_command(app);
for command in &commands {
app = app.subcommand(command.subcommand());
}
let matches = app.get_matches();
for command in &commands {
if let Some(sub_cmd_matches) = matches.subcommand_matches(command.name()) {
return command.run(&matches, sub_cmd_matches);
}
}
CommandDefault::run(&matches)
}