Function get_champions_by_role

Source
async fn get_champions_by_role(
    role: &str,
    collection: &Collection<ChampionData>,
) -> Result<Vec<ChampionData>>
Expand description

⚙️ Retrieves all champions that match the specified role from the database.

This asynchronous function queries a MongoDB collection to find all champions that include the specified role. The function returns a vector of ChampionData objects that match the role provided.

§Parameters:

  • role: A reference to a string representing the role to filter champions by (e.g., “Top”, “Jungler”).
  • collection: A reference to a MongoDB collection of ChampionData representing the champion data stored in the database.

§Returns:

  • Result<Vec<ChampionData>, mongodb::error::Error>: On success, returns a vector of ChampionData objects that match the role. On failure, returns an Error from MongoDB indicating what went wrong.

§Example:

let role = "AD Carry";
let champions = get_champions_by_role(role, &collection).await?;
for champion in champions {
    println!("Champion: {}", champion.name);
}

§⚠️ Notes:

  • The function uses MongoDB’s $in operator to search for champions whose roles include the specified value.
  • The result is collected into a vector using the try_collect method for convenience.
  • match_role_with_database_roles: Converts a Role enum into the corresponding string representation, which can be used as input for this function.

§Dependencies:

  • This function depends on a MongoDB collection that stores ChampionData documents.
  • Requires the futures crate for the try_collect method to handle the cursor results asynchronously.