dnd_character/api/
spells.rs

1use cynic::http::ReqwestExt;
2use cynic::QueryBuilder;
3use reqwest::Client;
4use crate::classes::{Class, ClassSpellCasting};
5use crate::GRAPHQL_API_URL;
6use super::shared::{ApiError, schema};
7
8#[derive(cynic::QueryVariables, Debug)]
9pub struct SpellsQueryVariables {
10    pub class: Option<StringFilter>,
11}
12
13#[derive(cynic::QueryFragment, Debug)]
14#[cynic(graphql_type = "Query", variables = "SpellsQueryVariables")]
15pub struct SpellsQuery {
16    #[arguments(class: $class)]
17    pub spells: Option<Vec<Spell>>,
18}
19
20#[derive(cynic::QueryFragment, Debug)]
21#[cfg_attr(feature = "serde", derive(serde::Serialize))]
22#[cfg_attr(feature = "utoipa", derive(utoipa::ToSchema))]
23pub struct Spell {
24    pub index: String,
25    pub level: i32,
26}
27
28#[derive(cynic::Scalar, Debug, Clone)]
29pub struct StringFilter(pub String);
30
31impl Class {
32    /// Returns the spells that the class can cast
33    /// If it's a knowladge based class it will return the spells that the character can know
34    /// If it's a prepared based class it will return the spells that the character can prepare
35    pub async fn get_spells(&self) -> Result<Vec<Spell>, ApiError> {
36        let op = SpellsQuery::build(SpellsQueryVariables {
37            class: Some(StringFilter(self.index().to_string())),
38        });
39
40        let spells = Client::new()
41            .post(GRAPHQL_API_URL.as_str())
42            .run_graphql(op).await?
43            .data.ok_or(ApiError::Schema)?
44            .spells.ok_or(ApiError::Schema)?;
45
46        Ok(spells)
47    }
48
49    pub async fn get_ready_spells(&self) -> Result<Vec<Vec<String>>, ApiError> {
50        match &self.1.spell_casting {
51            None => {
52                Ok(Vec::new())
53            }
54            Some(spell_casting) => {
55                match spell_casting {
56                    ClassSpellCasting::KnowledgePrepared { .. } => {
57                        Ok(Vec::new())
58                    }
59                    ClassSpellCasting::AlreadyKnowPrepared { spells_prepared_index, .. } => {
60                        Ok(spells_prepared_index.clone())
61                    }
62                    ClassSpellCasting::KnowledgeAlreadyPrepared { .. } => {
63                        Ok(Vec::new())
64                    }
65                }
66            }
67        }
68    }
69}