Skip to content

Commit 9c2a420

Browse files
committed
clean up custom command registration
1 parent a150ce2 commit 9c2a420

File tree

5 files changed

+15
-59
lines changed

5 files changed

+15
-59
lines changed

README.md

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -228,11 +228,10 @@ run_cmd!(info "This is an infomation message")?;
228228
```
229229

230230
#### Macros to register your own commands
231-
Declare your function with `#[export_cmd(..)]` attribute, and import it with [`use_custom_cmd!`](https://docs.rs/cmd_lib/latest/cmd_lib/macro.use_custom_cmd.html) macro:
231+
Declare your function with the right signature, and register it with [`use_custom_cmd!`](https://docs.rs/cmd_lib/latest/cmd_lib/macro.use_custom_cmd.html) macro:
232232

233233
```rust
234-
#[export_cmd(my_cmd)]
235-
fn foo(env: &mut CmdEnv) -> CmdResult {
234+
fn my_cmd(env: &mut CmdEnv) -> CmdResult {
236235
let msg = format!("msg from foo(), args: {:?}", env.args());
237236
writeln!(env.stderr(), "{}", msg)?;
238237
writeln!(env.stdout(), "bar")

macros/src/lib.rs

Lines changed: 6 additions & 46 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
1-
use proc_macro2::{Span, TokenStream, TokenTree};
1+
use proc_macro2::{TokenStream, TokenTree};
22
use proc_macro_error::{abort, proc_macro_error};
3-
use quote::{quote, ToTokens};
3+
use quote::quote;
44

55
/// Mark main function to log error result by default.
66
///
@@ -40,50 +40,10 @@ pub fn main(
4040
.into()
4141
}
4242

43-
/// Export the function as an command to be run by [`run_cmd!`] or [`run_fun!`].
44-
///
45-
/// ```
46-
/// # use cmd_lib::*;
47-
/// # use std::io::Write;
48-
/// #[export_cmd(my_cmd)]
49-
/// fn foo(env: &mut CmdEnv) -> CmdResult {
50-
/// let msg = format!("msg from foo(), args: {:?}\n", env.args());
51-
/// writeln!(env.stderr(), "{}", msg)?;
52-
/// writeln!(env.stdout(), "bar")
53-
/// }
54-
///
55-
/// use_custom_cmd!(my_cmd);
56-
/// run_cmd!(my_cmd)?;
57-
/// println!("get result: {}", run_fun!(my_cmd)?);
58-
/// # Ok::<(), std::io::Error>(())
59-
/// ```
60-
/// Here we export function `foo` as `my_cmd` command.
61-
62-
#[proc_macro_attribute]
63-
pub fn export_cmd(
64-
attr: proc_macro::TokenStream,
65-
item: proc_macro::TokenStream,
66-
) -> proc_macro::TokenStream {
67-
let cmd_name = attr.to_string();
68-
let export_cmd_fn = syn::Ident::new(&format!("export_cmd_{}", cmd_name), Span::call_site());
69-
70-
let orig_function: syn::ItemFn = syn::parse2(item.into()).unwrap();
71-
let fn_ident = &orig_function.sig.ident;
72-
73-
let mut new_functions = orig_function.to_token_stream();
74-
new_functions.extend(quote! (
75-
fn #export_cmd_fn() {
76-
export_cmd(#cmd_name, #fn_ident);
77-
}
78-
));
79-
new_functions.into()
80-
}
81-
8243
/// Import user registered custom command.
8344
/// ```
8445
/// # use cmd_lib::*;
85-
/// #[export_cmd(my_cmd)]
86-
/// fn foo(env: &mut CmdEnv) -> CmdResult {
46+
/// fn my_cmd(env: &mut CmdEnv) -> CmdResult {
8747
/// let msg = format!("msg from foo(), args: {:?}\n", env.args());
8848
/// writeln!(env.stderr(), "{}", msg)?;
8949
/// writeln!(env.stdout(), "bar")
@@ -105,15 +65,15 @@ pub fn use_custom_cmd(item: proc_macro::TokenStream) -> proc_macro::TokenStream
10565
abort!(t, "only comma is allowed");
10666
}
10767
} else if let TokenTree::Ident(cmd) = t {
108-
let cmd_fn = syn::Ident::new(&format!("export_cmd_{}", cmd), Span::call_site());
109-
cmd_fns.push(cmd_fn);
68+
let cmd_name = cmd.to_string();
69+
cmd_fns.push(quote!(&#cmd_name, #cmd));
11070
} else {
11171
abort!(t, "expect a list of comma separated commands");
11272
}
11373
}
11474

11575
quote! (
116-
#(#cmd_fns();)*
76+
#(::cmd_lib::register_cmd(#cmd_fns);)*
11777
)
11878
.into()
11979
}

