musli_json/en/
object_key_encoder.rs1use core::fmt;
2
3use musli::en::{Encode, Encoder};
4use musli::Context;
5use musli_utils::Writer;
6
7pub(crate) struct JsonObjectKeyEncoder<'a, W, C: ?Sized> {
8 cx: &'a C,
9 writer: W,
10}
11
12impl<'a, W, C: ?Sized> JsonObjectKeyEncoder<'a, W, C> {
13 #[inline]
14 pub(super) fn new(cx: &'a C, writer: W) -> Self {
15 Self { cx, writer }
16 }
17}
18
19macro_rules! format_integer {
20 ($slf:ident, $value:ident) => {{
21 $slf.writer.write_byte($slf.cx, b'"')?;
22 let mut buffer = itoa::Buffer::new();
23 $slf.writer
24 .write_bytes($slf.cx, buffer.format($value).as_bytes())?;
25 $slf.writer.write_byte($slf.cx, b'"')?;
26 Ok(())
27 }};
28}
29
30#[musli::encoder]
31impl<'a, W, C> Encoder for JsonObjectKeyEncoder<'a, W, C>
32where
33 W: Writer,
34 C: ?Sized + Context,
35{
36 type Cx = C;
37 type Error = C::Error;
38 type Ok = ();
39 type Mode = C::Mode;
40 type WithContext<'this, U> = JsonObjectKeyEncoder<'this, W, U> where U: 'this + Context;
41
42 #[inline]
43 fn cx(&self) -> &Self::Cx {
44 self.cx
45 }
46
47 #[inline]
48 fn with_context<U>(self, cx: &U) -> Result<Self::WithContext<'_, U>, C::Error>
49 where
50 U: Context,
51 {
52 Ok(JsonObjectKeyEncoder::new(cx, self.writer))
53 }
54
55 #[inline]
56 fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
57 write!(f, "any type that can be used as an object key")
58 }
59
60 #[inline]
61 fn encode<T>(self, value: T) -> Result<Self::Ok, Self::Error>
62 where
63 T: Encode<Self::Mode>,
64 {
65 value.encode(self.cx, self)
66 }
67
68 #[inline]
69 fn encode_u8(mut self, value: u8) -> Result<Self::Ok, C::Error> {
70 format_integer!(self, value)
71 }
72
73 #[inline]
74 fn encode_u16(mut self, value: u16) -> Result<Self::Ok, C::Error> {
75 format_integer!(self, value)
76 }
77
78 #[inline]
79 fn encode_u32(mut self, value: u32) -> Result<Self::Ok, C::Error> {
80 format_integer!(self, value)
81 }
82
83 #[inline]
84 fn encode_u64(mut self, value: u64) -> Result<Self::Ok, C::Error> {
85 format_integer!(self, value)
86 }
87
88 #[inline]
89 fn encode_u128(mut self, value: u128) -> Result<Self::Ok, C::Error> {
90 format_integer!(self, value)
91 }
92
93 #[inline]
94 fn encode_i8(mut self, value: i8) -> Result<Self::Ok, C::Error> {
95 format_integer!(self, value)
96 }
97
98 #[inline]
99 fn encode_i16(mut self, value: i16) -> Result<Self::Ok, C::Error> {
100 format_integer!(self, value)
101 }
102
103 #[inline]
104 fn encode_i32(mut self, value: i32) -> Result<Self::Ok, C::Error> {
105 format_integer!(self, value)
106 }
107
108 #[inline]
109 fn encode_i64(mut self, value: i64) -> Result<Self::Ok, C::Error> {
110 format_integer!(self, value)
111 }
112
113 #[inline]
114 fn encode_i128(mut self, value: i128) -> Result<Self::Ok, C::Error> {
115 format_integer!(self, value)
116 }
117
118 #[inline]
119 fn encode_usize(mut self, value: usize) -> Result<Self::Ok, C::Error> {
120 format_integer!(self, value)
121 }
122
123 #[inline]
124 fn encode_isize(mut self, value: isize) -> Result<Self::Ok, C::Error> {
125 format_integer!(self, value)
126 }
127
128 #[inline]
129 fn encode_string(self, string: &str) -> Result<Self::Ok, C::Error> {
130 super::encode_string(self.cx, self.writer, string.as_bytes())
131 }
132
133 #[inline]
134 fn collect_string<T>(self, value: &T) -> Result<Self::Ok, <Self::Cx as Context>::Error>
135 where
136 T: ?Sized + fmt::Display,
137 {
138 let buf = self.cx.collect_string(value)?;
139 self.encode_string(buf.as_ref())
140 }
141}