Skip to content

Commit

Permalink
flyttat till monorepo
Browse files Browse the repository at this point in the history
  • Loading branch information
vincent-thomas committed Sep 28, 2023
1 parent 87000e2 commit efff132
Show file tree
Hide file tree
Showing 45 changed files with 241 additions and 192 deletions.
14 changes: 13 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

33 changes: 2 additions & 31 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,31 +1,2 @@
[package]
name = "clier"
version = "0.5.0"
edition = "2021"
authors = ["Vincent Thomas"]
description = "A cli parser and framework for rust"
license = "MIT"
repository = "https://github.com/vincent-thomas/clier"
keywords = ["cli", "parser", "parse"]
documentation = "https://docs.rs/clier"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
macros = []

[[bench]]
name = "parser_bench"
harness = false

[dependencies]
console = "0.15.7"
serde = {version = "1.0.188", features = ["derive"]}
serde_json = "1.0.106"
thiserror = "1.0.48"

[dev-dependencies]
criterion = "0.5.1"

[workspace]
members = ["clier", "clier_app", "clier_parser"]
9 changes: 3 additions & 6 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@


default:
@cargo clippy
@cargo clippy -q

build:
@cargo build

build-release:
@cargo build -q --release

build-examples:
@cargo build -q --examples
@cargo build --release

test:
@cargo test
@cargo nextest r && cargo test --doc

doc:
@cargo doc --no-deps
Expand Down
3 changes: 0 additions & 3 deletions clier.config.json

This file was deleted.

31 changes: 31 additions & 0 deletions clier/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
[package]
name = "clier"
version = "0.5.0"
edition = "2021"
authors = ["Vincent Thomas"]
description = "A cli parser and framework for rust"
license = "MIT"
repository = "https://github.com/vincent-thomas/clier"
keywords = ["cli", "parser", "parse"]
documentation = "https://docs.rs/clier"

[package.metadata.docs.rs]
all-features = true
rustdoc-args = ["--cfg", "docsrs"]

[features]
macros = []

[[bench]]
name = "parser_bench"
harness = false

[dependencies]
console = "0.15.7"
thiserror = "1.0.48"
clier_parser = { version = "0.5.0", path = "../clier_parser" }


[dev-dependencies]
criterion = "0.5.1"

