From 7751829bd5bb6eaff1a83442e031020ecb8c89bb Mon Sep 17 00:00:00 2001 From: Kiana Sheibani Date: Sun, 27 Aug 2023 05:33:03 -0400 Subject: [PATCH] Add ID wrapper types --- cli/src/main.rs | 2 +- cli/src/queries.rs | 14 +++++++++----- cli/src/queries/search_games.rs | 6 +++--- 3 files changed, 13 insertions(+), 9 deletions(-) diff --git a/cli/src/main.rs b/cli/src/main.rs index 1a1eabb..545107c 100644 --- a/cli/src/main.rs +++ b/cli/src/main.rs @@ -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"); diff --git a/cli/src/queries.rs b/cli/src/queries.rs index aedb693..c33e0ef 100644 --- a/cli/src/queries.rs +++ b/cli/src/queries.rs @@ -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 { - ::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: QueryBuilder { type Unwrapped; diff --git a/cli/src/queries/search_games.rs b/cli/src/queries/search_games.rs index bca68e3..97e1c27 100644 --- a/cli/src/queries/search_games.rs +++ b/cli/src/queries/search_games.rs @@ -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 for VideogameSearch { .map(|game| { let game_ = game?; Some(VideogameResponse { - id: game_.id?, + id: VideogameId(game_.id?.0), name: game_.name?, }) })