Skip to content

Commit

Permalink
feat: impl fn characters to Anime and Manga
Browse files Browse the repository at this point in the history
  • Loading branch information
AndrielFR committed Jan 11, 2025
1 parent 1d51a7b commit a59d9d8
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 15 deletions.
13 changes: 13 additions & 0 deletions queries/get_character.graphql
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,19 @@ query ($id: Int) {
}
age
bloodType
media {
edges {
node {
title {
romaji
english
native
}
id
type
}
}
}
isFavourite
isFavouriteBlocked
siteUrl
Expand Down
20 changes: 15 additions & 5 deletions src/models/anime.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,9 +49,6 @@ use crate::{Client, Result};
/// * `trending` - The trending of the anime.
/// * `favourites` - The number of favourites of the anime.
/// * `tags` - The tags of the anime.
/// * `characters` - The characters of the anime.
/// * `staff` - The staff of the anime.
/// * `studios` - The studios of the anime.
/// * `is_favourite` - Whether the anime is favourite or not.
/// * `is_favourite_blocked` - Whether the anime is favourite blocked or not.
/// * `is_adult` - Whether the anime is adult or not.
Expand Down Expand Up @@ -125,8 +122,7 @@ pub struct Anime {
/// The relations of the anime.
pub(crate) relations: Value,
/// The characters of the anime.
#[serde(skip)]
pub characters: Option<Vec<Character>>,
pub(crate) characters: Value,
/// The staff of the anime.
#[serde(skip)]
pub staff: Option<Vec<Person>>,
Expand Down Expand Up @@ -186,6 +182,20 @@ impl Anime {
}
}

/// Returns the characters of the anime.
pub fn characters(&self) -> Vec<Character> {
self.characters
.as_object()
.unwrap()
.get("nodes")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|c| serde_json::from_value(c.clone()).unwrap())
.collect()
}

/// Returns the relations of the anime.
pub fn relations(&self) -> Vec<Relation> {
self.relations
Expand Down
18 changes: 17 additions & 1 deletion src/models/character.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@

//! This module contains the `Character` struct and its related types.
use std::fmt::Display;

use serde::{Deserialize, Serialize};
use serde_json::Value;

use super::{Date, Gender, Image, Name, Person};
use crate::{Client, Result};
Expand All @@ -30,6 +33,9 @@ pub struct Character {
pub age: Option<String>,
/// The blood type of the character.
pub blood_type: Option<String>,
/// The medias that the character participates.
#[serde(rename = "media")]
pub(crate) medias: Option<Value>,
/// Whether the character is a favorite.
pub is_favourite: Option<bool>,
/// Whether the character is blocked from being a favorite.
Expand Down Expand Up @@ -100,7 +106,7 @@ impl Character {
/// # Ok(())
/// # }
/// ```
pub async fn get_medias<T>(&self) -> Result<T> {
pub async fn get_medias<T>(&self) -> Result<Vec<T>> {
unimplemented!()
}
}
Expand All @@ -116,3 +122,13 @@ pub enum CharacterRole {
/// A supporting character.
Supporting,
}

impl Display for CharacterRole {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
match self {
CharacterRole::Background => write!(f, "Background"),
CharacterRole::Main => write!(f, "Main"),
CharacterRole::Supporting => write!(f, "Supporting"),
}
}
}
21 changes: 15 additions & 6 deletions src/models/manga.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,10 +47,6 @@ use crate::{Client, Result};
/// * `trending` - The trending of the manga.
/// * `favourites` - The number of favourites of the manga.
/// * `tags` - The tags of the manga.
/// * `relations` - The relations of the manga.
/// * `characters` - The characters of the manga.
/// * `staff` - The staff of the manga.
/// * `studios` - The studios of the manga.
/// * `is_favourite` - Whether the manga is favourite or not.
/// * `is_favourite_blocked` - Whether the manga is favourite blocked or not.
/// * `is_adult` - Whether the manga is adult or not.
Expand Down Expand Up @@ -116,8 +112,7 @@ pub struct Manga {
/// The relations of the manga.
pub(crate) relations: Value,
/// The characters of the manga.
#[serde(skip)]
pub characters: Option<Vec<Character>>,
pub(crate) characters: Value,
/// The staff of the manga.
#[serde(skip)]
pub staff: Option<Vec<Person>>,
Expand Down Expand Up @@ -173,6 +168,20 @@ impl Manga {
}
}

/// Returns the characters of the manga.
pub fn characters(&self) -> Vec<Character> {
self.characters
.as_object()
.unwrap()
.get("nodes")
.unwrap()
.as_array()
.unwrap()
.iter()
.map(|c| serde_json::from_value(c.clone()).unwrap())
.collect()
}

/// Returns the relations of the manga.
pub fn relations(&self) -> Vec<Relation> {
self.relations
Expand Down
4 changes: 2 additions & 2 deletions src/models/person.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl Person {
/// # Ok(())
/// # }
/// ```
pub async fn get_medias<T>(&self) -> Result<T> {
pub async fn get_medias<T>(&self) -> Result<Vec<T>> {
unimplemented!()
}

Expand Down Expand Up @@ -137,7 +137,7 @@ impl Person {
/// # Ok(())
/// # }
/// ```
pub async fn get_character_medias<T>(&self, _character_id: i64) -> Result<T> {
pub async fn get_character_medias<T>(&self, _character_id: i64) -> Result<Vec<T>> {
unimplemented!()
}
}
2 changes: 1 addition & 1 deletion src/models/studio.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ impl Studio {
/// # Ok(())
/// # }
/// ```
pub async fn get_medias<T>(&self) -> Result<T> {
pub async fn get_medias<T>(&self) -> Result<Vec<T>> {
unimplemented!()
}
}

0 comments on commit a59d9d8

Please sign in to comment.