From eb7388c5d51e4ef3ea928d5f8f9e5b218cdbbd57 Mon Sep 17 00:00:00 2001 From: Sebastian Thiel Date: Mon, 15 Jun 2020 10:58:57 +0800 Subject: [PATCH] use structopt instead of clap many dependencies, but also convenience and safety --- Cargo.lock | 119 +++++++++++++++++++++++++++++++++++++++++++++++++++- Cargo.toml | 2 +- src/main.rs | 39 +++++++++-------- tasks.md | 8 ++++ 4 files changed, 148 insertions(+), 20 deletions(-) create mode 100644 tasks.md diff --git a/Cargo.lock b/Cargo.lock index 4a14012c767..7b54ab9cd23 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -171,8 +171,17 @@ name = "grit-cli" version = "1.0.0" dependencies = [ "anyhow", - "clap", "git-core", + "structopt", +] + +[[package]] +name = "heck" +version = "0.3.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "20564e78d53d2bb135c343b3f47714a56af2061f1c928fdb541dc7b9fdd94205" +dependencies = [ + "unicode-segmentation", ] [[package]] @@ -245,12 +254,56 @@ dependencies = [ "libc", ] +[[package]] +name = "proc-macro-error" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "98e9e4b82e0ef281812565ea4751049f1bdcdfccda7d3f459f2e138a40c08678" +dependencies = [ + "proc-macro-error-attr", + "proc-macro2", + "quote", + "syn", + "version_check", +] + +[[package]] +name = "proc-macro-error-attr" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4f5444ead4e9935abd7f27dc51f7e852a0569ac888096d5ec2499470794e2e53" +dependencies = [ + "proc-macro2", + "quote", + "syn", + "syn-mid", + "version_check", +] + +[[package]] +name = "proc-macro2" +version = "1.0.18" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "beae6331a816b1f65d04c45b078fd8e6c93e8071771f41b8163255bbd8d7c8fa" +dependencies = [ + "unicode-xid", +] + [[package]] name = "quick-error" version = "1.2.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a1d01941d82fa2ab50be1e79e6714289dd7cde78eba4c074bc5a4374f650dfe0" +[[package]] +name = "quote" +version = "1.0.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "aa563d17ecb180e500da1cfd2b028310ac758de548efdd203e18f283af693f37" +dependencies = [ + "proc-macro2", +] + [[package]] name = "rayon" version = "1.3.0" @@ -302,6 +355,52 @@ version = "0.8.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a" +[[package]] +name = "structopt" +version = "0.3.14" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "863246aaf5ddd0d6928dfeb1a9ca65f505599e4e1b399935ef7e75107516b4ef" +dependencies = [ + "clap", + "lazy_static", + "structopt-derive", +] + +[[package]] +name = "structopt-derive" +version = "0.4.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d239ca4b13aee7a2142e6795cbd69e457665ff8037aed33b3effdc430d2f927a" +dependencies = [ + "heck", + "proc-macro-error", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "syn" +version = "1.0.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5304cfdf27365b7585c25d4af91b35016ed21ef88f17ced89c7093b43dba8b6" +dependencies = [ + "proc-macro2", + "quote", + "unicode-xid", +] + +[[package]] +name = "syn-mid" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7be3539f6c128a931cf19dcee741c1af532c7fd387baa739c03dd2e96479338a" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + [[package]] name = "textwrap" version = "0.11.0" @@ -311,18 +410,36 @@ dependencies = [ "unicode-width", ] +[[package]] +name = "unicode-segmentation" +version = "1.6.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e83e153d1053cbb5a118eeff7fd5be06ed99153f00dbcd8ae310c5fb2b22edc0" + [[package]] name = "unicode-width" version = "0.1.7" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "caaa9d531767d1ff2150b9332433f32a24622147e5ebb1f26409d5da67afd479" +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" + [[package]] name = "vec_map" version = "0.8.2" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f1bddf1187be692e79c5ffeab891132dfb0f236ed36a43c7ed39f1165ee20191" +[[package]] +name = "version_check" +version = "0.9.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5a972e5669d67ba988ce3dc826706fb0a8b01471c088cb0b6110b805cc36aed" + [[package]] name = "walkdir" version = "2.3.1" diff --git a/Cargo.toml b/Cargo.toml index f6993f15893..e5a693c375e 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -13,9 +13,9 @@ test = false doctest = false [dependencies] -clap = "2.31.2" git-core = { version = "0.1.0", path = "git-core" } anyhow = "1.0.31" +structopt = "0.3.14" [profile.release] panic = 'unwind' diff --git a/src/main.rs b/src/main.rs index e8fc71c07f0..d3abdc20d29 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,29 +1,32 @@ -#[macro_use] -extern crate clap; -extern crate git_core as git; use anyhow::{Context, Result}; +use git_core; +use structopt::StructOpt; -mod app { - use clap::{App, AppSettings, SubCommand}; +mod options { + use structopt::StructOpt; - pub fn new<'a, 'b>() -> App<'a, 'b> { - let app: App = app_from_crate!(); - app.setting(AppSettings::SubcommandRequired).subcommand( - SubCommand::with_name("init") - .alias("initialize") - .about("Initialize the repository in the current directory."), - ) + #[derive(Debug, StructOpt)] + #[structopt(about = "The git, simply swift")] + #[structopt(setting = structopt::clap::AppSettings::SubcommandRequired)] + pub struct Args { + #[structopt(subcommand)] + pub cmd: Subcommands, + } + + #[derive(Debug, StructOpt)] + pub enum Subcommands { + /// Initialize the repository in the current directory. + #[structopt(alias = "initialize")] + Init, } } fn main() -> Result<()> { - let app = app::new(); - let matches = app.get_matches(); - match matches.subcommand() { - ("init", Some(_args)) => { - git::init::repository().with_context(|| "Repository initialization failed") + let args = options::Args::from_args(); + match args.cmd { + options::Subcommands::Init => { + git_core::init::repository().with_context(|| "Repository initialization failed") } - _ => unreachable!(), }?; Ok(()) } diff --git a/tasks.md b/tasks.md new file mode 100644 index 00000000000..8174aae01a5 --- /dev/null +++ b/tasks.md @@ -0,0 +1,8 @@ +### TODO + +* [x] remove failure and replace with quick-check/anyhow +* [x] minimize dependencies and test-runs +* [ ] structopt for grit +* [ ] make parser tests unit tests that are close to the code they test +* [ ] parser tests handle binary strings nicely +* [ ] switch parsing to nom (binary only)