Repository: awslabs/llrt
File: src/fs/read_dir.rs (Rust)

Code before:

impl<'js> IntoJs<'js> for ReadDir {
    fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
        let arr = Array::new(ctx.clone())?;
        for (index, (name, metadata)) in self.items.into_iter().enumerate() {
            if let Some(metadata) = metadata {
                let dirent = Dirent { metadata };

                let dirent = Class::instance(ctx.clone(), dirent)?;
                dirent.set(PredefinedAtom::Name, name)?;
                arr.set(index, dirent)?;
            } else {
                arr.set(index, name)?;
            }
        }
        arr.into_js(ctx)
    }
}

pub async fn read_dir<'js>(
    ctx: Ctx<'js>,
    path: String,
    options: Opt<Object<'js>>,
) -> Result<ReadDir> {
    let mut path = path;
    let with_file_types = options
        .0
        .as_ref()
        .and_then(|opts| opts.get("withFileTypes").ok())
        .and_then(|file_types: Value| file_types.as_bool())
        .unwrap_or_default();

    let with_recursive = options
        .0
        .as_ref()
        .and_then(|opts| opts.get("recursive").ok())
        .and_then(|recursive: Value| recursive.as_bool())
        .unwrap_or_default();

    let skip_root_pos = {
        match path.as_str() {
            // . | ./
            "." | CURRENT_DIR_STR => CURRENT_DIR_STR.len(),
            // ./path
            _ if path.starts_with(CURRENT_DIR_STR) => path.len() + 1,
            // path
            _ => {
                if !is_absolute(path.clone()) {
                    path = [CURRENT_DIR_STR.to_string(), path].concat();
                }
                path.len() + 1

Review comment (suggestion):
When we now have two options. We can first check if we have options and then access the variables:
```rust
if let Some(opts) = opts.0{
 //check for withFileTypes & recursive option
}
```

Code after:
}

impl<'js> IntoJs<'js> for ReadDir {
    fn into_js(self, ctx: &Ctx<'js>) -> Result<Value<'js>> {
        let arr = Array::new(ctx.clone())?;
        for (index, (name, metadata)) in self.items.into_iter().enumerate() {
            if let Some(metadata) = metadata {
                let dirent = Dirent { metadata };

                let dirent = Class::instance(ctx.clone(), dirent)?;
                dirent.set(PredefinedAtom::Name, name)?;
                arr.set(index, dirent)?;
            } else {
                arr.set(index, name)?;
            }
        }
        arr.into_js(ctx)
    }
}

pub async fn read_dir<'js>(
    ctx: Ctx<'js>,
    path: String,
    options: Opt<Object<'js>>,
) -> Result<ReadDir> {
    let mut path = path;

    let mut with_file_types = false;
    let mut with_recursive = false;

    if let Some(options) = options.0 {
        with_file_types = options
            .get("withFileTypes")
            .ok()
            .and_then(|file_types: Value| file_types.as_bool())
            .unwrap_or_default();

        with_recursive = options
            .get("recursive")
            .ok()
            .and_then(|recursive: Value| recursive.as_bool())
            .unwrap_or_default();
    };

    let skip_root_pos = {
        match path.as_str() {
            // . | ./
            "." | CURRENT_DIR_STR => CURRENT_DIR_STR.len(),
            // ./path
            _ if path.starts_with(CURRENT_DIR_STR) => path.len() + 1,
            // path