Skip to content

Commit ea52986

Browse files
committed
Use deserialization result in doctests
1 parent f90894a commit ea52986

File tree

2 files changed

+39
-24
lines changed

2 files changed

+39
-24
lines changed

src/datetime/serde.rs

+21-14
Original file line numberDiff line numberDiff line change
@@ -189,16 +189,17 @@ pub mod ts_nanoseconds {
189189
/// # Example:
190190
///
191191
/// ```rust
192-
/// # use chrono::{DateTime, Utc};
192+
/// # use chrono::{DateTime, TimeZone, Utc};
193193
/// # use serde_derive::Deserialize;
194194
/// use chrono::serde::ts_nanoseconds::deserialize as from_nano_ts;
195-
/// #[derive(Deserialize)]
195+
/// #[derive(Debug, PartialEq, Deserialize)]
196196
/// struct S {
197197
/// #[serde(deserialize_with = "from_nano_ts")]
198198
/// time: DateTime<Utc>
199199
/// }
200200
///
201201
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
202+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355733).unwrap() });
202203
/// # Ok::<(), serde_json::Error>(())
203204
/// ```
204205
#[must_use]
@@ -316,16 +317,17 @@ pub mod ts_nanoseconds_option {
316317
/// # Example:
317318
///
318319
/// ```rust
319-
/// # use chrono::{DateTime, Utc};
320+
/// # use chrono::{DateTime, TimeZone, Utc};
320321
/// # use serde_derive::Deserialize;
321322
/// use chrono::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt;
322-
/// #[derive(Deserialize)]
323+
/// #[derive(Debug, PartialEq, Deserialize)]
323324
/// struct S {
324325
/// #[serde(deserialize_with = "from_nano_tsopt")]
325326
/// time: Option<DateTime<Utc>>
326327
/// }
327328
///
328329
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
330+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355733).single() });
329331
/// # Ok::<(), serde_json::Error>(())
330332
/// ```
331333
#[must_use]
@@ -444,16 +446,17 @@ pub mod ts_microseconds {
444446
/// # Example:
445447
///
446448
/// ```rust
447-
/// # use chrono::{DateTime, Utc};
449+
/// # use chrono::{DateTime, TimeZone, Utc};
448450
/// # use serde_derive::Deserialize;
449451
/// use chrono::serde::ts_microseconds::deserialize as from_micro_ts;
450-
/// #[derive(Deserialize)]
452+
/// #[derive(Debug, PartialEq, Deserialize)]
451453
/// struct S {
452454
/// #[serde(deserialize_with = "from_micro_ts")]
453455
/// time: DateTime<Utc>
454456
/// }
455457
///
456458
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
459+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355000).unwrap() });
457460
/// # Ok::<(), serde_json::Error>(())
458461
/// ```
459462
#[must_use]
@@ -570,16 +573,17 @@ pub mod ts_microseconds_option {
570573
/// # Example:
571574
///
572575
/// ```rust
573-
/// # use chrono::{DateTime, Utc};
576+
/// # use chrono::{DateTime, TimeZone, Utc};
574577
/// # use serde_derive::Deserialize;
575578
/// use chrono::serde::ts_microseconds_option::deserialize as from_micro_tsopt;
576-
/// #[derive(Deserialize)]
579+
/// #[derive(Debug, PartialEq, Deserialize)]
577580
/// struct S {
578581
/// #[serde(deserialize_with = "from_micro_tsopt")]
579582
/// time: Option<DateTime<Utc>>
580583
/// }
581584
///
582585
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
586+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918355000).single() });
583587
/// # Ok::<(), serde_json::Error>(())
584588
/// ```
585589
#[must_use]
@@ -698,16 +702,17 @@ pub mod ts_milliseconds {
698702
/// # Example:
699703
///
700704
/// ```rust
701-
/// # use chrono::{DateTime, Utc};
705+
/// # use chrono::{DateTime, TimeZone, Utc};
702706
/// # use serde_derive::Deserialize;
703707
/// use chrono::serde::ts_milliseconds::deserialize as from_milli_ts;
704-
/// #[derive(Deserialize)]
708+
/// #[derive(Debug, PartialEq, Deserialize)]
705709
/// struct S {
706710
/// #[serde(deserialize_with = "from_milli_ts")]
707711
/// time: DateTime<Utc>
708712
/// }
709713
///
710714
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
715+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1526522699, 918000000).unwrap() });
711716
/// # Ok::<(), serde_json::Error>(())
712717
/// ```
713718
#[must_use]
@@ -962,16 +967,17 @@ pub mod ts_seconds {
962967
/// # Example:
963968
///
964969
/// ```rust
965-
/// # use chrono::{DateTime, Utc};
970+
/// # use chrono::{DateTime, TimeZone, Utc};
966971
/// # use serde_derive::Deserialize;
967972
/// use chrono::serde::ts_seconds::deserialize as from_ts;
968-
/// #[derive(Deserialize)]
973+
/// #[derive(Debug, PartialEq, Deserialize)]
969974
/// struct S {
970975
/// #[serde(deserialize_with = "from_ts")]
971976
/// time: DateTime<Utc>
972977
/// }
973978
///
974979
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
980+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1431684000, 0).unwrap() });
975981
/// # Ok::<(), serde_json::Error>(())
976982
/// ```
977983
#[must_use]
@@ -1082,16 +1088,17 @@ pub mod ts_seconds_option {
10821088
/// # Example:
10831089
///
10841090
/// ```rust
1085-
/// # use chrono::{DateTime, Utc};
1091+
/// # use chrono::{DateTime, TimeZone, Utc};
10861092
/// # use serde_derive::Deserialize;
10871093
/// use chrono::serde::ts_seconds_option::deserialize as from_tsopt;
1088-
/// #[derive(Deserialize)]
1094+
/// #[derive(Debug, PartialEq, Deserialize)]
10891095
/// struct S {
10901096
/// #[serde(deserialize_with = "from_tsopt")]
10911097
/// time: Option<DateTime<Utc>>
10921098
/// }
10931099
///
10941100
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
1101+
/// assert_eq!(my_s, S { time: Utc.timestamp_opt(1431684000, 0).single() });
10951102
/// # Ok::<(), serde_json::Error>(())
10961103
/// ```
10971104
#[must_use]

src/naive/datetime/serde.rs

+18-10
Original file line numberDiff line numberDiff line change
@@ -128,13 +128,14 @@ pub mod ts_nanoseconds {
128128
/// # use chrono::NaiveDateTime;
129129
/// # use serde_derive::Deserialize;
130130
/// use chrono::naive::serde::ts_nanoseconds::deserialize as from_nano_ts;
131-
/// #[derive(Deserialize)]
131+
/// #[derive(Debug, PartialEq, Deserialize)]
132132
/// struct S {
133133
/// #[serde(deserialize_with = "from_nano_ts")]
134134
/// time: NaiveDateTime
135135
/// }
136136
///
137137
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
138+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733).unwrap() });
138139
/// # Ok::<(), serde_json::Error>(())
139140
/// ```
140141
#[must_use]
@@ -253,13 +254,14 @@ pub mod ts_nanoseconds_option {
253254
/// # use chrono::naive::NaiveDateTime;
254255
/// # use serde_derive::Deserialize;
255256
/// use chrono::naive::serde::ts_nanoseconds_option::deserialize as from_nano_tsopt;
256-
/// #[derive(Deserialize)]
257+
/// #[derive(Debug, PartialEq, Deserialize)]
257258
/// struct S {
258259
/// #[serde(deserialize_with = "from_nano_tsopt")]
259260
/// time: Option<NaiveDateTime>
260261
/// }
261262
///
262263
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355733 }"#)?;
264+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355733) });
263265
/// # Ok::<(), serde_json::Error>(())
264266
/// ```
265267
#[must_use]
@@ -378,13 +380,14 @@ pub mod ts_microseconds {
378380
/// # use chrono::NaiveDateTime;
379381
/// # use serde_derive::Deserialize;
380382
/// use chrono::naive::serde::ts_microseconds::deserialize as from_micro_ts;
381-
/// #[derive(Deserialize)]
383+
/// #[derive(Debug, PartialEq, Deserialize)]
382384
/// struct S {
383385
/// #[serde(deserialize_with = "from_micro_ts")]
384386
/// time: NaiveDateTime
385387
/// }
386388
///
387389
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
390+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000).unwrap() });
388391
/// # Ok::<(), serde_json::Error>(())
389392
/// ```
390393
#[must_use]
@@ -506,13 +509,14 @@ pub mod ts_microseconds_option {
506509
/// # use chrono::naive::NaiveDateTime;
507510
/// # use serde_derive::Deserialize;
508511
/// use chrono::naive::serde::ts_microseconds_option::deserialize as from_micro_tsopt;
509-
/// #[derive(Deserialize)]
512+
/// #[derive(Debug, PartialEq, Deserialize)]
510513
/// struct S {
511514
/// #[serde(deserialize_with = "from_micro_tsopt")]
512515
/// time: Option<NaiveDateTime>
513516
/// }
514517
///
515518
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
519+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918355000) });
516520
/// # Ok::<(), serde_json::Error>(())
517521
/// ```
518522
#[must_use]
@@ -631,13 +635,14 @@ pub mod ts_milliseconds {
631635
/// # use chrono::NaiveDateTime;
632636
/// # use serde_derive::Deserialize;
633637
/// use chrono::naive::serde::ts_milliseconds::deserialize as from_milli_ts;
634-
/// #[derive(Deserialize)]
638+
/// #[derive(Debug, PartialEq, Deserialize)]
635639
/// struct S {
636640
/// #[serde(deserialize_with = "from_milli_ts")]
637641
/// time: NaiveDateTime
638642
/// }
639643
///
640644
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
645+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000).unwrap() });
641646
/// # Ok::<(), serde_json::Error>(())
642647
/// ```
643648
#[must_use]
@@ -746,7 +751,7 @@ pub mod ts_milliseconds_option {
746751
}
747752
}
748753

