@@ -24,7 +24,10 @@ pub struct Frontmatter {
24
24
pub tags : Option < Vec < liquid_core:: model:: KString > > ,
25
25
#[ serde( skip_serializing_if = "Option::is_none" ) ]
26
26
pub excerpt_separator : Option < liquid_core:: model:: KString > ,
27
- #[ serde( skip_serializing_if = "Option::is_none" ) ]
27
+ #[ serde(
28
+ skip_serializing_if = "Option::is_none" ,
29
+ deserialize_with = "deserialize_date_time"
30
+ ) ]
28
31
pub published_date : Option < DateTime > ,
29
32
#[ serde( skip_serializing_if = "Option::is_none" ) ]
30
33
pub format : Option < SourceFormat > ,
@@ -46,6 +49,32 @@ pub struct Frontmatter {
46
49
pub collection : Option < liquid_core:: model:: KString > ,
47
50
}
48
51
52
+ fn deserialize_date_time < ' de , D > ( deserializer : D ) -> Result < Option < DateTime > , D :: Error >
53
+ where
54
+ D : serde:: Deserializer < ' de > ,
55
+ {
56
+ let s: std:: borrow:: Cow < ' _ , str > = serde:: Deserialize :: deserialize ( deserializer) ?;
57
+ if s == "now" || s == "today" {
58
+ // The date time parsing support now and today not needed in this context
59
+ Err ( serde:: de:: Error :: custom ( format ! (
60
+ "value '{}' not a valid date time format" ,
61
+ s
62
+ ) ) )
63
+ } else {
64
+ let parsed = DateTime :: from_str ( & s) ;
65
+ if parsed. is_some ( ) {
66
+ Ok ( parsed)
67
+ } else if !s. trim ( ) . is_empty ( ) {
68
+ Err ( serde:: de:: Error :: custom ( format ! (
69
+ "value '{}' not a valid date time format" ,
70
+ s
71
+ ) ) )
72
+ } else {
73
+ Ok ( None )
74
+ }
75
+ }
76
+ }
77
+
49
78
impl Frontmatter {
50
79
pub fn empty ( ) -> Self {
51
80
Self :: default ( )
0 commit comments