Skip to content

Int as_i32 feature for backward compliant GraphQL spec compliance #65

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,8 @@ edition = "2018"
combine = "3.2.0"
thiserror = "1.0.11"

[features]
as_i32 = []

[dev-dependencies]
pretty_assertions = "0.5.0"
12 changes: 11 additions & 1 deletion src/common.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,12 @@ pub struct Directive<'a, T: Text<'a>> {
#[derive(Debug, Clone, PartialEq)]
// we use i64 as a reference implementation: graphql-js thinks even 32bit
// integers is enough. We might consider lift this limit later though
#[cfg(not(feature = "as_i32"))]
pub struct Number(pub(crate) i64);
#[derive(Debug, Clone, PartialEq)]
#[cfg(feature = "as_i32")]
pub struct Number(pub(crate) i32);


#[derive(Debug, Clone, PartialEq)]
pub enum Value<'a, T: Text<'a>> {
Expand Down Expand Up @@ -85,7 +90,9 @@ pub enum Type<'a, T: Text<'a>> {
NonNullType(Box<Type<'a, T>>),
}


impl Number {
#[cfg(not(feature = "as_i32"))]
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Feature flags should be additive. We can still provide as_i64 even with an i32 because that has an impl Into.

/// Returns a number as i64 if it fits the type
pub fn as_i64(&self) -> Option<i64> {
Some(self.0)
Expand All @@ -94,7 +101,9 @@ impl Number {

impl From<i32> for Number {
fn from(i: i32) -> Self {
Number(i as i64)
#[cfg(not(feature = "as_i32"))]
let i = i as i64;
Number(i)
}
}

Expand Down Expand Up @@ -328,6 +337,7 @@ mod tests {
use super::Number;
use super::unquote_string;

#[cfg(not(feature = "as_i32"))]
#[test]
fn number_from_i32_and_to_i64_conversion() {
assert_eq!(Number::from(1).as_i64(), Some(1));
Expand Down