From a68d201bbaf01433fd30c3e8f6d4cb7033868ca9 Mon Sep 17 00:00:00 2001 From: Michal Date: Thu, 21 Jul 2022 00:13:28 +0100 Subject: [PATCH] Code optimise --- src/args.rs | 32 ++++++++++++++++---------------- src/internal/mod.rs | 4 ++-- src/internal/strings.rs | 3 ++- src/main.rs | 3 +-- src/operations/config.rs | 3 ++- src/operations/init.rs | 3 ++- src/operations/mod.rs | 12 ++++++------ src/operations/prune.rs | 3 ++- src/operations/pull.rs | 3 ++- src/repository/config.rs | 5 +++-- src/repository/mod.rs | 8 ++++---- src/repository/package.rs | 5 +++-- src/workspace/mod.rs | 4 ++-- src/workspace/read.rs | 4 ++-- 14 files changed, 49 insertions(+), 43 deletions(-) diff --git a/src/args.rs b/src/args.rs index fecad99..f2e73ec 100644 --- a/src/args.rs +++ b/src/args.rs @@ -1,71 +1,71 @@ use clap::{ArgAction, Parser, Subcommand}; #[derive(Debug, Clone, Parser)] -#[clap(name="Malachite", version=env!("CARGO_PKG_VERSION"), about=env!("CARGO_PKG_DESCRIPTION"))] +#[clap(name = "Malachite", version = env ! ("CARGO_PKG_VERSION"), about = env ! ("CARGO_PKG_DESCRIPTION"))] pub struct Args { #[clap(subcommand)] pub subcommand: Option, /// Sets the level of verbosity - #[clap(long, short, global(true), action=ArgAction::Count)] + #[clap(long, short, global(true), action = ArgAction::Count)] pub verbose: u8, /// Complete operations without prompting user - #[clap(long="noconfirm", global(true), action=ArgAction::SetTrue)] + #[clap(long = "noconfirm", global(true), action = ArgAction::SetTrue)] pub no_confirm: bool, } #[derive(Debug, Clone, Subcommand)] pub enum Operation { /// Builds the given packages - #[clap(name="build", aliases=&["b"])] + #[clap(name = "build", aliases = & ["b"])] Build { /// The packages to operate on - #[clap(name="package(s)", action=ArgAction::Append, index=1)] + #[clap(name = "package(s)", action = ArgAction::Append, index = 1)] packages: Vec, /// Builds all packages in mlc.toml (except if -x is specified) - #[clap(long="all", takes_value=false, action=ArgAction::SetTrue, conflicts_with="package(s)")] + #[clap(long = "all", takes_value = false, action = ArgAction::SetTrue, conflicts_with = "package(s)")] all: bool, /// Excludes packages from given operation - #[clap(short='x', long="exclude", action=ArgAction::Append, takes_value=true)] + #[clap(short = 'x', long = "exclude", action = ArgAction::Append, takes_value = true)] exclude: Vec, /// Does not regenerate repository after building given package(s) - #[clap(short='n', long="no-regen", action=ArgAction::SetTrue)] + #[clap(short = 'n', long = "no-regen", action = ArgAction::SetTrue)] no_regen: bool, }, /// Generates repository from built packages - #[clap(name="repo-gen", aliases=&["r"])] + #[clap(name = "repo-gen", aliases = & ["r"])] RepoGen, /// Prunes duplicate packages from the repository - #[clap(name="prune", aliases=&["p"])] + #[clap(name = "prune", aliases = & ["p"])] Prune, /// Clones all git repositories from mlc.toml branching from current directory - #[clap(name="init", aliases=&["i"])] + #[clap(name = "init", aliases = & ["i"])] Init, /// Pulls in git repositories from mlc.toml branching from current directory - #[clap(name="pull", aliases=&["u"])] + #[clap(name = "pull", aliases = & ["u"])] Pull { /// The packages to operate on - #[clap(name="package(s)", help="The packages to operate on", action=ArgAction::Append, index=1)] + #[clap(name = "package(s)", help = "The packages to operate on", action = ArgAction::Append, index = 1)] packages: Vec, /// Pulls from all git repositories from mlc.toml branching from current directory - #[clap(long="all", action=ArgAction::SetTrue, conflicts_with="package(s)")] + #[clap(long = "all", action = ArgAction::SetTrue, conflicts_with = "package(s)")] all: bool, /// Excludes packages from given operation - #[clap(short='x', long="exclude", action=ArgAction::Append, takes_value=true)] + #[clap(short = 'x', long = "exclude", action = ArgAction::Append, takes_value = true)] exclude: Vec, }, /// Create and/or open local config file - #[clap(name="config", aliases=&["c"])] + #[clap(name = "config", aliases = & ["c"])] Config, } diff --git a/src/internal/mod.rs b/src/internal/mod.rs index c68dd1d..b9a7664 100755 --- a/src/internal/mod.rs +++ b/src/internal/mod.rs @@ -1,5 +1,5 @@ +pub use exit_codes::*; + mod exit_codes; pub mod strings; pub mod structs; - -pub use exit_codes::*; diff --git a/src/internal/strings.rs b/src/internal/strings.rs index 15028c6..903f9e4 100755 --- a/src/internal/strings.rs +++ b/src/internal/strings.rs @@ -1,7 +1,8 @@ -use crate::internal::AppExitCode; use colored::*; use std::process::exit; +use crate::internal::AppExitCode; + const LOGO_SYMBOL: &str = "μ"; const ERR_SYMBOL: &str = "❌"; diff --git a/src/main.rs b/src/main.rs index 2ecd9ac..d101fb7 100755 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,4 @@ +use clap::Parser; use std::env; use std::path::Path; use std::process::Command; @@ -5,8 +6,6 @@ use std::process::Command; use crate::args::{Args, Operation}; use crate::internal::AppExitCode; use crate::repository::create_config; -use clap::Parser; - use crate::workspace::read_cfg; #[global_allocator] diff --git a/src/operations/config.rs b/src/operations/config.rs index 4e102cf..2d02f1b 100644 --- a/src/operations/config.rs +++ b/src/operations/config.rs @@ -1,8 +1,9 @@ -use crate::create_config; use std::env; use std::path::Path; use std::process::Command; +use crate::create_config; + pub fn config() { if !Path::exists("mlc.toml".as_ref()) { create_config(); diff --git a/src/operations/init.rs b/src/operations/init.rs index 6cd638b..32c2f84 100644 --- a/src/operations/init.rs +++ b/src/operations/init.rs @@ -1,6 +1,7 @@ +use std::process::Command; + use crate::internal::AppExitCode; use crate::{crash, info, workspace}; -use std::process::Command; pub fn init() { let config = workspace::read_cfg(); diff --git a/src/operations/mod.rs b/src/operations/mod.rs index f3e1f81..514bbc2 100644 --- a/src/operations/mod.rs +++ b/src/operations/mod.rs @@ -1,11 +1,11 @@ -mod build; -mod config; -mod init; -mod prune; -mod pull; - pub use build::*; pub use config::*; pub use init::*; pub use prune::*; pub use pull::*; + +mod build; +mod config; +mod init; +mod prune; +mod pull; diff --git a/src/operations/prune.rs b/src/operations/prune.rs index 52ac06b..2bb15a2 100644 --- a/src/operations/prune.rs +++ b/src/operations/prune.rs @@ -1,7 +1,8 @@ -use crate::{info, read_cfg}; use std::fs; use std::process::Command; +use crate::{info, read_cfg}; + pub fn prune() { let config = read_cfg(); let mut packages = vec![]; diff --git a/src/operations/pull.rs b/src/operations/pull.rs index 737f9c4..088baa3 100644 --- a/src/operations/pull.rs +++ b/src/operations/pull.rs @@ -1,7 +1,8 @@ -use crate::info; use std::env; use std::process::Command; +use crate::info; + fn do_the_pulling(packages: Vec) { for dir in packages { let current_dir = env::current_dir().unwrap(); diff --git a/src/repository/config.rs b/src/repository/config.rs index 12a616a..c2274fe 100644 --- a/src/repository/config.rs +++ b/src/repository/config.rs @@ -1,10 +1,11 @@ -use crate::crash; -use crate::internal::AppExitCode; use std::env; use std::fs::File; use std::io::Write; use std::path::Path; +use crate::crash; +use crate::internal::AppExitCode; + const DEFAULT_CONFIG: &str = r#"# either "repository" or "workspace" mode = "" diff --git a/src/repository/mod.rs b/src/repository/mod.rs index 0d2ce29..59534d6 100755 --- a/src/repository/mod.rs +++ b/src/repository/mod.rs @@ -1,7 +1,7 @@ -mod config; -mod package; -mod repo; - pub use config::*; pub use package::*; pub use repo::*; + +mod config; +mod package; +mod repo; diff --git a/src/repository/package.rs b/src/repository/package.rs index 2e1de55..602298e 100755 --- a/src/repository/package.rs +++ b/src/repository/package.rs @@ -1,9 +1,10 @@ -use crate::crash; -use crate::internal::AppExitCode; use std::path::Path; use std::process::Command; use std::{env, fs}; +use crate::crash; +use crate::internal::AppExitCode; + pub fn build(pkg: &str) -> i32 { let dir = env::current_dir().unwrap(); if !Path::exists("out".as_ref()) { diff --git a/src/workspace/mod.rs b/src/workspace/mod.rs index cd2e912..aa2213b 100755 --- a/src/workspace/mod.rs +++ b/src/workspace/mod.rs @@ -1,3 +1,3 @@ -mod read; - pub use read::*; + +mod read; diff --git a/src/workspace/read.rs b/src/workspace/read.rs index 4ad3649..ac167e5 100755 --- a/src/workspace/read.rs +++ b/src/workspace/read.rs @@ -1,9 +1,9 @@ -use crate::crash; -use crate::internal::AppExitCode; use std::fs; use std::path::Path; +use crate::crash; use crate::internal::structs::{Config, SplitRepo, UnexpandedConfig}; +use crate::internal::AppExitCode; pub fn read_cfg() -> Config { if !Path::exists("mlc.toml".as_ref()) {