1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
use cynic::http::ReqwestExt;
use cynic::QueryBuilder;
use reqwest::Client;
use crate::classes::{Class, ClassSpellCasting};
use super::shared::{ApiError, schema};

#[derive(cynic::QueryVariables, Debug)]
pub struct SpellsQueryVariables {
    pub class: Option<StringFilter>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cynic(graphql_type = "Query", variables = "SpellsQueryVariables")]
pub struct SpellsQuery {
    #[arguments(class: $class)]
    pub spells: Option<Vec<Spell>>,
}

#[derive(cynic::QueryFragment, Debug)]
#[cfg_attr(feature = "serde", derive(serde::Serialize))]
pub struct Spell {
    pub index: String,
    pub level: i32,
}

#[derive(cynic::Scalar, Debug, Clone)]
pub struct StringFilter(pub String);

impl Class {
    /// Returns the spells that the class can cast
    /// If it's a knowladge based class it will return the spells that the character can know
    /// If it's a prepared based class it will return the spells that the character can prepare
    pub async fn get_spells(&self) -> Result<Vec<Spell>, ApiError> {
        let op = SpellsQuery::build(SpellsQueryVariables {
            class: Some(StringFilter(self.index().to_string())),
        });

        let spells = Client::new()
            .post("https://www.dnd5eapi.co/graphql")
            .run_graphql(op).await?
            .data.ok_or(ApiError::Schema)?
            .spells.ok_or(ApiError::Schema)?;

        Ok(spells)
    }

    pub async fn get_ready_spells(&self) -> Result<Vec<Vec<String>>, ApiError> {
        match &self.1.spell_casting {
            None => {
                Ok(Vec::new())
            }
            Some(spell_casting) => {
                match spell_casting {
                    ClassSpellCasting::KnowledgePrepared { .. } => {
                        Ok(Vec::new())
                    }
                    ClassSpellCasting::AlreadyKnowPrepared { spells_prepared_index, .. } => {
                        Ok(spells_prepared_index.clone())
                    }
                    ClassSpellCasting::KnowledgeAlreadyPrepared { .. } => {
                        Ok(Vec::new())
                    }
                }
            }
        }
    }
}