| 1 |
use crate::lib::*; |
| 2 |
use crate::ser::{Error, Impossible, Serialize, Serializer}; |
| 3 |
|
| 4 |
impl Error for fmt::Error { |
| 5 |
fn custom<T: Display>(_msg: T) -> Self { |
| 6 |
fmt::Error |
| 7 |
} |
| 8 |
} |
| 9 |
|
| 10 |
macro_rules! fmt_primitives { |
| 11 |
($($f:ident: $t:ty,)*) => { |
| 12 |
$( |
| 13 |
fn $f(self, v: $t) -> fmt::Result { |
| 14 |
Display::fmt(&v, self) |
| 15 |
} |
| 16 |
)* |
| 17 |
}; |
| 18 |
} |
| 19 |
|
| 20 |
/// ```edition2021 |
| 21 |
/// use serde::ser::Serialize; |
| 22 |
/// use serde_derive::Serialize; |
| 23 |
/// use std::fmt::{self, Display}; |
| 24 |
/// |
| 25 |
/// #[derive(Serialize)] |
| 26 |
/// #[serde(rename_all = "kebab-case")] |
| 27 |
/// pub enum MessageType { |
| 28 |
/// StartRequest, |
| 29 |
/// EndRequest, |
| 30 |
/// } |
| 31 |
/// |
| 32 |
/// impl Display for MessageType { |
| 33 |
/// fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
| 34 |
/// self.serialize(f) |
| 35 |
/// } |
| 36 |
/// } |
| 37 |
/// ``` |
| 38 |
impl<'a, 'b> Serializer for &'a mut fmt::Formatter<'b> { |
| 39 |
type Ok = (); |
| 40 |
type Error = fmt::Error; |
| 41 |
type SerializeSeq = Impossible<(), fmt::Error>; |
| 42 |
type SerializeTuple = Impossible<(), fmt::Error>; |
| 43 |
type SerializeTupleStruct = Impossible<(), fmt::Error>; |
| 44 |
type SerializeTupleVariant = Impossible<(), fmt::Error>; |
| 45 |
type SerializeMap = Impossible<(), fmt::Error>; |
| 46 |
type SerializeStruct = Impossible<(), fmt::Error>; |
| 47 |
type SerializeStructVariant = Impossible<(), fmt::Error>; |
| 48 |
|
| 49 |
fmt_primitives! { |
| 50 |
serialize_bool: bool, |
| 51 |
serialize_i8: i8, |
| 52 |
serialize_i16: i16, |
| 53 |
serialize_i32: i32, |
| 54 |
serialize_i64: i64, |
| 55 |
serialize_i128: i128, |
| 56 |
serialize_u8: u8, |
| 57 |
serialize_u16: u16, |
| 58 |
serialize_u32: u32, |
| 59 |
serialize_u64: u64, |
| 60 |
serialize_u128: u128, |
| 61 |
serialize_f32: f32, |
| 62 |
serialize_f64: f64, |
| 63 |
serialize_char: char, |
| 64 |
serialize_str: &str, |
| 65 |
serialize_unit_struct: &'static str, |
| 66 |
} |
| 67 |
|
| 68 |
fn serialize_unit_variant( |
| 69 |
self, |
| 70 |
_name: &'static str, |
| 71 |
_variant_index: u32, |
| 72 |
variant: &'static str, |
| 73 |
) -> fmt::Result { |
| 74 |
Display::fmt(variant, self) |
| 75 |
} |
| 76 |
|
| 77 |
fn serialize_newtype_struct<T>(self, _name: &'static str, value: &T) -> fmt::Result |
| 78 |
where |
| 79 |
T: ?Sized + Serialize, |
| 80 |
{ |
| 81 |
Serialize::serialize(value, self) |
| 82 |
} |
| 83 |
|
| 84 |
fn serialize_bytes(self, _v: &[u8]) -> fmt::Result { |
| 85 |
Err(fmt::Error) |
| 86 |
} |
| 87 |
|
| 88 |
fn serialize_none(self) -> fmt::Result { |
| 89 |
Err(fmt::Error) |
| 90 |
} |
| 91 |
|
| 92 |
fn serialize_some<T>(self, _value: &T) -> fmt::Result |
| 93 |
where |
| 94 |
T: ?Sized + Serialize, |
| 95 |
{ |
| 96 |
Err(fmt::Error) |
| 97 |
} |
| 98 |
|
| 99 |
fn serialize_unit(self) -> fmt::Result { |
| 100 |
Err(fmt::Error) |
| 101 |
} |
| 102 |
|
| 103 |
fn serialize_newtype_variant<T>( |
| 104 |
self, |
| 105 |
_name: &'static str, |
| 106 |
_variant_index: u32, |
| 107 |
_variant: &'static str, |
| 108 |
_value: &T, |
| 109 |
) -> fmt::Result |
| 110 |
where |
| 111 |
T: ?Sized + Serialize, |
| 112 |
{ |
| 113 |
Err(fmt::Error) |
| 114 |
} |
| 115 |
|
| 116 |
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, fmt::Error> { |
| 117 |
Err(fmt::Error) |
| 118 |
} |
| 119 |
|
| 120 |
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, fmt::Error> { |
| 121 |
Err(fmt::Error) |
| 122 |
} |
| 123 |
|
| 124 |
fn serialize_tuple_struct( |
| 125 |
self, |
| 126 |
_name: &'static str, |
| 127 |
_len: usize, |
| 128 |
) -> Result<Self::SerializeTupleStruct, fmt::Error> { |
| 129 |
Err(fmt::Error) |
| 130 |
} |
| 131 |
|
| 132 |
fn serialize_tuple_variant( |
| 133 |
self, |
| 134 |
_name: &'static str, |
| 135 |
_variant_index: u32, |
| 136 |
_variant: &'static str, |
| 137 |
_len: usize, |
| 138 |
) -> Result<Self::SerializeTupleVariant, fmt::Error> { |
| 139 |
Err(fmt::Error) |
| 140 |
} |
| 141 |
|
| 142 |
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, fmt::Error> { |
| 143 |
Err(fmt::Error) |
| 144 |
} |
| 145 |
|
| 146 |
fn serialize_struct( |
| 147 |
self, |
| 148 |
_name: &'static str, |
| 149 |
_len: usize, |
| 150 |
) -> Result<Self::SerializeStruct, fmt::Error> { |
| 151 |
Err(fmt::Error) |
| 152 |
} |
| 153 |
|
| 154 |
fn serialize_struct_variant( |
| 155 |
self, |
| 156 |
_name: &'static str, |
| 157 |
_variant_index: u32, |
| 158 |
_variant: &'static str, |
| 159 |
_len: usize, |
| 160 |
) -> Result<Self::SerializeStructVariant, fmt::Error> { |
| 161 |
Err(fmt::Error) |
| 162 |
} |
| 163 |
|
| 164 |
fn collect_str<T>(self, value: &T) -> fmt::Result |
| 165 |
where |
| 166 |
T: ?Sized + Display, |
| 167 |
{ |
| 168 |
Display::fmt(value, self) |
| 169 |
} |
| 170 |
} |