dnd_character/api/
spells.rs1use super::shared::{schema, ApiError};
2use crate::classes::{Class, ClassSpellCasting};
3use crate::GRAPHQL_API_URL;
4use cynic::http::ReqwestExt;
5use cynic::QueryBuilder;
6use reqwest::Client;
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 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)
43 .await?
44 .data
45 .ok_or(ApiError::Schema)?
46 .spells
47 .ok_or(ApiError::Schema)?;
48
49 Ok(spells)
50 }
51
52 pub async fn get_ready_spells(&self) -> Result<Vec<Vec<String>>, ApiError> {
53 match &self.1.spell_casting {
54 None => Ok(Vec::new()),
55 Some(spell_casting) => match spell_casting {
56 ClassSpellCasting::KnowledgePrepared { .. } => Ok(Vec::new()),
57 ClassSpellCasting::AlreadyKnowPrepared {
58 spells_prepared_index,
59 ..
60 } => Ok(spells_prepared_index.clone()),
61 ClassSpellCasting::KnowledgeAlreadyPrepared { spells_index, .. } => {
62 Ok(spells_index.clone())
63 }
64 },
65 }
66 }
67}