@@ -54,7 +54,7 @@ use std::str;
5454
5555use derive_more:: with_trait:: { Debug , Display , Error , Into } ;
5656
57- use crate :: graphql_scalar;
57+ use crate :: { ScalarValue , graphql_scalar} ;
5858
5959/// Representation of a civil date in the Gregorian calendar.
6060///
@@ -84,8 +84,8 @@ mod local_date {
8484 /// [1]: https://graphql-scalars.dev/docs/scalars/local-date
8585 const FORMAT : & str = "%Y-%m-%d" ;
8686
87- pub ( super ) fn to_output ( v : & LocalDate ) -> String {
88- v. strftime ( FORMAT ) . to_string ( ) // TODO: Optimize via `Display`?
87+ pub ( super ) fn to_output ( v : & LocalDate ) -> impl Display {
88+ v. strftime ( FORMAT )
8989 }
9090
9191 pub ( super ) fn from_input ( s : & str ) -> Result < LocalDate , Box < str > > {
@@ -131,13 +131,12 @@ mod local_time {
131131 /// [1]: https://graphql-scalars.dev/docs/scalars/local-time
132132 const FORMAT_NO_SECS : & str = "%H:%M" ;
133133
134- pub ( super ) fn to_output ( v : & LocalTime ) -> String {
134+ pub ( super ) fn to_output ( v : & LocalTime ) -> impl Display {
135135 if v. subsec_nanosecond ( ) == 0 {
136136 v. strftime ( FORMAT_NO_MILLIS )
137137 } else {
138138 v. strftime ( FORMAT )
139139 }
140- . to_string ( ) // TODO: Optimize via `Display`?
141140 }
142141
143142 pub ( super ) fn from_input ( s : & str ) -> Result < LocalTime , Box < str > > {
@@ -180,8 +179,8 @@ mod local_date_time {
180179 /// [1]: https://graphql-scalars.dev/docs/scalars/local-date-time
181180 const FORMAT : & str = "%Y-%m-%dT%H:%M:%S" ;
182181
183- pub ( super ) fn to_output ( v : & LocalDateTime ) -> String {
184- v. strftime ( FORMAT ) . to_string ( ) // TODO: Optimize via `Display`?
182+ pub ( super ) fn to_output ( v : & LocalDateTime ) -> impl Display {
183+ v. strftime ( FORMAT )
185184 }
186185
187186 pub ( super ) fn from_input ( s : & str ) -> Result < LocalDateTime , Box < str > > {
@@ -209,21 +208,20 @@ mod local_date_time {
209208pub type DateTime = jiff:: Timestamp ;
210209
211210mod date_time {
212- use std:: str:: FromStr as _;
213-
214211 use super :: * ;
215212
216213 /// Format of a [`DateTime` scalar][1].
217214 ///
218215 /// [1]: https://graphql-scalars.dev/docs/scalars/date-time
219216 const FORMAT : & str = "%Y-%m-%dT%H:%M:%S%.fZ" ;
220217
221- pub ( super ) fn to_output ( v : & DateTime ) -> String {
222- v. strftime ( FORMAT ) . to_string ( ) // TODO: Optimize via `Display`?
218+ pub ( super ) fn to_output ( v : & DateTime ) -> impl Display {
219+ v. strftime ( FORMAT )
223220 }
224221
225222 pub ( super ) fn from_input ( s : & str ) -> Result < DateTime , Box < str > > {
226- DateTime :: from_str ( s) . map_err ( |e| format ! ( "Invalid `DateTime`: {e}" ) . into ( ) )
223+ s. parse ( )
224+ . map_err ( |e| format ! ( "Invalid `DateTime`: {e}" ) . into ( ) )
227225 }
228226}
229227
@@ -246,22 +244,18 @@ mod date_time {
246244#[ graphql_scalar]
247245#[ graphql(
248246 with = zoned_date_time,
247+ to_output_with = ScalarValue :: from_displayable,
249248 parse_token( String ) ,
250249 specified_by_url = "https://datatracker.ietf.org/doc/html/rfc9557#section-4.1" ,
251250) ]
252251pub type ZonedDateTime = jiff:: Zoned ;
253252
254253mod zoned_date_time {
255- use std:: str:: FromStr as _;
256-
257- use super :: * ;
258-
259- pub ( super ) fn to_output ( v : & ZonedDateTime ) -> String {
260- v. to_string ( ) // TODO: Optimize via `Display`?
261- }
254+ use super :: ZonedDateTime ;
262255
263256 pub ( super ) fn from_input ( s : & str ) -> Result < ZonedDateTime , Box < str > > {
264- ZonedDateTime :: from_str ( s) . map_err ( |e| format ! ( "Invalid `ZonedDateTime`: {e}" ) . into ( ) )
257+ s. parse ( )
258+ . map_err ( |e| format ! ( "Invalid `ZonedDateTime`: {e}" ) . into ( ) )
265259 }
266260}
267261
@@ -279,22 +273,18 @@ mod zoned_date_time {
279273#[ graphql_scalar]
280274#[ graphql(
281275 with = duration,
276+ to_output_with = ScalarValue :: from_displayable,
282277 parse_token( String ) ,
283278 specified_by_url = "https://graphql-scalars.dev/docs/scalars/duration" ,
284279) ]
285280pub type Duration = jiff:: Span ;
286281
287282mod duration {
288- use std:: str:: FromStr as _;
289-
290- use super :: * ;
291-
292- pub ( super ) fn to_output ( v : & Duration ) -> String {
293- v. to_string ( ) // TODO: Optimize via `Display`?
294- }
283+ use super :: Duration ;
295284
296285 pub ( super ) fn from_input ( s : & str ) -> Result < Duration , Box < str > > {
297- Duration :: from_str ( s) . map_err ( |e| format ! ( "Invalid `Duration`: {e}" ) . into ( ) )
286+ s. parse ( )
287+ . map_err ( |e| format ! ( "Invalid `Duration`: {e}" ) . into ( ) )
298288 }
299289}
300290
@@ -379,6 +369,7 @@ pub enum TimeZoneParsingError {
379369#[ graphql_scalar]
380370#[ graphql(
381371 with = time_zone,
372+ to_output_with = ScalarValue :: from_displayable,
382373 parse_token( String ) ,
383374 specified_by_url = "https://graphql-scalars.dev/docs/scalars/time-zone" ,
384375) ]
@@ -408,11 +399,7 @@ impl str::FromStr for TimeZone {
408399}
409400
410401mod time_zone {
411- use super :: * ;
412-
413- pub ( super ) fn to_output ( v : & TimeZone ) -> String {
414- v. to_string ( ) // TODO: Optimize via `Display`?
415- }
402+ use super :: TimeZone ;
416403
417404 pub ( super ) fn from_input ( s : & str ) -> Result < TimeZone , Box < str > > {
418405 s. parse ( )
0 commit comments