Clean up trash support on Android (#10225)

# Description

Currently on Android, there are warnings about unused variables. This PR
fixes that with more conditional guards for the unused variables.

Additionally, in #10013, @kubouch gave feedback in [the last
PR](https://github.com/nushell/nushell/pull/10013#pullrequestreview-1596828128)
that it was unwieldy to repeat

```rust
#[cfg(all(
    feature = "trash-support",
    not(target_os = "android"),
    not(target_os = "ios")
))]
```
This commit is contained in:
Skyler Hawthorne 2023-09-05 08:38:23 -04:00 committed by GitHub
parent e566a073dc
commit 5f1e8a6af8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

View File

@ -1,9 +1,5 @@
use std::collections::HashMap; use std::collections::HashMap;
#[cfg(all( use std::io::Error;
feature = "trash-support",
not(target_os = "android"),
not(target_os = "ios")
))]
use std::io::ErrorKind; use std::io::ErrorKind;
#[cfg(unix)] #[cfg(unix)]
use std::os::unix::prelude::FileTypeExt; use std::os::unix::prelude::FileTypeExt;
@ -27,6 +23,11 @@ const GLOB_PARAMS: nu_glob::MatchOptions = nu_glob::MatchOptions {
recursive_match_hidden_dir: true, recursive_match_hidden_dir: true,
}; };
const TRASH_SUPPORTED: bool = cfg!(all(
feature = "trash-support",
not(any(target_os = "android", target_os = "ios"))
));
#[derive(Clone)] #[derive(Clone)]
pub struct Rm; pub struct Rm;
@ -95,24 +96,21 @@ impl Command for Rm {
example: "rm file.txt", example: "rm file.txt",
result: None, result: None,
}]; }];
#[cfg(all( if TRASH_SUPPORTED {
feature = "trash-support", examples.append(&mut vec![
not(target_os = "android"), Example {
not(target_os = "ios") description: "Move a file to the trash",
))] example: "rm --trash file.txt",
examples.append(&mut vec![ result: None,
Example { },
description: "Move a file to the trash", Example {
example: "rm --trash file.txt", description:
result: None, "Delete a file permanently, even if the 'always_trash' config option is true",
}, example: "rm --permanent file.txt",
Example { result: None,
description: },
"Delete a file permanently, even if the 'always_trash' config option is true", ]);
example: "rm --permanent file.txt", }
result: None,
},
]);
examples.push(Example { examples.push(Example {
description: "Delete a file, ignoring 'file not found' errors", description: "Delete a file, ignoring 'file not found' errors",
example: "rm --force file.txt", example: "rm --force file.txt",
@ -133,11 +131,6 @@ fn rm(
call: &Call, call: &Call,
) -> Result<PipelineData, ShellError> { ) -> Result<PipelineData, ShellError> {
let trash = call.has_flag("trash"); let trash = call.has_flag("trash");
#[cfg(all(
feature = "trash-support",
not(target_os = "android"),
not(target_os = "ios")
))]
let permanent = call.has_flag("permanent"); let permanent = call.has_flag("permanent");
let recursive = call.has_flag("recursive"); let recursive = call.has_flag("recursive");
let force = call.has_flag("force"); let force = call.has_flag("force");
@ -182,13 +175,9 @@ fn rm(
} }
let span = call.head; let span = call.head;
let rm_always_trash = engine_state.get_config().rm_always_trash;
let config = engine_state.get_config(); if !TRASH_SUPPORTED {
let rm_always_trash = config.rm_always_trash;
#[cfg(not(feature = "trash-support"))]
{
if rm_always_trash { if rm_always_trash {
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
"Cannot execute `rm`; the current configuration specifies \ "Cannot execute `rm`; the current configuration specifies \
@ -202,9 +191,9 @@ fn rm(
)); ));
} else if trash { } else if trash {
return Err(ShellError::GenericError( return Err(ShellError::GenericError(
"Cannot execute `rm` with option `--trash`; feature `trash-support` not enabled" "Cannot execute `rm` with option `--trash`; feature `trash-support` not enabled or on an unsupported platform"
.into(), .into(),
"this option is only available if nu is built with the `trash-support` feature" "this option is only available if nu is built with the `trash-support` feature and the platform supports trash"
.into(), .into(),
Some(span), Some(span),
None, None,
@ -376,55 +365,41 @@ fn rm(
format!("rm: remove '{}'? ", f.to_string_lossy()), format!("rm: remove '{}'? ", f.to_string_lossy()),
); );
let result; let result = if let Err(e) = interaction {
#[cfg(all( let e = Error::new(ErrorKind::Other, &*e.to_string());
feature = "trash-support", Err(e)
not(target_os = "android"), } else if interactive && !confirmed {
not(target_os = "ios") Ok(())
))] } else if TRASH_SUPPORTED && (trash || (rm_always_trash && !permanent)) {
{ #[cfg(all(
use std::io::Error; feature = "trash-support",
result = if let Err(e) = interaction { not(any(target_os = "android", target_os = "ios"))
let e = Error::new(ErrorKind::Other, &*e.to_string()); ))]
Err(e) {
} else if interactive && !confirmed {
Ok(())
} else if trash || (rm_always_trash && !permanent) {
trash::delete(&f).map_err(|e: trash::Error| { trash::delete(&f).map_err(|e: trash::Error| {
Error::new(ErrorKind::Other, format!("{e:?}\nTry '--trash' flag")) Error::new(ErrorKind::Other, format!("{e:?}\nTry '--trash' flag"))
}) })
} else if metadata.is_file() }
|| is_socket
|| is_fifo // Should not be reachable since we error earlier if
|| metadata.file_type().is_symlink() // these options are given on an unsupported platform
#[cfg(any(
not(feature = "trash-support"),
target_os = "android",
target_os = "ios"
))]
{ {
std::fs::remove_file(&f) unreachable!()
} else { }
std::fs::remove_dir_all(&f) } else if metadata.is_file()
}; || is_socket
} || is_fifo
#[cfg(any( || metadata.file_type().is_symlink()
not(feature = "trash-support"),
target_os = "android",
target_os = "ios"
))]
{ {
use std::io::{Error, ErrorKind}; std::fs::remove_file(&f)
result = if let Err(e) = interaction { } else {
let e = Error::new(ErrorKind::Other, &*e.to_string()); std::fs::remove_dir_all(&f)
Err(e) };
} else if interactive && !confirmed {
Ok(())
} else if metadata.is_file()
|| is_socket
|| is_fifo
|| metadata.file_type().is_symlink()
{
std::fs::remove_file(&f)
} else {
std::fs::remove_dir_all(&f)
};
}
if let Err(e) = result { if let Err(e) = result {
let msg = format!("Could not delete {:}: {e:}", f.to_string_lossy()); let msg = format!("Could not delete {:}: {e:}", f.to_string_lossy());