logo
format_description!() { /* proc-macro */ }
This is supported on crate features macros and serde-human-readable and serde only.
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.

Examples

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>,
}
Run