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,
)) {
for game in response.into_iter() {
println!("{} - {}", game.id, game.name);
println!("{} - {}", game.id.0, game.name);
}
} else {
println!("No response");

View file

@ -7,17 +7,21 @@ pub mod search_games;
use schema::schema;
// Types
// 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
// new scalar type that serializes to u64.
#[derive(cynic::Scalar, Debug, Copy, Clone)]
pub struct ID(pub u64);
impl Display for ID {
fn fmt(&self, f: &mut Formatter<'_>) -> core::fmt::Result {
<u64 as Display>::fmt(&self.0, f)
}
}
// Wrapper types to differentiate between different types of IDs
#[derive(Debug, Copy, Clone)]
pub struct VideogameId(pub u64);
#[derive(Debug, Copy, Clone)]
pub struct PlayerId(pub u64);
// Query machinery
pub trait QueryUnwrap<Vars>: QueryBuilder<Vars> {
type Unwrapped;

View file

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