Add ID wrapper types

This commit is contained in:
Kiana Sheibani 2023-08-27 05:33:03 -04:00
parent c3a42da1d2
commit 7751829bd5
Signed by: toki
GPG key ID: 6CB106C25E86A9F7
3 changed files with 13 additions and 9 deletions

View file

@ -45,7 +45,7 @@ fn main() {
&auth_key, &auth_key,
)) { )) {
for game in response.into_iter() { for game in response.into_iter() {
println!("{} - {}", game.id, game.name); println!("{} - {}", game.id.0, game.name);
} }
} else { } else {
println!("No response"); println!("No response");

View file

@ -7,17 +7,21 @@ pub mod search_games;
use schema::schema; use schema::schema;
// Types
// HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas // HACK: Unfortunately, start.gg seems to use integers for its ID type, whereas
// cynic always assumes that IDs are strings. To get around that, we define a // cynic always assumes that IDs are strings. To get around that, we define a
// new scalar type that serializes to u64. // new scalar type that serializes to u64.
#[derive(cynic::Scalar, Debug, Copy, Clone)] #[derive(cynic::Scalar, Debug, Copy, Clone)]
pub struct ID(pub u64); pub struct ID(pub u64);
impl Display for ID { // Wrapper types to differentiate between different types of IDs
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result { #[derive(Debug, Copy, Clone)]
<u64 as Display>::fmt(&self.0, f) pub struct VideogameId(pub u64);
} #[derive(Debug, Copy, Clone)]
} pub struct PlayerId(pub u64);
// Query machinery
pub trait QueryUnwrap<Vars>: QueryBuilder<Vars> { pub trait QueryUnwrap<Vars>: QueryBuilder<Vars> {
type Unwrapped; type Unwrapped;

View file

@ -1,4 +1,4 @@
use super::{QueryUnwrap, ID}; use super::{QueryUnwrap, VideogameId, ID};
use cynic::GraphQlResponse; use cynic::GraphQlResponse;
use schema::schema; use schema::schema;
@ -32,7 +32,7 @@ pub struct Videogame {
// Unwrapping // Unwrapping
pub struct VideogameResponse { pub struct VideogameResponse {
pub id: ID, pub id: VideogameId,
pub name: String, pub name: String,
} }
@ -51,7 +51,7 @@ impl QueryUnwrap<VideogameSearchVars> for VideogameSearch {
.map(|game| { .map(|game| {
let game_ = game?; let game_ = game?;
Some(VideogameResponse { Some(VideogameResponse {
id: game_.id?, id: VideogameId(game_.id?.0),
name: game_.name?, name: game_.name?,
}) })
}) })