11
11
| | | precise enough for nanoseconds. |
12
12
| | | Values will be truncated to microsecond |
13
13
| | | resolution. |
14
+ | `NaiveTime` | H:M:S | Optional. Use the `scalar-naivetime` |
15
+ | | | feature. |
14
16
15
17
*/
16
18
#![ allow( clippy:: needless_lifetimes) ]
@@ -99,6 +101,30 @@ where
99
101
}
100
102
}
101
103
104
+ #[ cfg( feature = "scalar-naivetime" ) ]
105
+ #[ crate :: graphql_scalar_internal( description = "NaiveTime" ) ]
106
+ impl < S > GraphQLScalar for NaiveTime
107
+ where
108
+ S : ScalarValue ,
109
+ {
110
+ fn resolve ( & self ) -> Value {
111
+ Value :: scalar ( self . format ( "%H:%M:%S" ) . to_string ( ) )
112
+ }
113
+
114
+ fn from_input_value ( v : & InputValue ) -> Option < NaiveTime > {
115
+ v. as_string_value ( )
116
+ . and_then ( |s| NaiveTime :: parse_from_str ( s, "%H:%M:%S" ) . ok ( ) )
117
+ }
118
+
119
+ fn from_str < ' a > ( value : ScalarToken < ' a > ) -> ParseScalarResult < ' a , S > {
120
+ if let ScalarToken :: String ( value) = value {
121
+ Ok ( S :: from ( value. to_owned ( ) ) )
122
+ } else {
123
+ Err ( ParseError :: UnexpectedToken ( Token :: Scalar ( value) ) )
124
+ }
125
+ }
126
+ }
127
+
102
128
// JSON numbers (i.e. IEEE doubles) are not precise enough for nanosecond
103
129
// datetimes. Values will be truncated to microsecond resolution.
104
130
#[ crate :: graphql_scalar_internal( description = "NaiveDateTime" ) ]
@@ -194,6 +220,20 @@ mod test {
194
220
assert_eq ! ( parsed. day( ) , d) ;
195
221
}
196
222
223
+ #[ test]
224
+ #[ cfg( feature = "scalar-naivetime" ) ]
225
+ fn naivetime_from_input_value ( ) {
226
+ let input: crate :: InputValue < DefaultScalarValue > ;
227
+ input = InputValue :: scalar ( "21:12:19" . to_string ( ) ) ;
228
+ let [ h, m, s] = [ 21 , 12 , 19 ] ;
229
+ let parsed: NaiveTime = crate :: FromInputValue :: from_input_value ( & input) . unwrap ( ) ;
230
+ let expected = NaiveTime :: from_hms ( h, m, s) ;
231
+ assert_eq ! ( parsed, expected) ;
232
+ assert_eq ! ( parsed. hour( ) , h) ;
233
+ assert_eq ! ( parsed. minute( ) , m) ;
234
+ assert_eq ! ( parsed. second( ) , s) ;
235
+ }
236
+
197
237
#[ test]
198
238
fn naivedatetime_from_input_value ( ) {
199
239
let raw = 1_000_000_000_f64 ;
@@ -223,6 +263,27 @@ mod integration_test {
223
263
struct Root ;
224
264
225
265
#[ crate :: graphql_object_internal]
266
+ #[ cfg( feature = "scalar-naivetime" ) ]
267
+ impl Root {
268
+ fn exampleNaiveDate ( ) -> NaiveDate {
269
+ NaiveDate :: from_ymd ( 2015 , 3 , 14 )
270
+ }
271
+ fn exampleNaiveDateTime ( ) -> NaiveDateTime {
272
+ NaiveDate :: from_ymd ( 2016 , 7 , 8 ) . and_hms ( 9 , 10 , 11 )
273
+ }
274
+ fn exampleNaiveTime ( ) -> NaiveTime {
275
+ NaiveTime :: from_hms ( 16 , 7 , 8 )
276
+ }
277
+ fn exampleDateTimeFixedOffset ( ) -> DateTime < FixedOffset > {
278
+ DateTime :: parse_from_rfc3339 ( "1996-12-19T16:39:57-08:00" ) . unwrap ( )
279
+ }
280
+ fn exampleDateTimeUtc ( ) -> DateTime < Utc > {
281
+ Utc . timestamp ( 61 , 0 )
282
+ }
283
+ }
284
+
285
+ #[ crate :: graphql_object_internal]
286
+ #[ cfg( not( feature = "scalar-naivetime" ) ) ]
226
287
impl Root {
227
288
fn exampleNaiveDate ( ) -> NaiveDate {
228
289
NaiveDate :: from_ymd ( 2015 , 3 , 14 )
@@ -238,6 +299,18 @@ mod integration_test {
238
299
}
239
300
}
240
301
302
+ #[ cfg( feature = "scalar-naivetime" ) ]
303
+ let doc = r#"
304
+ {
305
+ exampleNaiveDate,
306
+ exampleNaiveDateTime,
307
+ exampleNaiveTime,
308
+ exampleDateTimeFixedOffset,
309
+ exampleDateTimeUtc,
310
+ }
311
+ "# ;
312
+
313
+ #[ cfg( not( feature = "scalar-naivetime" ) ) ]
241
314
let doc = r#"
242
315
{
243
316
exampleNaiveDate,
@@ -265,6 +338,8 @@ mod integration_test {
265
338
vec![
266
339
( "exampleNaiveDate" , Value :: scalar( "2015-03-14" ) ) ,
267
340
( "exampleNaiveDateTime" , Value :: scalar( 1_467_969_011.0 ) ) ,
341
+ #[ cfg( feature = "scalar-naivetime" ) ]
342
+ ( "exampleNaiveTime" , Value :: scalar( "16:07:08" ) ) ,
268
343
(
269
344
"exampleDateTimeFixedOffset" ,
270
345
Value :: scalar( "1996-12-19T16:39:57-08:00" ) ,
0 commit comments