serde_with/
formats.rs

1//! Specify the format and how lenient the deserialization is
2
3#[allow(unused_imports)]
4use crate::prelude::*;
5
6/// Specify how to serialize/deserialize a type
7///
8/// The format specifier allows to configure how a value is serialized/deserialized.
9/// For example, you can serialize a timestamp as an integer using the UNIX epoch, as a string containing an integer, or as a string using ISO 8601.
10/// This [`Format`] traits allows more flexibility in configuring the format without the need to create a new type for each case.
11pub trait Format {}
12
13macro_rules! impl_format {
14    ($(#[$attr:meta] $t:ty)*) => {
15        $(
16            #[$attr]
17            impl Format for $t {}
18        )*
19    };
20}
21macro_rules! create_format {
22    ($(#[$attr:meta] $t:ident)*) => {
23        $(
24            #[$attr]
25                        pub struct $t;
26            impl_format!(#[$attr] $t);
27        )*
28    };
29}
30impl_format!(
31    /// Serialize into an i8
32    i8
33    /// Serialize into a u8
34    u8
35    /// Serialize into an i16
36    i16
37    /// Serialize into a u16
38    u16
39    /// Serialize into an i32
40    i32
41    /// Serialize into a u32
42    u32
43    /// Serialize into an i64
44    i64
45    /// Serialize into a u64
46    u64
47    /// Serialize into an i128
48    i128
49    /// Serialize into a u128
50    u128
51
52    /// Serialize into a f32
53    f32
54    /// Serialize into a f64
55    f64
56
57    /// Serialize into a bool
58    bool
59);
60#[cfg(feature = "alloc")]
61impl_format!(
62    /// Serialize into a String
63    String
64);
65
66create_format!(
67    /// Use uppercase characters
68    Uppercase
69    /// Use lowercase characters
70    Lowercase
71
72    /// Use in combination with [`OneOrMany`](crate::OneOrMany). Emit single element for lists of size 1.
73    PreferOne
74    /// Use in combination with [`OneOrMany`](crate::OneOrMany). Always emit the list form.
75    PreferMany
76
77    /// Emit padding during serialization.
78    Padded
79    /// Do not emit padding during serialization.
80    Unpadded
81);
82
83/// Specify how lenient the deserialization process should be
84///
85/// Formats which make use of this trait should specify how it affects the deserialization behavior.
86pub trait Strictness {}
87
88/// Use strict deserialization behavior, see [`Strictness`].
89pub struct Strict;
90impl Strictness for Strict {}
91
92/// Use a flexible deserialization behavior, see [`Strictness`].
93pub struct Flexible;
94impl Strictness for Flexible {}
95
96/// Separator for string-based collection de/serialization
97pub trait Separator {
98    /// Return the string delimiting two elements in the string-based collection
99    fn separator() -> &'static str;
100}
101
102/// Predefined separator using a single space
103pub struct SpaceSeparator;
104
105impl Separator for SpaceSeparator {
106    #[inline]
107    fn separator() -> &'static str {
108        " "
109    }
110}
111
112/// Predefined separator using a single comma
113pub struct CommaSeparator;
114
115impl Separator for CommaSeparator {
116    #[inline]
117    fn separator() -> &'static str {
118        ","
119    }
120}
121
122/// Predefined separator using a single semicolon
123pub struct SemicolonSeparator;
124
125impl Separator for SemicolonSeparator {
126    #[inline]
127    fn separator() -> &'static str {
128        ";"
129    }
130}
131
132/// Predefined separator using a single semicolon
133pub struct ColonSeparator;
134
135impl Separator for ColonSeparator {
136    #[inline]
137    fn separator() -> &'static str {
138        ":"
139    }
140}
141
142/// Predefined separator using a single linefeed.
143pub struct UnixLineSeparator;
144
145impl Separator for UnixLineSeparator {
146    #[inline]
147    fn separator() -> &'static str {
148        "\n"
149    }
150}
151
152/// Predefined separator using a DOS/Windows line ending.
153pub struct DosLineSeparator;
154
155impl Separator for DosLineSeparator {
156    #[inline]
157    fn separator() -> &'static str {
158        "\r\n"
159    }
160}