Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: use macro apply in resolvers #104

Open
wants to merge 10 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .changeset/poor-roses-cry.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
---
'archons': patch
---

Use macro apply for resolvers
2 changes: 1 addition & 1 deletion __test__/global.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { Context, defineCommand, run } from '../index'
import { type Context, defineCommand, run } from '../index.js'

test('global option', (t) => {
const dev = defineCommand({
Expand Down
4 changes: 2 additions & 2 deletions __test__/index.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import { spawnSync } from 'child_process'

import { Command, defineCommand, run } from '../index'
import { type Context, Command, defineCommand, run } from '../index.js'

const cmd: Command = {
meta: {
Expand All @@ -15,7 +15,7 @@ const cmd: Command = {
action: 'set',
},
},
callback: (_: any) => {},
callback: (_: Context) => {},
}

const main = defineCommand(cmd)
Expand Down
2 changes: 1 addition & 1 deletion __test__/options.spec.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import test from 'ava'
import { spawnSync } from 'child_process'

import { Context, defineCommand, run } from '../index'
import { type Context, defineCommand, run } from '../index.js'

test('positional option', (t) => {
const main = defineCommand({
Expand Down
2 changes: 1 addition & 1 deletion __test__/subcommand.spec.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import test from 'ava'

import { Context, defineCommand, run } from '../index'
import { Context, defineCommand, run } from '../index.js'

test('sub command', (t) => {
const cmd = defineCommand({
Expand Down
3 changes: 2 additions & 1 deletion examples/global.cts
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import { Context, defineCommand, run } from '..'
import { type Context, defineCommand, run } from '..'

const dev = defineCommand({
meta: {
Expand All @@ -9,6 +9,7 @@ const dev = defineCommand({
console.log(ctx.args.config)
},
})

const main = defineCommand({
meta: {
name: 'test',
Expand Down
20 changes: 13 additions & 7 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,12 @@
"url": "https://github.com/fu050409"
},
"main": "index.js",
"exports": {
".": {
"import": "./index.js",
"require": "./index.js"
}
},
"type": "commonjs",
"repository": "https://github.com/noctisynth/archons.git",
"license": "MIT",
Expand Down Expand Up @@ -65,20 +71,20 @@
"postinstall": "husky"
},
"devDependencies": {
"@changesets/cli": "^2.27.11",
"@changesets/cli": "^2.28.1",
"@napi-rs/cli": "^2.18.4",
"@swc-node/register": "^1.10.9",
"@swc/core": "^1.10.9",
"@swc/core": "^1.10.18",
"@taplo/cli": "^0.7.0",
"@types/node": "^22.10.10",
"@types/node": "^22.13.5",
"ava": "^6.2.0",
"chalk": "^5.4.1",
"husky": "^9.1.7",
"lint-staged": "^15.4.2",
"lint-staged": "^15.4.3",
"npm-run-all2": "^7.0.2",
"oxlint": "^0.15.7",
"prettier": "^3.4.2",
"tinybench": "^3.1.0",
"oxlint": "^0.15.11",
"prettier": "^3.5.2",
"tinybench": "^3.1.1",
"typescript": "^5.7.3"
},
"lint-staged": {
Expand Down
50 changes: 50 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,53 @@ pub mod types;
pub mod utils;

pub type HashMap<K, V> = rustc_hash::FxHashMap<K, V>;

#[macro_export]
macro_rules! apply_opt {
($arg:ident, $opt:ident, $field:ident => $method:ident) => {
if let Some(val) = $opt.$field {
$arg = $arg.$method(val);
}
};
($arg:ident, $opt:ident, &$field:ident => $method:ident) => {
if let Some(val) = &$opt.$field {
$arg = $arg.$method(val);
}
};
($arg:ident, $opt:ident, $wrapper:ident($field:ident) => $method:ident) => {
if let Some(val) = $opt.$field {
$arg = $arg.$method($wrapper(val));
}
};
($arg:ident, $opt:ident, $wrapper:ident(&$field:ident) => $method:ident) => {
if let Some(val) = &$opt.$field {
$arg = $arg.$method($wrapper(val));
}
};
($arg:ident, $opt:ident, $wrapper:ident!($field:ident) => $method:ident) => {
if let Some(val) = $opt.$field {
$arg = $arg.$method($wrapper!(val));
}
};
($arg:ident, $opt:ident, $wrapper:ident!(&$field:ident) => $method:ident) => {
if let Some(val) = &$opt.$field {
$arg = $arg.$method($wrapper!(val));
}
};
($arg:ident, $opt:ident, $field:ident) => {
apply_opt!($arg, $opt, $field => $field);
};
($arg:ident, $opt:ident, &$field:ident) => {
apply_opt!($arg, $opt, &$field => $field);
};
}

#[macro_export]
macro_rules! to_char_vec {
($vec:ident) => {
$vec
.into_iter()
.map(|c| c.chars().next().unwrap())
.collect::<Vec<char>>()
};
}
96 changes: 24 additions & 72 deletions src/resolver.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use crate::{
apply_opt, to_char_vec,
types::{Command, CommandMeta, CommandOption},
utils::{leak_borrowed_str, leak_borrowed_str_or_default, leak_str},
HashMap,
Expand Down Expand Up @@ -38,17 +39,9 @@ pub(crate) fn resolve_command_meta(
);
clap = clap.name(name);

if let Some(version) = &meta.version {
clap = clap.version(leak_borrowed_str(version));
}

if let Some(about) = &meta.about {
clap = clap.about(leak_borrowed_str(about));
}

if let Some(subcommand_required) = meta.subcommand_required {
clap = clap.subcommand_required(subcommand_required);
}
apply_opt!(clap, meta, leak_borrowed_str(&version) => version);
apply_opt!(clap, meta, leak_borrowed_str(&about) => about);
apply_opt!(clap, meta, subcommand_required);

if meta.styled.unwrap_or(false) {
use clap::builder::styling;
Expand All @@ -63,17 +56,17 @@ pub(crate) fn resolve_command_meta(
clap
}

pub(crate) fn resolve_action(action: &Option<String>, type_: &Option<String>) -> clap::ArgAction {
let type_ = type_.as_deref().unwrap_or("option");
pub(crate) fn resolve_action(action: &Option<String>, r#type: &Option<String>) -> clap::ArgAction {
let r#type = r#type.as_deref().unwrap_or("option");
match action.as_deref() {
Some("set") => clap::ArgAction::Set,
Some("append") => clap::ArgAction::Append,
Some("count") => clap::ArgAction::Count,
Some("store") => clap::ArgAction::SetTrue,
Some("store_false") => clap::ArgAction::SetFalse,
None => match type_ {
None => match r#type {
"option" | "positional" => clap::ArgAction::Set,
_ => panic!("Unsupported type: {:?}", type_),
_ => panic!("Unsupported type: {:?}", r#type),
},
_ => panic!("Unsupported action: {:?}", action),
}
Expand Down Expand Up @@ -176,63 +169,22 @@ pub(crate) fn resolve_command_options(
);
}
arg = arg.value_parser(resolve_parser(opt.parser.as_deref(), opt.action.as_deref()));
if let Some(alias) = &opt.alias {
arg = arg.visible_aliases(alias);
}
if let Some(hidden_alias) = &opt.hidden_alias {
arg = arg.aliases(hidden_alias);
}
if let Some(short_alias) = &opt.short_alias {
let short_alias = short_alias
.iter()
.map(|s| s.chars().next().unwrap())
.collect::<Vec<char>>();
arg = arg.visible_short_aliases(short_alias);
}
if let Some(hidden_short_alias) = &opt.hidden_short_alias {
let hidden_short_alias = hidden_short_alias
.iter()
.map(|s| s.chars().next().unwrap())
.collect::<Vec<char>>();
arg = arg.short_aliases(hidden_short_alias);
}
if let Some(value_hint) = &opt.value_hint {
arg = arg.value_hint(resolve_value_hint(value_hint.as_str()));
}
if let Some(help) = &opt.help {
arg = arg.help(help);
}
if let Some(required) = opt.required {
arg = arg.required(required);
}
if let Some(default) = &opt.default {
arg = arg.default_value(default);
}
if let Some(default_missing) = opt.default_missing {
arg = arg.default_missing_value(default_missing);
}
if let Some(num_args) = opt.num_args {
let num_args = resolve_num_args(num_args);
arg = arg.num_args(num_args);
}
if let Some(required_equals) = opt.required_equals {
arg = arg.require_equals(required_equals);
}
if let Some(hidden) = opt.hidden {
arg = arg.hide(hidden);
}
if let Some(global) = opt.global {
arg = arg.global(global);
}
if let Some(exclusive) = opt.exclusive {
arg = arg.exclusive(exclusive);
}
if let Some(conflicts_with) = &opt.conflicts_with {
arg = arg.conflicts_with_all(conflicts_with);
}
if let Some(hide_default_value) = opt.hide_default_value {
arg = arg.hide_default_value(hide_default_value);
}
apply_opt!(arg, opt, &alias => visible_aliases);
apply_opt!(arg, opt, &hidden_alias => aliases);
apply_opt!(arg, opt, to_char_vec!(&short_alias) => short_aliases);
apply_opt!(arg, opt, to_char_vec!(&hidden_short_alias) => short_aliases);
apply_opt!(arg, opt, resolve_value_hint(&value_hint) => value_hint);
apply_opt!(arg, opt, &help);
apply_opt!(arg, opt, required);
apply_opt!(arg, opt, default => default_value);
apply_opt!(arg, opt, default_missing => default_missing_value);
apply_opt!(arg, opt, resolve_num_args(num_args) => num_args);
apply_opt!(arg, opt, required_equals => require_equals);
apply_opt!(arg, opt, hidden => hide);
apply_opt!(arg, opt, global);
apply_opt!(arg, opt, exclusive);
apply_opt!(arg, opt, &conflicts_with => conflicts_with_all);
apply_opt!(arg, opt, hide_default_value);
arg
})
.collect::<Vec<clap::Arg>>(),
Expand Down
Loading
Loading