Skip to content

Commit 603518e

Browse files
committed
Improve macro to allow errors and fallthrough cases
1 parent 0491ad8 commit 603518e

File tree

5 files changed

+20
-42
lines changed

5 files changed

+20
-42
lines changed

src/bin/mkdir.rs

Lines changed: 7 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,40 +1,19 @@
1-
use std::io::{Write, stdout};
1+
use std::io::stdout;
22

3-
use coreutils::{Result, get_umask, help_text, version_text};
3+
use coreutils::{Result, cli, get_umask};
44
use rustix::fs::{Mode, mkdir};
5-
use sap::{
6-
Argument::{Long, Value},
7-
Parser,
8-
};
9-
10-
const VERSION: &str = version_text!("mkdir");
11-
const HELP: &str = help_text!("mkdir");
125

136
fn main() -> Result {
147
let mut stdout = stdout();
158

16-
let mut arg_parser = Parser::from_env()?;
17-
189
let mut dirs = Vec::new();
1910

20-
while let Some(arg) = arg_parser.forward()? {
21-
match arg {
22-
Long("version") => {
23-
stdout.write_all(VERSION.as_bytes())?;
24-
stdout.flush()?;
25-
return Ok(());
26-
}
27-
Long("help") => {
28-
stdout.write_all(HELP.as_bytes())?;
29-
stdout.flush()?;
30-
return Ok(());
31-
}
32-
Value(value) => {
33-
dirs.push(value.to_owned()); // FIXME: I am not a fan of all this allocation but we can't easily move out of arg_parser
34-
}
35-
argument => return Err(argument.into_error(None).into()),
11+
cli! {
12+
"mkdir", stdout, #error
13+
Value(value) => {
14+
dirs.push(value.to_owned()); // FIXME: I am not a fan of all this allocation but we can't easily move out of the argument parser
3615
}
37-
}
16+
};
3817

3918
let mode = Mode::from_raw_mode(0o777) & !get_umask();
4019

src/bin/uname.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ fn main() -> Result {
2222
let mut stdout = stdout();
2323

2424
cli! {
25-
"uname", stdout, while,
25+
"uname", stdout, #error
2626
Short('a') | Long("all") => {
2727
info_mask = Info::all();
2828
}
@@ -34,7 +34,6 @@ fn main() -> Result {
3434
Short('p') | Long("processor") => info_mask |= Info::PROCESSOR
3535
Short('i') | Long("hardware-platform") => info_mask |= Info::HARDWARE_PLATFORM
3636
Short('o') | Long("operating-system") => info_mask |= Info::OPERATING_SYSTEM
37-
arg => return Err(arg.into_error(None).into())
3837
};
3938

4039
if info_mask.is_empty() {

src/bin/whoami.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -9,10 +9,7 @@ const CANNOT_FIND_UID: &[u8] = b"cannot find name for user ID: ";
99
fn main() -> Result {
1010
let mut stdout = stdout();
1111

12-
cli! {
13-
"whoami", stdout, if,
14-
arg => return Err(arg.into_error(None).into())
15-
};
12+
cli! ("whoami", stdout, #error);
1613

1714
let uid = geteuid();
1815

src/bin/yes.rs

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -13,11 +13,10 @@ fn main() -> Result {
1313
let mut buffer: Option<Vec<u8>> = None;
1414

1515
let arg_parser = cli! {
16-
"yes", stdout, while,
16+
"yes", stdout, #error
1717
Value(value) => {
1818
extend_buffer(&mut buffer, value.as_bytes().to_vec());
1919
}
20-
arg => return Err(arg.into_error(None).into())
2120
};
2221

2322
arg_parser

src/lib.rs

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -80,14 +80,14 @@ macro_rules! help_text {
8080
#[macro_export]
8181
#[doc(hidden)]
8282
macro_rules! _cli_impl {
83-
($name:literal, $stdout:ident, $loop_type:tt, $($item:pat => $matcher:expr)*) => {
83+
($name:literal, $stdout:ident, $($item:pat => $matcher:expr)*) => {
8484
{
8585
use std::io::Write;
8686
use sap::Argument::*;
8787

8888
let mut arg_parser = sap::Parser::from_env()?;
8989

90-
$loop_type let Some(arg) = arg_parser.forward()? {
90+
while let Some(arg) = arg_parser.forward()? {
9191
match arg {
9292
Long("version") => {
9393
$stdout.write_all($crate::version_text!($name).as_bytes())?;
@@ -110,12 +110,16 @@ macro_rules! _cli_impl {
110110

111111
#[macro_export]
112112
macro_rules! cli {
113-
($name:literal, $stdout:ident, while, $($item:pat => $matcher:expr)*) => {
114-
$crate::_cli_impl!($name, $stdout, while, $($item => $matcher)*)
113+
($name:literal, $stdout:ident, $($item:pat => $matcher:expr)*) => {
114+
$crate::_cli_impl!($name, $stdout, $($item => $matcher)*)
115115
};
116116

117-
($name:literal, $stdout:ident, if, $($item:pat => $matcher:expr)*) => {
118-
$crate::_cli_impl!($name, $stdout, if, $($item => $matcher)*)
117+
($name:literal, $stdout:ident, #fall $($item:pat => $matcher:expr)*) => {
118+
$crate::_cli_impl!($name, $stdout, $($item => $matcher)* _ => {})
119+
};
120+
121+
($name:literal, $stdout:ident, #error $($item:pat => $matcher:expr)*) => {
122+
$crate::_cli_impl!($name, $stdout, $($item => $matcher)* arg => return Err(arg.into_error(None).into()))
119123
};
120124
}
121125

0 commit comments

Comments
 (0)