Macro time::serde::format_description
source · format_description!() { /* proc-macro */ }
Expand description
Generate a custom serializer and deserializer from the provided string.
The syntax accepted by this macro is the same as format_description::parse()
, which can
be found in the book.
§Usage
Invoked as serde::format_description!(mod_name, Date, "<format string>")
. This puts a
module named mod_name
in the current scope that can be used to format Date
structs. A
submodule (mod_name::option
) is also generated for Option<Date>
. Both modules are only
visible in the current scope.
The returned Option
will contain a deserialized value if present and None
if the field
is present but the value is null
(or the equivalent in other formats). To return None
when the field is not present, you should use #[serde(default)]
on the field.
§Examples
use ::serde::{Serialize, Deserialize};
use time::serde;
// Makes a module `mod my_format { ... }`.
serde::format_description!(my_format, OffsetDateTime, "hour=[hour], minute=[minute]");
#[derive(Serialize, Deserialize)]
struct SerializesWithCustom {
#[serde(with = "my_format")]
dt: OffsetDateTime,
#[serde(with = "my_format::option")]
maybe_dt: Option<OffsetDateTime>,
}