mirror of
https://github.com/uutils/coreutils
synced 2024-12-13 06:42:42 +00:00
remove unnecessary and unused macros
This commit is contained in:
parent
cb051e7416
commit
c075f105a4
8 changed files with 8 additions and 211 deletions
|
@ -292,13 +292,13 @@ fn du(
|
|||
let read = match fs::read_dir(&my_stat.path) {
|
||||
Ok(read) => read,
|
||||
Err(e) => {
|
||||
safe_writeln!(
|
||||
writeln!(
|
||||
stderr(),
|
||||
"{}: cannot read directory {}: {}",
|
||||
options.util_name,
|
||||
my_stat.path.quote(),
|
||||
e
|
||||
);
|
||||
).unwrap();
|
||||
return Box::new(iter::once(my_stat));
|
||||
}
|
||||
};
|
||||
|
|
|
@ -413,7 +413,7 @@ impl<'a> State<'a> {
|
|||
|
||||
// This is fatal if the check is enabled.
|
||||
if input.check_order == CheckOrder::Enabled {
|
||||
exit!(1);
|
||||
std::process::exit(1);
|
||||
}
|
||||
|
||||
self.has_failed = true;
|
||||
|
|
|
@ -410,7 +410,7 @@ fn get_size(size_str_opt: Option<String>) -> Option<u64> {
|
|||
util_name(),
|
||||
size_str_opt.unwrap().maybe_quote()
|
||||
);
|
||||
exit!(1);
|
||||
std::process::exit(1);
|
||||
}
|
||||
};
|
||||
|
||||
|
|
|
@ -91,7 +91,7 @@ pub fn uumain(args: impl uucore::Args) -> i32 {
|
|||
if cfg!(windows) {
|
||||
// see https://github.com/rust-lang/rust/issues/29494
|
||||
show_error!("{} is currently not supported in this platform", OPT_FILTER);
|
||||
exit!(-1);
|
||||
std::process::exit(-1);
|
||||
} else {
|
||||
settings.filter = Some(matches.value_of(OPT_FILTER).unwrap().to_owned());
|
||||
}
|
||||
|
|
|
@ -18,7 +18,6 @@ mod parser; // string parsing modules
|
|||
|
||||
// * cross-platform modules
|
||||
pub use crate::mods::backup_control;
|
||||
pub use crate::mods::coreopts;
|
||||
pub use crate::mods::display;
|
||||
pub use crate::mods::error;
|
||||
pub use crate::mods::os;
|
||||
|
|
|
@ -26,9 +26,7 @@
|
|||
//! - From custom messages: [`show_error!`], [`show_usage_error!`]
|
||||
//! - Print warnings: [`show_warning!`]
|
||||
//! - Terminate util execution
|
||||
//! - Terminate regularly: [`exit!`], [`return_if_err!`]
|
||||
//! - Crash program: [`crash!`], [`crash_if_err!`], [`safe_unwrap!`]
|
||||
//! - Unwrapping result types: [`safe_unwrap!`]
|
||||
//! - Crash program: [`crash!`], [`crash_if_err!`]
|
||||
|
||||
// spell-checker:ignore sourcepath targetpath
|
||||
|
||||
|
@ -223,22 +221,10 @@ macro_rules! show_usage_error(
|
|||
})
|
||||
);
|
||||
|
||||
//====
|
||||
|
||||
/// Calls [`std::process::exit`] with the provided exit code.
|
||||
///
|
||||
/// Why not call exit directly?
|
||||
#[macro_export]
|
||||
macro_rules! exit(
|
||||
($exit_code:expr) => ({
|
||||
::std::process::exit($exit_code)
|
||||
})
|
||||
);
|
||||
|
||||
/// Display an error and [`exit!`]
|
||||
///
|
||||
/// Displays the provided error message using [`show_error!`], then invokes
|
||||
/// [`exit!`] with the provided exit code.
|
||||
/// [`std::process::exit`] with the provided exit code.
|
||||
///
|
||||
/// # Examples
|
||||
///
|
||||
|
@ -255,7 +241,7 @@ macro_rules! exit(
|
|||
macro_rules! crash(
|
||||
($exit_code:expr, $($args:tt)+) => ({
|
||||
$crate::show_error!($($args)+);
|
||||
$crate::exit!($exit_code)
|
||||
std::process::exit($exit_code);
|
||||
})
|
||||
);
|
||||
|
||||
|
@ -289,52 +275,6 @@ macro_rules! crash_if_err(
|
|||
)
|
||||
);
|
||||
|
||||
/// Unwrap some Result, crashing instead of panicking.
|
||||
///
|
||||
/// Drop this in favor of `crash_if_err!`
|
||||
#[macro_export]
|
||||
macro_rules! safe_unwrap(
|
||||
($exp:expr) => (
|
||||
match $exp {
|
||||
Ok(m) => m,
|
||||
Err(f) => $crate::crash!(1, "{}", f.to_string())
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
//====
|
||||
|
||||
/// Unwraps the Result. Instead of panicking, it shows the error and then
|
||||
/// returns from the function with the provided exit code.
|
||||
/// Assumes the current function returns an i32 value.
|
||||
///
|
||||
/// Replace with `crash_if_err`?
|
||||
#[macro_export]
|
||||
macro_rules! return_if_err(
|
||||
($exit_code:expr, $exp:expr) => (
|
||||
match $exp {
|
||||
Ok(m) => m,
|
||||
Err(f) => {
|
||||
$crate::show_error!("{}", f);
|
||||
return $exit_code;
|
||||
}
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
//====
|
||||
|
||||
/// This is used exclusively by du...
|
||||
#[macro_export]
|
||||
macro_rules! safe_writeln(
|
||||
($fd:expr, $($args:tt)+) => (
|
||||
match writeln!($fd, $($args)+) {
|
||||
Ok(_) => {}
|
||||
Err(f) => panic!("{}", f)
|
||||
}
|
||||
)
|
||||
);
|
||||
|
||||
//-- message templates
|
||||
|
||||
//-- message templates : (join utility sub-macros)
|
||||
|
|
|
@ -1,7 +1,6 @@
|
|||
// mods ~ cross-platforms modules (core/bundler file)
|
||||
|
||||
pub mod backup_control;
|
||||
pub mod coreopts;
|
||||
pub mod display;
|
||||
pub mod error;
|
||||
pub mod os;
|
||||
|
|
|
@ -1,141 +0,0 @@
|
|||
pub struct HelpText<'a> {
|
||||
pub name: &'a str,
|
||||
pub version: &'a str,
|
||||
pub syntax: &'a str,
|
||||
pub summary: &'a str,
|
||||
pub long_help: &'a str,
|
||||
pub display_usage: bool,
|
||||
}
|
||||
|
||||
pub struct CoreOptions<'a> {
|
||||
options: getopts::Options,
|
||||
help_text: HelpText<'a>,
|
||||
}
|
||||
|
||||
impl<'a> CoreOptions<'a> {
|
||||
pub fn new(help_text: HelpText<'a>) -> Self {
|
||||
let mut ret = CoreOptions {
|
||||
options: getopts::Options::new(),
|
||||
help_text,
|
||||
};
|
||||
ret.options
|
||||
.optflag("", "help", "print usage information")
|
||||
.optflag("", "version", "print name and version number");
|
||||
ret
|
||||
}
|
||||
pub fn optflagopt(
|
||||
&mut self,
|
||||
short_name: &str,
|
||||
long_name: &str,
|
||||
desc: &str,
|
||||
hint: &str,
|
||||
) -> &mut CoreOptions<'a> {
|
||||
self.options.optflagopt(short_name, long_name, desc, hint);
|
||||
self
|
||||
}
|
||||
pub fn optflag(
|
||||
&mut self,
|
||||
short_name: &str,
|
||||
long_name: &str,
|
||||
desc: &str,
|
||||
) -> &mut CoreOptions<'a> {
|
||||
self.options.optflag(short_name, long_name, desc);
|
||||
self
|
||||
}
|
||||
pub fn optflagmulti(
|
||||
&mut self,
|
||||
short_name: &str,
|
||||
long_name: &str,
|
||||
desc: &str,
|
||||
) -> &mut CoreOptions<'a> {
|
||||
self.options.optflagmulti(short_name, long_name, desc);
|
||||
self
|
||||
}
|
||||
pub fn optopt(
|
||||
&mut self,
|
||||
short_name: &str,
|
||||
long_name: &str,
|
||||
desc: &str,
|
||||
hint: &str,
|
||||
) -> &mut CoreOptions<'a> {
|
||||
self.options.optopt(short_name, long_name, desc, hint);
|
||||
self
|
||||
}
|
||||
pub fn optmulti(
|
||||
&mut self,
|
||||
short_name: &str,
|
||||
long_name: &str,
|
||||
desc: &str,
|
||||
hint: &str,
|
||||
) -> &mut CoreOptions<'a> {
|
||||
self.options.optmulti(short_name, long_name, desc, hint);
|
||||
self
|
||||
}
|
||||
pub fn usage(&self, summary: &str) -> String {
|
||||
self.options.usage(summary)
|
||||
}
|
||||
pub fn parse(&mut self, args: Vec<String>) -> getopts::Matches {
|
||||
let matches = match self.options.parse(&args[1..]) {
|
||||
Ok(m) => Some(m),
|
||||
Err(f) => {
|
||||
eprint!("{}: error: ", self.help_text.name);
|
||||
eprintln!("{}", f);
|
||||
::std::process::exit(1);
|
||||
}
|
||||
}
|
||||
.unwrap();
|
||||
if matches.opt_present("help") {
|
||||
let usage_str = if self.help_text.display_usage {
|
||||
format!(
|
||||
"\n {}\n\n Reference\n",
|
||||
self.options.usage(self.help_text.summary)
|
||||
)
|
||||
.replace("Options:", " Options:")
|
||||
} else {
|
||||
String::new()
|
||||
};
|
||||
print!(
|
||||
"
|
||||
{0} {1}
|
||||
|
||||
{0} {2}
|
||||
{3}{4}
|
||||
",
|
||||
self.help_text.name,
|
||||
self.help_text.version,
|
||||
self.help_text.syntax,
|
||||
usage_str,
|
||||
self.help_text.long_help
|
||||
);
|
||||
crate::exit!(0);
|
||||
} else if matches.opt_present("version") {
|
||||
println!("{} {}", self.help_text.name, self.help_text.version);
|
||||
crate::exit!(0);
|
||||
}
|
||||
matches
|
||||
}
|
||||
}
|
||||
|
||||
#[macro_export]
|
||||
macro_rules! app {
|
||||
($syntax: expr, $summary: expr, $long_help: expr) => {
|
||||
uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText {
|
||||
name: uucore::util_name(),
|
||||
version: env!("CARGO_PKG_VERSION"),
|
||||
syntax: $syntax,
|
||||
summary: $summary,
|
||||
long_help: $long_help,
|
||||
display_usage: true,
|
||||
})
|
||||
};
|
||||
($syntax: expr, $summary: expr, $long_help: expr, $display_usage: expr) => {
|
||||
uucore::coreopts::CoreOptions::new(uucore::coreopts::HelpText {
|
||||
name: uucore::util_name(),
|
||||
version: env!("CARGO_PKG_VERSION"),
|
||||
syntax: $syntax,
|
||||
summary: $summary,
|
||||
long_help: $long_help,
|
||||
display_usage: $display_usage,
|
||||
})
|
||||
};
|
||||
}
|
Loading…
Reference in a new issue