1
Fork 0
mirror of https://github.com/RGBCube/uutils-coreutils synced 2025-08-01 05:27:45 +00:00

refactor/uucore ~ improve macro scope hygiene

This commit is contained in:
Roy Ivy III 2021-06-30 22:24:41 -05:00 committed by Michael Debertol
parent ed240a7e50
commit fa5dc90789

View file

@ -17,11 +17,11 @@ macro_rules! executable_os(
#[macro_export] #[macro_export]
macro_rules! executable( macro_rules! executable(
() => ({ () => ({
let exe = match executable_os!().to_str() { let exe = match $crate::executable_os!().to_str() {
// * UTF-8 // * UTF-8
Some(s) => s.to_string(), Some(s) => s.to_string(),
// * "lossless" debug format if `executable_os!()` is not well-formed UTF-8 // * "lossless" debug format if `executable_os!()` is not well-formed UTF-8
None => format!("{:?}", executable_os!()) None => format!("{:?}", $crate::executable_os!())
}; };
&exe.to_owned() &exe.to_owned()
}) })
@ -31,7 +31,7 @@ macro_rules! executable(
#[macro_export] #[macro_export]
macro_rules! executable_name( macro_rules! executable_name(
() => ({ () => ({
&std::path::Path::new(executable_os!()).file_stem().unwrap().to_string_lossy() &std::path::Path::new($crate::executable_os!()).file_stem().unwrap().to_string_lossy()
}) })
); );
@ -55,7 +55,7 @@ macro_rules! show(
($err:expr) => ({ ($err:expr) => ({
let e = $err; let e = $err;
uucore::error::set_exit_code(e.code()); uucore::error::set_exit_code(e.code());
eprintln!("{}: {}", executable_name!(), e); eprintln!("{}: {}", $crate::executable_name!(), e);
}) })
); );
@ -72,7 +72,7 @@ macro_rules! show_if_err(
#[macro_export] #[macro_export]
macro_rules! show_error( macro_rules! show_error(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: ", executable_name!()); eprint!("{}: ", $crate::executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -81,7 +81,7 @@ macro_rules! show_error(
#[macro_export] #[macro_export]
macro_rules! show_error_custom_description ( macro_rules! show_error_custom_description (
($err:expr,$($args:tt)+) => ({ ($err:expr,$($args:tt)+) => ({
eprint!("{}: {}: ", executable_name!(), $err); eprint!("{}: {}: ", $crate::executable_name!(), $err);
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -89,7 +89,7 @@ macro_rules! show_error_custom_description (
#[macro_export] #[macro_export]
macro_rules! show_warning( macro_rules! show_warning(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: warning: ", executable_name!()); eprint!("{}: warning: ", $crate::executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
}) })
); );
@ -98,9 +98,9 @@ macro_rules! show_warning(
#[macro_export] #[macro_export]
macro_rules! show_usage_error( macro_rules! show_usage_error(
($($args:tt)+) => ({ ($($args:tt)+) => ({
eprint!("{}: ", executable_name!()); eprint!("{}: ", $crate::executable_name!());
eprintln!($($args)+); eprintln!($($args)+);
eprintln!("Try `{:?} --help` for more information.", executable!()); eprintln!("Try `{:?} --help` for more information.", $crate::executable!());
}) })
); );
@ -118,8 +118,8 @@ macro_rules! exit(
#[macro_export] #[macro_export]
macro_rules! crash( macro_rules! crash(
($exit_code:expr, $($args:tt)+) => ({ ($exit_code:expr, $($args:tt)+) => ({
show_error!($($args)+); $crate::show_error!($($args)+);
exit!($exit_code) $crate::exit!($exit_code)
}) })
); );
@ -130,7 +130,7 @@ macro_rules! crash_if_err(
($exit_code:expr, $exp:expr) => ( ($exit_code:expr, $exp:expr) => (
match $exp { match $exp {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!($exit_code, "{}", f), Err(f) => $crate::crash!($exit_code, "{}", f),
} }
) )
); );
@ -146,7 +146,7 @@ macro_rules! return_if_err(
match $exp { match $exp {
Ok(m) => m, Ok(m) => m,
Err(f) => { Err(f) => {
show_error!("{}", f); $crate::show_error!("{}", f);
return $exit_code; return $exit_code;
} }
} }
@ -182,7 +182,7 @@ macro_rules! safe_unwrap(
($exp:expr) => ( ($exp:expr) => (
match $exp { match $exp {
Ok(m) => m, Ok(m) => m,
Err(f) => crash!(1, "{}", f.to_string()) Err(f) => $crate::crash!(1, "{}", f.to_string())
} }
) )
); );
@ -197,7 +197,7 @@ macro_rules! snippet_list_join_oxford_comma {
format!("{}, {} {}", $valOne, $conjunction, $valTwo) format!("{}, {} {}", $valOne, $conjunction, $valTwo)
); );
($conjunction:expr, $valOne:expr, $valTwo:expr $(, $remaining_values:expr)*) => ( ($conjunction:expr, $valOne:expr, $valTwo:expr $(, $remaining_values:expr)*) => (
format!("{}, {}", $valOne, snippet_list_join_oxford_comma!($conjunction, $valTwo $(, $remaining_values)*)) format!("{}, {}", $valOne, $crate::snippet_list_join_oxford_comma!($conjunction, $valTwo $(, $remaining_values)*))
); );
} }
@ -207,7 +207,7 @@ macro_rules! snippet_list_join {
format!("{} {} {}", $valOne, $conjunction, $valTwo) format!("{} {} {}", $valOne, $conjunction, $valTwo)
); );
($conjunction:expr, $valOne:expr, $valTwo:expr $(, $remaining_values:expr)*) => ( ($conjunction:expr, $valOne:expr, $valTwo:expr $(, $remaining_values:expr)*) => (
format!("{}, {}", $valOne, snippet_list_join_oxford_comma!($conjunction, $valTwo $(, $remaining_values)*)) format!("{}, {}", $valOne, $crate::snippet_list_join_oxford_comma!($conjunction, $valTwo $(, $remaining_values)*))
); );
} }
@ -225,10 +225,10 @@ macro_rules! msg_invalid_input {
#[macro_export] #[macro_export]
macro_rules! msg_invalid_opt_use { macro_rules! msg_invalid_opt_use {
($about:expr, $flag:expr) => { ($about:expr, $flag:expr) => {
msg_invalid_input!(format!("The '{}' option {}", $flag, $about)) $crate::msg_invalid_input!(format!("The '{}' option {}", $flag, $about))
}; };
($about:expr, $long_flag:expr, $short_flag:expr) => { ($about:expr, $long_flag:expr, $short_flag:expr) => {
msg_invalid_input!(format!( $crate::msg_invalid_input!(format!(
"The '{}' ('{}') option {}", "The '{}' ('{}') option {}",
$long_flag, $short_flag, $about $long_flag, $short_flag, $about
)) ))
@ -238,10 +238,10 @@ macro_rules! msg_invalid_opt_use {
#[macro_export] #[macro_export]
macro_rules! msg_opt_only_usable_if { macro_rules! msg_opt_only_usable_if {
($clause:expr, $flag:expr) => { ($clause:expr, $flag:expr) => {
msg_invalid_opt_use!(format!("only usable if {}", $clause), $flag) $crate::msg_invalid_opt_use!(format!("only usable if {}", $clause), $flag)
}; };
($clause:expr, $long_flag:expr, $short_flag:expr) => { ($clause:expr, $long_flag:expr, $short_flag:expr) => {
msg_invalid_opt_use!( $crate::msg_invalid_opt_use!(
format!("only usable if {}", $clause), format!("only usable if {}", $clause),
$long_flag, $long_flag,
$short_flag $short_flag
@ -252,13 +252,13 @@ macro_rules! msg_opt_only_usable_if {
#[macro_export] #[macro_export]
macro_rules! msg_opt_invalid_should_be { macro_rules! msg_opt_invalid_should_be {
($expects:expr, $received:expr, $flag:expr) => { ($expects:expr, $received:expr, $flag:expr) => {
msg_invalid_opt_use!( $crate::msg_invalid_opt_use!(
format!("expects {}, but was provided {}", $expects, $received), format!("expects {}, but was provided {}", $expects, $received),
$flag $flag
) )
}; };
($expects:expr, $received:expr, $long_flag:expr, $short_flag:expr) => { ($expects:expr, $received:expr, $long_flag:expr, $short_flag:expr) => {
msg_invalid_opt_use!( $crate::msg_invalid_opt_use!(
format!("expects {}, but was provided {}", $expects, $received), format!("expects {}, but was provided {}", $expects, $received),
$long_flag, $long_flag,
$short_flag $short_flag
@ -271,13 +271,13 @@ macro_rules! msg_opt_invalid_should_be {
#[macro_export] #[macro_export]
macro_rules! msg_expects_one_of { macro_rules! msg_expects_one_of {
($valOne:expr $(, $remaining_values:expr)*) => ( ($valOne:expr $(, $remaining_values:expr)*) => (
msg_invalid_input!(format!("expects one of {}", snippet_list_join!("or", $valOne $(, $remaining_values)*))) $crate::msg_invalid_input!(format!("expects one of {}", $crate::snippet_list_join!("or", $valOne $(, $remaining_values)*)))
); );
} }
#[macro_export] #[macro_export]
macro_rules! msg_expects_no_more_than_one_of { macro_rules! msg_expects_no_more_than_one_of {
($valOne:expr $(, $remaining_values:expr)*) => ( ($valOne:expr $(, $remaining_values:expr)*) => (
msg_invalid_input!(format!("expects no more than one of {}", snippet_list_join!("or", $valOne $(, $remaining_values)*))) ; $crate::msg_invalid_input!(format!("expects no more than one of {}", $crate::snippet_list_join!("or", $valOne $(, $remaining_values)*))) ;
); );
} }