Skip to content

Commit db9a189

Browse files
committed
Change macro expansion and apply ergonomics
1 parent 63e751e commit db9a189

File tree

13 files changed

+332
-315
lines changed

13 files changed

+332
-315
lines changed

juniper/src/executor_tests/variables.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
use crate::{
2-
GraphQLInputObject, GraphQLScalar, ScalarValue, Value,
2+
GraphQLInputObject, GraphQLScalar,
33
executor::Variables,
44
graphql_object, graphql_value, graphql_vars,
55
parser::SourcePosition,
@@ -14,8 +14,8 @@ use crate::{
1414
struct TestComplexScalar;
1515

1616
impl TestComplexScalar {
17-
fn to_output<S: ScalarValue>(&self) -> Value<S> {
18-
graphql_value!("SerializedValue")
17+
fn to_output(&self) -> &'static str {
18+
"SerializedValue"
1919
}
2020

2121
fn from_input(s: &str) -> Result<Self, Box<str>> {

juniper/src/integrations/bigdecimal.rs

Lines changed: 12 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,7 @@
88
//!
99
//! [`BigDecimal`]: bigdecimal::BigDecimal
1010
11-
use std::str::FromStr as _;
12-
13-
use crate::{Scalar, ScalarValue, Value, graphql_scalar};
11+
use crate::graphql_scalar;
1412

1513
// TODO: Try remove on upgrade of `bigdecimal` crate.
1614
mod for_minimal_versions_check_only {
@@ -29,18 +27,20 @@ mod for_minimal_versions_check_only {
2927
/// See also [`bigdecimal`] crate for details.
3028
///
3129
/// [`bigdecimal`]: https://docs.rs/bigdecimal
32-
#[graphql_scalar(
30+
#[graphql_scalar]
31+
#[graphql(
3332
with = bigdecimal_scalar,
3433
parse_token(i32, f64, String),
3534
specified_by_url = "https://docs.rs/bigdecimal",
3635
)]
3736
type BigDecimal = bigdecimal::BigDecimal;
3837

3938
mod bigdecimal_scalar {
40-
use super::*;
39+
use super::BigDecimal;
40+
use crate::{Scalar, ScalarValue};
4141

42-
pub(super) fn to_output<S: ScalarValue>(v: &BigDecimal) -> Value<S> {
43-
Value::scalar(v.to_string())
42+
pub(super) fn to_output(v: &BigDecimal) -> String {
43+
v.to_string() // TODO: Optimize via `Display`?
4444
}
4545

4646
pub(super) fn from_input(v: &Scalar<impl ScalarValue>) -> Result<BigDecimal, Box<str>> {
@@ -50,13 +50,14 @@ mod bigdecimal_scalar {
5050
// See akubera/bigdecimal-rs#103 for details:
5151
// https://github.com/akubera/bigdecimal-rs/issues/103
5252
let mut buf = ryu::Buffer::new();
53-
BigDecimal::from_str(buf.format(f))
53+
buf.format(f)
54+
.parse::<BigDecimal>()
5455
.map_err(|e| format!("Failed to parse `BigDecimal` from `Float`: {e}").into())
5556
} else {
5657
v.try_to::<&str>()
5758
.map_err(|e| e.to_string().into())
5859
.and_then(|s| {
59-
BigDecimal::from_str(s).map_err(|e| {
60+
s.parse::<BigDecimal>().map_err(|e| {
6061
format!("Failed to parse `BigDecimal` from `String`: {e}").into()
6162
})
6263
})
@@ -66,8 +67,6 @@ mod bigdecimal_scalar {
6667

6768
#[cfg(test)]
6869
mod test {
69-
use std::str::FromStr as _;
70-
7170
use crate::{FromInputValue as _, InputValue, ToInputValue as _, graphql_input_value};
7271

7372
use super::BigDecimal;
@@ -91,7 +90,7 @@ mod test {
9190
] {
9291
let input: InputValue = input;
9392
let parsed = BigDecimal::from_input_value(&input);
94-
let expected = BigDecimal::from_str(expected).unwrap();
93+
let expected = expected.parse::<BigDecimal>().unwrap();
9594

9695
assert!(
9796
parsed.is_ok(),
@@ -130,7 +129,7 @@ mod test {
130129
"123",
131130
"43.44",
132131
] {
133-
let actual: InputValue = BigDecimal::from_str(raw).unwrap().to_input_value();
132+
let actual: InputValue = raw.parse::<BigDecimal>().unwrap().to_input_value();
134133

135134
assert_eq!(actual, graphql_input_value!((raw)), "on value: {raw}");
136135
}

juniper/src/integrations/bson.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@
1313
//! [s1]: https://graphql-scalars.dev/docs/scalars/object-id
1414
//! [s4]: https://graphql-scalars.dev/docs/scalars/date-time
1515
16-
use crate::{ScalarValue, Value, graphql_scalar};
16+
use crate::graphql_scalar;
1717

1818
// TODO: Try remove on upgrade of `bson` crate.
1919
mod for_minimal_versions_check_only {
@@ -29,7 +29,8 @@ mod for_minimal_versions_check_only {
2929
/// [0]: https://www.mongodb.com/docs/manual/reference/bson-types#objectid
3030
/// [1]: https://graphql-scalars.dev/docs/scalars/object-id
3131
/// [2]: https://docs.rs/bson/*/bson/oid/struct.ObjectId.html
32-
#[graphql_scalar(
32+
#[graphql_scalar]
33+
#[graphql(
3334
name = "ObjectID",
3435
with = object_id,
3536
parse_token(String),
@@ -38,10 +39,10 @@ mod for_minimal_versions_check_only {
3839
type ObjectId = bson::oid::ObjectId;
3940

4041
mod object_id {
41-
use super::*;
42+
use super::ObjectId;
4243

43-
pub(super) fn to_output<S: ScalarValue>(v: &ObjectId) -> Value<S> {
44-
Value::scalar(v.to_hex())
44+
pub(super) fn to_output(v: &ObjectId) -> String {
45+
v.to_hex()
4546
}
4647

4748
pub(super) fn from_input(s: &str) -> Result<ObjectId, Box<str>> {
@@ -62,21 +63,20 @@ mod object_id {
6263
/// [1]: https://graphql-scalars.dev/docs/scalars/date-time
6364
/// [2]: https://docs.rs/bson/*/bson/struct.DateTime.html
6465
/// [3]: https://www.mongodb.com/docs/manual/reference/bson-types#date
65-
#[graphql_scalar(
66+
#[graphql_scalar]
67+
#[graphql(
6668
with = date_time,
6769
parse_token(String),
6870
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date-time",
6971
)]
7072
type DateTime = bson::DateTime;
7173

7274
mod date_time {
73-
use super::*;
75+
use super::DateTime;
7476

75-
pub(super) fn to_output<S: ScalarValue>(v: &DateTime) -> Value<S> {
76-
Value::scalar(
77-
(*v).try_to_rfc3339_string()
78-
.unwrap_or_else(|e| panic!("failed to format `DateTime` as RFC 3339: {e}")),
79-
)
77+
pub(super) fn to_output(v: &DateTime) -> String {
78+
(*v).try_to_rfc3339_string()
79+
.unwrap_or_else(|e| panic!("failed to format `DateTime` as RFC 3339: {e}"))
8080
}
8181

8282
pub(super) fn from_input(s: &str) -> Result<DateTime, Box<str>> {

juniper/src/integrations/chrono.rs

Lines changed: 31 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ use std::fmt;
2323

2424
use chrono::{FixedOffset, TimeZone};
2525

26-
use crate::{ScalarValue, Value, graphql_scalar};
26+
use crate::graphql_scalar;
2727

2828
/// Date in the proleptic Gregorian calendar (without time zone).
2929
///
@@ -36,26 +36,24 @@ use crate::{ScalarValue, Value, graphql_scalar};
3636
///
3737
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
3838
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDate.html
39-
#[graphql_scalar(
39+
#[graphql_scalar]
40+
#[graphql(
4041
with = local_date,
4142
parse_token(String),
4243
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date",
4344
)]
4445
pub type LocalDate = chrono::NaiveDate;
4546

4647
mod local_date {
47-
use super::*;
48+
use super::LocalDate;
4849

4950
/// Format of a [`LocalDate` scalar][1].
5051
///
5152
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date
5253
const FORMAT: &str = "%Y-%m-%d";
5354

54-
pub(super) fn to_output<S>(v: &LocalDate) -> Value<S>
55-
where
56-
S: ScalarValue,
57-
{
58-
Value::scalar(v.format(FORMAT).to_string())
55+
pub(super) fn to_output(v: &LocalDate) -> String {
56+
v.format(FORMAT).to_string() // TODO: Optimize via `Display`?
5957
}
6058

6159
pub(super) fn from_input(s: &str) -> Result<LocalDate, Box<str>> {
@@ -75,7 +73,8 @@ mod local_date {
7573
///
7674
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
7775
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveTime.html
78-
#[graphql_scalar(
76+
#[graphql_scalar]
77+
#[graphql(
7978
with = local_time,
8079
parse_token(String),
8180
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-time",
@@ -85,7 +84,7 @@ pub type LocalTime = chrono::NaiveTime;
8584
mod local_time {
8685
use chrono::Timelike as _;
8786

88-
use super::*;
87+
use super::LocalTime;
8988

9089
/// Full format of a [`LocalTime` scalar][1].
9190
///
@@ -102,18 +101,13 @@ mod local_time {
102101
/// [1]: https://graphql-scalars.dev/docs/scalars/local-time
103102
const FORMAT_NO_SECS: &str = "%H:%M";
104103

105-
pub(super) fn to_output<S>(v: &LocalTime) -> Value<S>
106-
where
107-
S: ScalarValue,
108-
{
109-
Value::scalar(
110-
if v.nanosecond() == 0 {
111-
v.format(FORMAT_NO_MILLIS)
112-
} else {
113-
v.format(FORMAT)
114-
}
115-
.to_string(),
116-
)
104+
pub(super) fn to_output(v: &LocalTime) -> String {
105+
if v.nanosecond() == 0 {
106+
v.format(FORMAT_NO_MILLIS)
107+
} else {
108+
v.format(FORMAT)
109+
}
110+
.to_string() // TODO: Optimize via `Display`?
117111
}
118112

119113
pub(super) fn from_input(s: &str) -> Result<LocalTime, Box<str>> {
@@ -134,26 +128,24 @@ mod local_time {
134128
///
135129
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
136130
/// [2]: https://docs.rs/chrono/latest/chrono/naive/struct.NaiveDateTime.html
137-
#[graphql_scalar(
131+
#[graphql_scalar]
132+
#[graphql(
138133
with = local_date_time,
139134
parse_token(String),
140135
specified_by_url = "https://graphql-scalars.dev/docs/scalars/local-date-time",
141136
)]
142137
pub type LocalDateTime = chrono::NaiveDateTime;
143138

144139
mod local_date_time {
145-
use super::*;
140+
use super::LocalDateTime;
146141

147142
/// Format of a [`LocalDateTime` scalar][1].
148143
///
149144
/// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
150145
const FORMAT: &str = "%Y-%m-%dT%H:%M:%S";
151146

152-
pub(super) fn to_output<S>(v: &LocalDateTime) -> Value<S>
153-
where
154-
S: ScalarValue,
155-
{
156-
Value::scalar(v.format(FORMAT).to_string())
147+
pub(super) fn to_output(v: &LocalDateTime) -> String {
148+
v.format(FORMAT).to_string() // TODO: Optimize via `Display`?
157149
}
158150

159151
pub(super) fn from_input(s: &str) -> Result<LocalDateTime, Box<str>> {
@@ -174,7 +166,8 @@ mod local_date_time {
174166
/// [0]: https://datatracker.ietf.org/doc/html/rfc3339#section-5
175167
/// [1]: https://graphql-scalars.dev/docs/scalars/date-time
176168
/// [2]: https://docs.rs/chrono/latest/chrono/struct.DateTime.html
177-
#[graphql_scalar(
169+
#[graphql_scalar]
170+
#[graphql(
178171
with = date_time,
179172
parse_token(String),
180173
specified_by_url = "https://graphql-scalars.dev/docs/scalars/date-time",
@@ -186,20 +179,19 @@ mod local_date_time {
186179
pub type DateTime<Tz> = chrono::DateTime<Tz>;
187180

188181
mod date_time {
189-
use chrono::{SecondsFormat, Utc};
182+
use std::fmt;
183+
184+
use chrono::{FixedOffset, SecondsFormat, TimeZone, Utc};
190185

191-
use super::*;
186+
use super::{DateTime, FromFixedOffset};
192187

193-
pub(super) fn to_output<S, Tz>(v: &DateTime<Tz>) -> Value<S>
188+
pub(super) fn to_output<Tz>(v: &DateTime<Tz>) -> String
194189
where
195-
S: ScalarValue,
196-
Tz: chrono::TimeZone,
190+
Tz: TimeZone,
197191
Tz::Offset: fmt::Display,
198192
{
199-
Value::scalar(
200-
v.with_timezone(&Utc)
201-
.to_rfc3339_opts(SecondsFormat::AutoSi, true),
202-
)
193+
v.with_timezone(&Utc)
194+
.to_rfc3339_opts(SecondsFormat::AutoSi, true) // TODO: Optimize via `Display`?
203195
}
204196

205197
pub(super) fn from_input<Tz>(s: &str) -> Result<DateTime<Tz>, Box<str>>

juniper/src/integrations/chrono_tz.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@
1111
//! [1]: http://www.iana.org/time-zones
1212
//! [s1]: https://graphql-scalars.dev/docs/scalars/time-zone
1313
14-
use crate::{ScalarValue, Value, graphql_scalar};
14+
use crate::graphql_scalar;
1515

1616
// TODO: Try remove on upgrade of `chrono-tz` crate.
1717
mod for_minimal_versions_check_only {
@@ -31,18 +31,19 @@ mod for_minimal_versions_check_only {
3131
/// [1]: https://graphql-scalars.dev/docs/scalars/time-zone
3232
/// [2]: https://docs.rs/chrono-tz/*/chrono_tz/enum.Tz.html
3333
/// [3]: https://en.wikipedia.org/wiki/List_of_tz_database_time_zones
34-
#[graphql_scalar(
34+
#[graphql_scalar]
35+
#[graphql(
3536
with = tz,
3637
parse_token(String),
3738
specified_by_url = "https://graphql-scalars.dev/docs/scalars/time-zone",
3839
)]
3940
pub type TimeZone = chrono_tz::Tz;
4041

4142
mod tz {
42-
use super::*;
43+
use super::TimeZone;
4344

44-
pub(super) fn to_output<S: ScalarValue>(v: &TimeZone) -> Value<S> {
45-
Value::scalar(v.name().to_owned())
45+
pub(super) fn to_output(v: &TimeZone) -> &'static str {
46+
v.name()
4647
}
4748

4849
pub(super) fn from_input(s: &str) -> Result<TimeZone, Box<str>> {

0 commit comments

Comments
 (0)