Skip to content

Commit

Permalink
support nameless api resources
Browse files Browse the repository at this point in the history
  • Loading branch information
AnActualEmerald committed Apr 2, 2024
1 parent 8a35db5 commit 876859c
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 1 deletion.
62 changes: 62 additions & 0 deletions src/endpoint.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,68 @@ macro_rules! endpoint {
}
};

($type:ty; for $name:literal; unnamed) => {
use crate::model::resource::{ApiResourceList, ApiResource};
use crate::client::{RustemonClient, Id};
use crate::error::Error;

/// Returns the default page regarding the resource.
///
/// # Arguments
///
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_page(rustemon_client: &RustemonClient) -> Result<ApiResourceList<$type>, Error> {
rustemon_client.get_by_endpoint::<ApiResourceList<$type>>($name).await
}

/// Returns the page targeted by the parameters.
///
/// # Arguments
///
/// `offset` - The offset to start retrieving the data from.
/// `limit` - Maximum number of elements returned by the call.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_page_with_param(
offset: i64,
limit: i64,
rustemon_client: &RustemonClient
) -> Result<ApiResourceList<$type>, Error> {
rustemon_client.get_with_limit_and_offset::<ApiResourceList<$type>>($name, limit, offset).await
}

/// Returns all entries from the given resource.
///
/// # Arguments
///
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_all_entries(rustemon_client: &RustemonClient) -> Result<Vec<ApiResource<$type>>, Error> {
let mut first_page = get_page(rustemon_client).await?;
let first_page_entries_count = first_page.results.len() as i64;

let total_entries_count = first_page.count;
let other_entries_count = total_entries_count - first_page_entries_count;

let mut all_entries = Vec::with_capacity(total_entries_count as usize);
all_entries.append(&mut first_page.results);

let mut other_entries = get_page_with_param(first_page_entries_count, other_entries_count, rustemon_client).await?;
all_entries.append(&mut other_entries.results);

Ok(all_entries)
}

/// Returns the resource, using its id.
///
/// # Arguments
///
/// `id` - The unique ID of the resource to get.
/// `rustemon_client` - The [RustemonClient] to use to access the resource.
pub async fn get_by_id(id: i64, rustemon_client: &RustemonClient) -> Result<$type, Error> {
rustemon_client.get_by_endpoint_and_id::<$type>($name, Id::Int(id)).await
}

};

($type:ty; for $name:literal; with $(($sub:ident, $sub_type:ty))+) => {

crate::endpoint!($type; for $name);
Expand Down
2 changes: 1 addition & 1 deletion src/evolution.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
/// Evolution chains are essentially family trees. They start with the lowest stage within a family
/// and detail evolution conditions for each as well as Pokémon they can evolve into up through the hierarchy.
pub mod evolution_chain {
crate::endpoint!(crate::model::evolution::EvolutionChain; for "evolution-chain");
crate::endpoint!(crate::model::evolution::EvolutionChain; for "evolution-chain"; unnamed);
}

/// Evolution triggers are the events and conditions that cause a Pokémon to evolve.
Expand Down
13 changes: 13 additions & 0 deletions src/model/resource.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,19 @@ pub struct NamedApiResourceList<T> {
pub results: Vec<NamedApiResource<T>>,
}

/// [ApiResourceList official documentation](https:///pokeapi.co/docs/v2#apiresourcelist)
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize)]
pub struct ApiResourceList<T> {
/// The total number of resources available from this API.
pub count: i64,
/// The URL for the next page in the list.
pub next: Option<String>,
/// The URL for the previous page in the list.
pub previous: Option<String>,
/// A list of named API resources.
pub results: Vec<ApiResource<T>>,
}

/// [ApiResource official documentation](https://pokeapi.co/docs/v2#apiresource)
#[derive(Default, Debug, Clone, PartialEq, Eq, serde::Deserialize)]
pub struct ApiResource<T> {
Expand Down

0 comments on commit 876859c

Please sign in to comment.