Skip to content

Commit e2e42ac

Browse files
authored
Merge pull request uutils#5304 from KAAtheWiseGit/main
`rm`: make the utility public
2 parents ba3f266 + 4b76b2f commit e2e42ac

File tree

2 files changed

+48
-11
lines changed

2 files changed

+48
-11
lines changed

.vscode/cspell.dictionaries/acronyms+names.wordlist.txt

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,7 @@ MinGW
5858
Minix
5959
NetBSD
6060
Novell
61+
Nushell
6162
OpenBSD
6263
POSIX
6364
PowerPC

src/uu/rm/src/rm.rs

Lines changed: 47 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -18,22 +18,52 @@ use uucore::{format_usage, help_about, help_section, help_usage, prompt_yes, sho
1818
use walkdir::{DirEntry, WalkDir};
1919

2020
#[derive(Eq, PartialEq, Clone, Copy)]
21-
enum InteractiveMode {
21+
/// Enum, determining when the `rm` will prompt the user about the file deletion
22+
pub enum InteractiveMode {
23+
/// Never prompt
2224
Never,
25+
/// Prompt once before removing more than three files, or when removing
26+
/// recursively.
2327
Once,
28+
/// Prompt before every removal
2429
Always,
30+
/// Prompt only on write-protected files
2531
PromptProtected,
2632
}
2733

28-
struct Options {
29-
force: bool,
30-
interactive: InteractiveMode,
34+
/// Options for the `rm` command
35+
///
36+
/// All options are public so that the options can be programmatically
37+
/// constructed by other crates, such as Nushell. That means that this struct
38+
/// is part of our public API. It should therefore not be changed without good
39+
/// reason.
40+
///
41+
/// The fields are documented with the arguments that determine their value.
42+
pub struct Options {
43+
/// `-f`, `--force`
44+
pub force: bool,
45+
/// Iterative mode, determines when the command will prompt.
46+
///
47+
/// Set by the following arguments:
48+
/// - `-i`: [`InteractiveMode::Always`]
49+
/// - `-I`: [`InteractiveMode::Once`]
50+
/// - `--interactive`: sets one of the above or [`InteractiveMode::Never`]
51+
/// - `-f`: implicitly sets [`InteractiveMode::Never`]
52+
///
53+
/// If no other option sets this mode, [`InteractiveMode::PromptProtected`]
54+
/// is used
55+
pub interactive: InteractiveMode,
3156
#[allow(dead_code)]
32-
one_fs: bool,
33-
preserve_root: bool,
34-
recursive: bool,
35-
dir: bool,
36-
verbose: bool,
57+
/// `--one-file-system`
58+
pub one_fs: bool,
59+
/// `--preserve-root`/`--no-preserve-root`
60+
pub preserve_root: bool,
61+
/// `-r`, `--recursive`
62+
pub recursive: bool,
63+
/// `-d`, `--dir`
64+
pub dir: bool,
65+
/// `-v`, `--verbose`
66+
pub verbose: bool,
3767
}
3868

3969
const ABOUT: &str = help_about!("rm.md");
@@ -249,7 +279,13 @@ pub fn uu_app() -> Command {
249279
}
250280

251281
// TODO: implement one-file-system (this may get partially implemented in walkdir)
252-
fn remove(files: &[&OsStr], options: &Options) -> bool {
282+
/// Remove (or unlink) the given files
283+
///
284+
/// Returns true if it has encountered an error.
285+
///
286+
/// Behavior is determined by the `options` parameter, see [`Options`] for
287+
/// details.
288+
pub fn remove(files: &[&OsStr], options: &Options) -> bool {
253289
let mut had_err = false;
254290

255291
for filename in files {
@@ -268,7 +304,7 @@ fn remove(files: &[&OsStr], options: &Options) -> bool {
268304
// TODO: actually print out the specific error
269305
// TODO: When the error is not about missing files
270306
// (e.g., permission), even rm -f should fail with
271-
// outputting the error, but there's no easy eay.
307+
// outputting the error, but there's no easy way.
272308
if options.force {
273309
false
274310
} else {

0 commit comments

Comments
 (0)