749-
/// Deserialize a `NaiveDateTime` from a nanosecond timestamp or none
754+
/// Deserialize a `NaiveDateTime` from a millisecond timestamp or none
750755
///
751756
/// Intended for use with `serde`s `deserialize_with` attribute.
752757
///
@@ -756,13 +761,14 @@ pub mod ts_milliseconds_option {
756761
/// # use chrono::naive::NaiveDateTime;
757762
/// # use serde_derive::Deserialize;
758763
/// use chrono::naive::serde::ts_milliseconds_option::deserialize as from_milli_tsopt;
759-
/// #[derive(Deserialize)]
764+
/// #[derive(Debug, PartialEq, Deserialize)]
760765
/// struct S {
761766
/// #[serde(deserialize_with = "from_milli_tsopt")]
762767
/// time: Option<NaiveDateTime>
763768
/// }
764769
///
765-
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918355 }"#)?;
770+
/// let my_s: S = serde_json::from_str(r#"{ "time": 1526522699918 }"#)?;
771+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1526522699, 918000000) });
766772
/// # Ok::<(), serde_json::Error>(())
767773
/// ```
768774
#[must_use]
@@ -881,13 +887,14 @@ pub mod ts_seconds {
881887
/// # use chrono::NaiveDateTime;
882888
/// # use serde_derive::Deserialize;
883889
/// use chrono::naive::serde::ts_seconds::deserialize as from_ts;
884-
/// #[derive(Deserialize)]
890+
/// #[derive(Debug, PartialEq, Deserialize)]
885891
/// struct S {
886892
/// #[serde(deserialize_with = "from_ts")]
887893
/// time: NaiveDateTime
888894
/// }
889895
///
890896
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
897+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1431684000, 0).unwrap() });
891898
/// # Ok::<(), serde_json::Error>(())
892899
/// ```
893900
#[must_use]
@@ -1003,13 +1010,14 @@ pub mod ts_seconds_option {
10031010
/// # use chrono::naive::NaiveDateTime;
10041011
/// # use serde_derive::Deserialize;
10051012
/// use chrono::naive::serde::ts_seconds_option::deserialize as from_tsopt;
1006-
/// #[derive(Deserialize)]
1013+
/// #[derive(Debug, PartialEq, Deserialize)]
10071014
/// struct S {
10081015
/// #[serde(deserialize_with = "from_tsopt")]
10091016
/// time: Option<NaiveDateTime>
10101017
/// }
10111018
///
10121019
/// let my_s: S = serde_json::from_str(r#"{ "time": 1431684000 }"#)?;
1020+
/// assert_eq!(my_s, S { time: NaiveDateTime::from_timestamp_opt(1431684000, 0) });
10131021
/// # Ok::<(), serde_json::Error>(())
10141022
/// ```
10151023
#[must_use]

0 commit comments

Comments
 (0)