src/lib.rs

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -251,13 +251,12 @@
251251
//! ```
252252
//!
253253
//! ### Macros to register your own commands
254-
//! Declare your function with `#[export_cmd(..)]` attribute, and import it with [`use_custom_cmd!`](https://docs.rs/cmd_lib/latest/cmd_lib/macro.use_custom_cmd.html) macro:
254+
//! Declare your function with the right signature, and register it with [`use_custom_cmd!`](https://docs.rs/cmd_lib/latest/cmd_lib/macro.use_custom_cmd.html) macro:
255255
//!
256256
//! ```
257257
//! # use cmd_lib::*;
258258
//! # use std::io::Write;
259-
//! #[export_cmd(my_cmd)]
260-
//! fn foo(env: &mut CmdEnv) -> CmdResult {
259+
//! fn my_cmd(env: &mut CmdEnv) -> CmdResult {
261260
//! let msg = format!("msg from foo(), args: {:?}", env.args());
262261
//! writeln!(env.stderr(), "{}", msg)?;
263262
//! writeln!(env.stdout(), "bar")
@@ -363,7 +362,7 @@
363362
//!
364363
365364
pub use cmd_lib_macros::{
366-
cmd_die, export_cmd, main, run_cmd, run_fun, spawn, spawn_with_output, use_custom_cmd,
365+
cmd_die, main, run_cmd, run_fun, spawn, spawn_with_output, use_custom_cmd,
367366
};
368367
/// Return type for [`run_fun!()`] macro.
369368
pub type FunResult = std::io::Result<String>;
@@ -378,7 +377,7 @@ pub use logger::try_init_default_logger;
378377
pub use main_error::MainError;
379378
pub use main_error::MainResult;
380379
#[doc(hidden)]
381-
pub use process::{export_cmd, AsOsStr, Cmd, CmdString, Cmds, GroupCmds, Redirect};
380+
pub use process::{register_cmd, AsOsStr, Cmd, CmdString, Cmds, GroupCmds, Redirect};
382381
pub use process::{set_debug, set_pipefail, CmdEnv};
383382

384383
mod builtins;

src/process.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,7 @@ lazy_static! {
7979
}
8080

8181
#[doc(hidden)]
82-
pub fn export_cmd(cmd: &'static str, func: FnFun) {
82+
pub fn register_cmd(cmd: &'static str, func: FnFun) {
8383
CMD_MAP.lock().unwrap().insert(OsString::from(cmd), func);
8484
}
8585

tests/test_macros.rs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -188,15 +188,13 @@ fn test_proc_env() {
188188
#[test]
189189
fn test_export_cmd() {
190190
use std::io::Write;
191-
#[export_cmd(my_cmd)]
192-
fn foo(env: &mut CmdEnv) -> CmdResult {
191+
fn my_cmd(env: &mut CmdEnv) -> CmdResult {
193192
let msg = format!("msg from foo(), args: {:?}", env.args());
194193
writeln!(env.stderr(), "{}", msg)?;
195194
writeln!(env.stdout(), "bar")
196195
}
197196

198-
#[export_cmd(my_cmd2)]
199-
fn foo2(env: &mut CmdEnv) -> CmdResult {
197+
fn my_cmd2(env: &mut CmdEnv) -> CmdResult {
200198
let msg = format!("msg from foo2(), args: {:?}", env.args());
201199
writeln!(env.stderr(), "{}", msg)?;
202200
writeln!(env.stdout(), "bar2")

0 commit comments

Comments
 (0)