Repository: meilisearch/meilisearch
File: meilisearch-auth/src/store.rs (Rust)

Code before:
                        && *action != Action::DocumentsDelete
                        && *action != Action::DocumentsGet
                })
                .chain(vec![
                    Action::DocumentsAdd,
                    Action::DocumentsDelete,
                    Action::DocumentsGet,
                ])
                .collect()
        } else if key.actions.contains(&Action::IndexesAll) {
            key.actions
                .iter()
                .cloned()
                .filter(|action| {
                    *action != Action::IndexesAdd
                        && *action != Action::IndexesGet
                        && *action != Action::IndexesDelete
                        && *action != Action::IndexesUpdate
                })
                .chain(vec![
                    Action::IndexesAdd,
                    Action::IndexesGet,
                    Action::IndexesDelete,
                    Action::IndexesUpdate,
                ])
                .collect()
        } else if key.actions.contains(&Action::SettingsAll) {
            key.actions
                .iter()
                .cloned()
                .filter(|action| {
                    *action != Action::SettingsGet && *action != Action::SettingsUpdate
                })
                .chain(vec![Action::SettingsGet, Action::SettingsUpdate])
                .collect()
        } else if key.actions.contains(&Action::TasksAll) {
            key.actions
                .iter()
                .cloned()
                .filter(|action| *action != Action::TasksGet)
                .chain(vec![Action::TasksGet])
                .collect()
        } else if key.actions.contains(&Action::DumpsAll) {
            key.actions
                .iter()
                .cloned()
                .filter(|action| *action != Action::DumpsCreate)
                .chain(vec![Action::DumpsCreate])
                .collect()
        } else if key.actions.contains(&Action::StatsAll) {
            key.actions

Review comment (refactor):
It was okay-ish to do a contain when there was only one, but now we’re iterating over and over on all the actions, and it’s taking a lot of lines of code.

I thought it would be easier to iterate on all the actions, transform the `.*` into what we want, and let the other as-is.

And finally, to remove the duplicate entry, I would prefer a simpler collect to a HashSet + a conversion to a vec at the very end of the computation.

Something like that;
```rust
let actions = key.actions.iter().flat_map(|action| match action {
    IndexesAll => [IndexesAdd, IndexesGet, IndexesDelete, IndexesUpdate].iter(),
    SettingsAll => todo!(),
    *All => todo!(),
...
    other => std::iter::once(other),
  })
  .collect::<HashSet<Action>>();

let action = actions.into_iter().collect::<Vec<Action>>();
```

(this code probably has issues with the iterator in the match, but you get the idea)

Code after:
                        ]
                        .iter(),
                    );
                }
                Action::IndexesAll => {
                    actions.extend(
                        [
                            Action::IndexesAdd,
                            Action::IndexesDelete,
                            Action::IndexesGet,
                            Action::IndexesUpdate,
                        ]
                        .iter(),
                    );
                }
                Action::SettingsAll => {
                    actions.extend([Action::SettingsGet, Action::SettingsUpdate].iter());
                }
                Action::DumpsAll => {
                    actions.insert(Action::DumpsCreate);
                }
                Action::TasksAll => {
                    actions.insert(Action::TasksGet);
                }
                Action::StatsAll => {
                    actions.insert(Action::StatsGet);
                }
                other => {
                    actions.insert(*other);
                }
            }
        }

        let no_index_restriction = key.indexes.contains(&StarOr::Star);
        for action in actions {
            if no_index_restriction {
                // If there is no index restriction we put None.
                db.put(&mut wtxn, &(&uid, &action, None), &key.expires_at)?;
            } else {
                // else we create a key for each index.
                for index in key.indexes.iter() {
                    db.put(
                        &mut wtxn,
                        &(&uid, &action, Some(index.deref().as_bytes())),
                        &key.expires_at,
                    )?;
                }
            }
        }

        wtxn.commit()?;