init commit
This commit is contained in:
4
.gitignore
vendored
4
.gitignore
vendored
@@ -3,10 +3,6 @@
|
||||
# will have compiled files and executables
|
||||
/target/
|
||||
|
||||
# Remove Cargo.lock from gitignore if creating an executable, leave it for libraries
|
||||
# More information here https://doc.rust-lang.org/cargo/guide/cargo-toml-vs-cargo-lock.html
|
||||
Cargo.lock
|
||||
|
||||
# These are backup files generated by rustfmt
|
||||
**/*.rs.bk
|
||||
|
||||
|
||||
1154
Cargo.lock
generated
Normal file
1154
Cargo.lock
generated
Normal file
File diff suppressed because it is too large
Load Diff
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "hardownload"
|
||||
version = "0.1.0"
|
||||
authors = ["Hatter Jiang <jht5945@gmail.com>"]
|
||||
edition = "2018"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
tokio = { version = "0.2.6", features = ["full"] }
|
||||
clap = "2.33.1"
|
||||
reqwest = "0.10.6"
|
||||
serde = { version = "1.0", features = ["derive"] }
|
||||
serde_json = "1.0"
|
||||
12
src/cmd.rs
Normal file
12
src/cmd.rs
Normal 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
24
src/cmd_default.rs
Normal 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
20
src/cmd_sample.rs
Normal 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
49
src/har.rs
Normal 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
32
src/main.rs
Normal 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)
|
||||
}
|
||||
Reference in New Issue
Block a user