File renamed without changes.
File renamed without changes.
File renamed without changes.
File renamed without changes.
4 changes: 2 additions & 2 deletions src/builder/command.rs → clier/src/builder/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,8 +17,8 @@ pub struct CmdArgs {
pub(crate) struct RunnableCommand {
/// The function to run command.
pub handler: Handler,
// / Usage of the command. Displayed in help.
// pub usage: Option<String>,
/// Usage of the command. Displayed in help.
pub usage: Option<String>,
/// Registered Flags that are required for command to run. Passed down with [crate::hooks::use_flags] hook.
pub flags: Option<Vec<RFlag>>,
/// The description of the command.
Expand Down
File renamed without changes.
File renamed without changes.
File renamed without changes.
3 changes: 1 addition & 2 deletions src/lib.rs → clier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,9 +64,8 @@ pub mod macros;
/// Run
pub mod run;

mod parser;
mod prelude;
pub use parser::Argv;
pub use clier_parser::Argv;

use run::Meta;
use std::env::args;
Expand Down
File renamed without changes.
File renamed without changes.
16 changes: 11 additions & 5 deletions src/run/help.rs → clier/src/run/help.rs
Original file line number Diff line number Diff line change
@@ -1,10 +1,12 @@
use std::collections::HashMap;

use crate::builder::{RFlag, RunnableCommand};
use crate::prelude::*;
use crate::Meta;
use crate::{prelude::*, Argv};
use console::{style, Term};

use super::resolver::command_fetcher;

fn help_renderer(
root_command: HashMap<String, &RunnableCommand>,
name: String,
Expand Down Expand Up @@ -56,7 +58,7 @@ pub(crate) fn help(commands: &HashMap<String, RunnableCommand>, args: &[String],
options.name.to_string()
};

let matcher = commands.get(args.join(".").as_str());
// let matcher = commands.get(args.join(".").as_str());
let children: Vec<(String, &RunnableCommand)> = if args.is_empty() {
commands
.iter()
Expand All @@ -77,7 +79,10 @@ pub(crate) fn help(commands: &HashMap<String, RunnableCommand>, args: &[String],
})
.collect()
};
if commands.get(args.join(".").as_str()).is_none() {

let (args, main_command) = command_fetcher(&Argv::from(args), commands.clone());

if main_command.is_none() {
if args.is_empty() {
help_renderer(
HashMap::from_iter(children),
Expand All @@ -90,12 +95,13 @@ pub(crate) fn help(commands: &HashMap<String, RunnableCommand>, args: &[String],
}
return;
};
let flags = if let Some(m) = matcher { m.clone().flags.unwrap_or(vec![]) } else { vec![] };
let flags =
if let Some(ref m) = main_command { m.clone().flags.unwrap_or(vec![]) } else { vec![] };

help_renderer(
HashMap::from_iter(children),
prog_name,
options.usage,
main_command.unwrap().usage,
options.version,
options.description,
flags,
Expand Down
4 changes: 2 additions & 2 deletions src/run/mod.rs → clier/src/run/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,10 @@ pub struct Meta {
pub name: String,
/// Description of the binary application
pub description: String,
/// Usage examples. Used for clearance in help command
pub usage: Option<String>,
/// Version
pub version: String,
/// Usage examples. Used for clearance in help command
pub usage: Option<String>,
}

/// Trait Runnable
Expand Down
95 changes: 18 additions & 77 deletions src/run/resolver/command.rs → clier/src/run/resolver/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ fn format_commands(registered_commands: &[RCommand]) -> HashMap<String, Runnable
name,
RunnableCommand {
handler: val.handler,
// usage: val.usage,
usage: val.usage,
flags: val.flags,
description: val.description,
},
Expand All @@ -62,18 +62,10 @@ fn format_commands(registered_commands: &[RCommand]) -> HashMap<String, Runnable
HashMap::from_iter(commands_vec)
}

pub(crate) fn resolve_command(argv: &Argv, registered_commands: &[RCommand]) -> Action {
let commands = format_commands(registered_commands);
match global_flags(argv) {
FlagsAction::ShowHelp => return Action::ShowHelp(commands),
FlagsAction::ShowVersion => return Action::ShowVersion,
FlagsAction::Nothing => {}
};

if let Some(command_to_run) = commands.get(argv.commands.join(".").as_str()) {
return Action::RunCommand(argv.commands.join("."), command_to_run.clone());
}

pub(crate) fn command_fetcher(
argv: &Argv,
commands: HashMap<String, RunnableCommand>,
) -> (Vec<String>, Option<RunnableCommand>) {
let valid_args = argv
.commands
.iter()
Expand All @@ -86,76 +78,25 @@ pub(crate) fn resolve_command(argv: &Argv, registered_commands: &[RCommand]) ->
.collect::<Vec<String>>();

let mut command = commands.get(&valid_args.join("."));

if command.is_none() && valid_args.is_empty() {
command = commands.get("root");
}
(valid_args, command.cloned())
}

pub(crate) fn resolve_command(argv: &Argv, registered_commands: &[RCommand]) -> Action {
let commands = format_commands(registered_commands);
match global_flags(argv) {
FlagsAction::ShowHelp => return Action::ShowHelp(commands),
FlagsAction::ShowVersion => return Action::ShowVersion,
FlagsAction::Nothing => {}
};

let (valid_args, command) = command_fetcher(argv, commands.clone());

if let Some(command_to_run) = command {
Action::RunCommand(valid_args.join("."), command_to_run.clone())
Action::RunCommand(valid_args.join("."), command_to_run)
} else {
Action::ShowHelp(commands)
}
}

// Write test cases for the following functions:
// - global_flags
// - format_commands
// - resolve_command
#[test]
fn test_flags() {
let action = resolve_command(
&Argv {
commands: vec!["command".to_string(), "subcommands".to_string()],
flags: HashMap::from([
("version".to_string(), "true".to_string()),
("help".to_string(), "false".to_string()),
]),
},
&[],
);

assert_eq!(action, Action::ShowVersion);

let action = resolve_command(
&Argv {
commands: vec!["command".to_string(), "subcommands".to_string()],
flags: HashMap::from([
("version".to_string(), "false".to_string()),
("help".to_string(), "true".to_string()),
]),
},
&[],
);

assert_eq!(action, Action::ShowHelp(HashMap::new()));
}

#[test]
fn test_resolve_commands() {
// Måste vara funktion p.g.a man jämför minnesadresser till funktionen.
let handler = |_args| 0;

let action = resolve_command(
&Argv {
commands: vec!["command".to_string(), "subcommands".to_string()],
flags: HashMap::new(),
},
&[RCommand {
name: "command".to_string(),
description: "description".to_string(),
handler,
usage: None,
flags: None,
children: None,
}],
);

assert_eq!(
action,
Action::RunCommand(
"command".to_string(),
RunnableCommand { description: "description".to_string(), handler, flags: None }
)
);
}
File renamed without changes.
2 changes: 2 additions & 0 deletions src/run/resolver/mod.rs → clier/src/run/resolver/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,5 @@ mod command;
mod flag;
pub use command::*;
pub use flag::*;

mod tests;
Loading

0 comments on commit efff132

Please sign in to comment.