Skip to content

Commit

Permalink
feat: use serde::Deserialize
Browse files Browse the repository at this point in the history
i'm dumb for not using it.

- now it works.
  • Loading branch information
AndrielFR committed Sep 10, 2024
1 parent b1edfab commit 3d0179a
Show file tree
Hide file tree
Showing 28 changed files with 209 additions and 1,196 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
src/main.rs
Cargo.lock
/target
/.vscode
5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ categories = ["api-binding"]
edition = "2021"

[dependencies]
reqwest = "^0.11"
tokio = { version = "^1.40", features = ["macros", "rt-multi-thread"] }
serde = { version = "^1.0", features = ["derive"] }
reqwest = "^0.12"
serde_json = "^1.0"
tokio = { version = "^1.17", features = ["macros", "rt-multi-thread"] }
15 changes: 9 additions & 6 deletions src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ use std::fs::File;
use std::io::Read;
use std::time::Duration;

use crate::models::{Anime, Character, Manga, Person};
use crate::Result;

#[derive(Clone)]
Expand Down Expand Up @@ -35,15 +36,15 @@ impl Client {

pub async fn get_anime(&self, variables: serde_json::Value) -> Result<crate::models::Anime> {
let data = self.request("anime", "get", variables).await.unwrap();
let mut anime = crate::models::Anime::parse(&data["data"]["Media"]);
let mut anime = serde_json::from_str::<Anime>(&data["data"]["Media"].to_string()).unwrap();
anime.is_full_loaded = true;

Ok(anime)
}

pub async fn get_manga(&self, variables: serde_json::Value) -> Result<crate::models::Manga> {
let data = self.request("manga", "get", variables).await.unwrap();
let mut manga = crate::models::Manga::parse(&data["data"]["Media"]);
let mut manga = serde_json::from_str::<Manga>(&data["data"]["Media"].to_string()).unwrap();
manga.is_full_loaded = true;

Ok(manga)
Expand All @@ -54,7 +55,8 @@ impl Client {
variables: serde_json::Value,
) -> Result<crate::models::Character> {
let data = self.request("character", "get", variables).await.unwrap();
let mut character = crate::models::Character::parse(&data["data"]["Character"]);
let mut character =
serde_json::from_str::<Character>(&data["data"]["Character"].to_string()).unwrap();
character.is_full_loaded = true;

Ok(character)
Expand All @@ -69,7 +71,8 @@ impl Client {
.request("person", "get", serde_json::json!({ "id": id }))
.await
.unwrap();
let mut person = crate::models::Person::parse(&data["data"]["Staff"]);
let mut person =
serde_json::from_str::<Person>(&data["data"]["Staff"].to_string()).unwrap();
person.is_full_loaded = true;

Ok(person)
Expand Down Expand Up @@ -109,11 +112,11 @@ impl Client {
Ok(result)
}

pub fn get_query(media_type: &str, action: &str) -> Result<String> {
pub(crate) fn get_query(media_type: &str, action: &str) -> Result<String> {
let mut graphql_query = String::new();

let media_type = media_type.to_lowercase();
let media_types = vec!["anime", "manga", "character", "user", "person", "studio"];
let media_types = ["anime", "manga", "character", "user", "person", "studio"];
if !media_types.contains(&media_type.as_str()) {
panic!("The media type '{}' does not exits", { media_type });
}
Expand Down
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,5 +12,5 @@ pub mod models;
mod client;
mod error;

pub use self::client::Client;
pub use self::error::{Error, Result};
pub use client::Client;
pub use error::{Error, Result};
Loading

0 comments on commit 3d0179a

Please sign in to comment.