Skip to content

Commit

Permalink
Made fs@ls easier to use with functions
Browse files Browse the repository at this point in the history
  • Loading branch information
adam-mcdaniel committed Sep 7, 2024
1 parent 5692178 commit c992a0b
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions src/binary/init/fs_module.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,9 +227,10 @@ pub fn get(env: &mut Environment) -> Expression {
String::from("ls") => Expression::builtin("ls", |args, env| {
super::check_exact_args_len("ls", &args, 1)?;
let cwd = PathBuf::from(env.get_cwd());
let dir = cwd.join(args[0].eval(env)?.to_string());
let path = args[0].eval(env)?.to_string();
let dir = cwd.join(&path);

list_directory(&dir)
list_directory(&dir, &Path::new(&path))

Check warning on line 233 in src/binary/init/fs_module.rs

View workflow job for this annotation

GitHub Actions / clippy

this expression creates a reference which is immediately dereferenced by the compiler

warning: this expression creates a reference which is immediately dereferenced by the compiler --> src/binary/init/fs_module.rs:233:34 | 233 | list_directory(&dir, &Path::new(&path)) | ^^^^^^^^^^^^^^^^^ help: change this to: `Path::new(&path)` | = help: for further information visit https://rust-lang.github.io/rust-clippy/master/index.html#needless_borrow = note: `#[warn(clippy::needless_borrow)]` on by default
}, "get a directory's entries as a list of strings"),
String::from("exists?") => Expression::builtin("exists", |args, env| {
super::check_exact_args_len("exists", &args, 1)?;
Expand Down Expand Up @@ -456,7 +457,7 @@ fn remove_path(path: &Path) -> Result<(), Error> {
}

/// Returns the paths of entries in a directory as a list of strings.
fn list_directory(dir: &Path) -> Result<Expression, Error> {
fn list_directory(dir: &Path, short: &Path) -> Result<Expression, Error> {
if dir.is_dir() {
// The list of paths (as strings) in the directory we will return.
let mut result = vec![];
Expand All @@ -469,7 +470,7 @@ fn list_directory(dir: &Path) -> Result<Expression, Error> {
if let Ok(entry) = entry {
let file_name_osstring = entry.file_name();
result.push(match file_name_osstring.into_string() {
Ok(file_name) => file_name,
Ok(file_name) => short.join(file_name).to_string_lossy().to_string(),
// If we cannot directly convert the filename to a string,
// it's probably an invalid UTF-8 string.
// In this case, we remove the invalid bytes and try again.
Expand Down

0 comments on commit c992a0b

Please sign in to comment.