Skip to content

Commit 02b226e

Browse files
authored
refactor: Workspace setup with serde_more_derive (#4)
1 parent 2cdf5a9 commit 02b226e

File tree

8 files changed

+482
-432
lines changed

8 files changed

+482
-432
lines changed

Cargo.toml

Lines changed: 8 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,26 +1,20 @@
1-
[package]
2-
name = "serde_more"
1+
[workspace]
2+
members = ["serde_more", "serde_more_derive"]
3+
resolver = "2"
4+
5+
[workspace.package]
36
version = "0.1.2"
47
authors = ["Jagoda Estera Ślązak <jslazak@jslazak.com>"]
5-
description = "Procedural macro to add arbitrary data when serializing using serde"
6-
categories = ["encoding"]
7-
keywords = ["serde", "serialization"]
88
license = "MIT OR Apache-2.0"
99
edition = "2024"
1010
repository = "https://github.com/j-g00da/serde-more"
11-
readme = "README.md"
12-
13-
[lib]
14-
proc-macro = true
1511

16-
[dependencies]
12+
[workspace.dependencies]
13+
serde = "1"
1714
quote = "1"
1815
proc-macro2 = "1"
1916
syn = "2"
20-
21-
[dev-dependencies]
22-
serde = { version = "1", features = ["derive"] }
2317
serde_json = "1"
2418
testresult = "0.4"
2519
rstest = "0.26"
26-
serde_with = { version = "3", features = ["macros", "hex"] }
20+
serde_with = "3"
File renamed without changes.

serde_more/Cargo.toml

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
[package]
2+
name = "serde_more"
3+
description = "Procedural macro to add arbitrary data when serializing using serde"
4+
categories = ["encoding", "no-std", "no-std::no-alloc"]
5+
keywords = ["serde", "serialization"]
6+
readme = "../README.md"
7+
version.workspace = true
8+
authors.workspace = true
9+
license.workspace = true
10+
edition.workspace = true
11+
repository.workspace = true
12+
13+
[dependencies]
14+
serde.workspace = true
15+
serde_more_derive = { path = "../serde_more_derive", version = "0.1.2" }
16+
17+
[dev-dependencies]
18+
serde = { workspace = true, features = ["derive"] }
19+
serde_json.workspace = true
20+
testresult.workspace = true
21+
rstest.workspace = true
22+
serde_with = { workspace = true, features = ["macros", "hex"] }

serde_more/src/lib.rs

Lines changed: 192 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,192 @@
1+
#![doc = include_str!("../../README.md")]
2+
#![no_std]
3+
4+
pub use serde_more_derive::SerializeMore;
5+
6+
use serde::ser::{SerializeMap, Serializer};
7+
8+
/// A custom serializer that flattens struct fields into a parent map.
9+
///
10+
/// This is NOT a part of the public API and is only intended to be used
11+
/// in the code generated by [`SerializeMore`] derive macro.
12+
#[doc(hidden)]
13+
pub struct FlatMapSerializer<'a, M: SerializeMap> {
14+
pub map: &'a mut M,
15+
}
16+
17+
impl<'a, M: SerializeMap> Serializer for FlatMapSerializer<'a, M> {
18+
type Ok = ();
19+
type Error = M::Error;
20+
21+
type SerializeSeq = serde::ser::Impossible<(), M::Error>;
22+
type SerializeTuple = serde::ser::Impossible<(), M::Error>;
23+
type SerializeTupleStruct = serde::ser::Impossible<(), M::Error>;
24+
type SerializeTupleVariant = serde::ser::Impossible<(), M::Error>;
25+
type SerializeMap = serde::ser::Impossible<(), M::Error>;
26+
type SerializeStruct = Self;
27+
type SerializeStructVariant = serde::ser::Impossible<(), M::Error>;
28+
29+
fn serialize_bool(self, _v: bool) -> Result<(), M::Error> {
30+
Err(serde::ser::Error::custom("expected struct"))
31+
}
32+
33+
fn serialize_i8(self, _v: i8) -> Result<(), M::Error> {
34+
Err(serde::ser::Error::custom("expected struct"))
35+
}
36+
37+
fn serialize_i16(self, _v: i16) -> Result<(), M::Error> {
38+
Err(serde::ser::Error::custom("expected struct"))
39+
}
40+
41+
fn serialize_i32(self, _v: i32) -> Result<(), M::Error> {
42+
Err(serde::ser::Error::custom("expected struct"))
43+
}
44+
45+
fn serialize_i64(self, _v: i64) -> Result<(), M::Error> {
46+
Err(serde::ser::Error::custom("expected struct"))
47+
}
48+
49+
fn serialize_u8(self, _v: u8) -> Result<(), M::Error> {
50+
Err(serde::ser::Error::custom("expected struct"))
51+
}
52+
53+
fn serialize_u16(self, _v: u16) -> Result<(), M::Error> {
54+
Err(serde::ser::Error::custom("expected struct"))
55+
}
56+
57+
fn serialize_u32(self, _v: u32) -> Result<(), M::Error> {
58+
Err(serde::ser::Error::custom("expected struct"))
59+
}
60+
61+
fn serialize_u64(self, _v: u64) -> Result<(), M::Error> {
62+
Err(serde::ser::Error::custom("expected struct"))
63+
}
64+
65+
fn serialize_f32(self, _v: f32) -> Result<(), M::Error> {
66+
Err(serde::ser::Error::custom("expected struct"))
67+
}
68+
69+
fn serialize_f64(self, _v: f64) -> Result<(), M::Error> {
70+
Err(serde::ser::Error::custom("expected struct"))
71+
}
72+
73+
fn serialize_char(self, _v: char) -> Result<(), M::Error> {
74+
Err(serde::ser::Error::custom("expected struct"))
75+
}
76+
77+
fn serialize_str(self, _v: &str) -> Result<(), M::Error> {
78+
Err(serde::ser::Error::custom("expected struct"))
79+
}
80+
81+
fn serialize_bytes(self, _v: &[u8]) -> Result<(), M::Error> {
82+
Err(serde::ser::Error::custom("expected struct"))
83+
}
84+
85+
fn serialize_none(self) -> Result<(), M::Error> {
86+
Err(serde::ser::Error::custom("expected struct"))
87+
}
88+
89+
fn serialize_some<T: ?Sized + serde::Serialize>(self, _value: &T) -> Result<(), M::Error> {
90+
Err(serde::ser::Error::custom("expected struct"))
91+
}
92+
93+
fn serialize_unit(self) -> Result<(), M::Error> {
94+
Err(serde::ser::Error::custom("expected struct"))
95+
}
96+
97+
fn serialize_unit_struct(self, _name: &'static str) -> Result<(), M::Error> {
98+
Err(serde::ser::Error::custom("expected struct"))
99+
}
100+
101+
fn serialize_unit_variant(
102+
self,
103+
_name: &'static str,
104+
_variant_index: u32,
105+
_variant: &'static str,
106+
) -> Result<(), M::Error> {
107+
Err(serde::ser::Error::custom("expected struct"))
108+
}
109+
110+
fn serialize_newtype_struct<T: ?Sized + serde::Serialize>(
111+
self,
112+
_name: &'static str,
113+
_value: &T,
114+
) -> Result<(), M::Error> {
115+
Err(serde::ser::Error::custom("expected struct"))
116+
}
117+
118+
fn serialize_newtype_variant<T: ?Sized + serde::Serialize>(
119+
self,
120+
_name: &'static str,
121+
_variant_index: u32,
122+
_variant: &'static str,
123+
_value: &T,
124+
) -> Result<(), M::Error> {
125+
Err(serde::ser::Error::custom("expected struct"))
126+
}
127+
128+
fn serialize_seq(self, _len: Option<usize>) -> Result<Self::SerializeSeq, M::Error> {
129+
Err(serde::ser::Error::custom("expected struct"))
130+
}
131+
132+
fn serialize_tuple(self, _len: usize) -> Result<Self::SerializeTuple, M::Error> {
133+
Err(serde::ser::Error::custom("expected struct"))
134+
}
135+
136+
fn serialize_tuple_struct(
137+
self,
138+
_name: &'static str,
139+
_len: usize,
140+
) -> Result<Self::SerializeTupleStruct, M::Error> {
141+
Err(serde::ser::Error::custom("expected struct"))
142+
}
143+
144+
fn serialize_tuple_variant(
145+
self,
146+
_name: &'static str,
147+
_variant_index: u32,
148+
_variant: &'static str,
149+
_len: usize,
150+
) -> Result<Self::SerializeTupleVariant, M::Error> {
151+
Err(serde::ser::Error::custom("expected struct"))
152+
}
153+
154+
fn serialize_map(self, _len: Option<usize>) -> Result<Self::SerializeMap, M::Error> {
155+
Err(serde::ser::Error::custom("expected struct"))
156+
}
157+
158+
fn serialize_struct(
159+
self,
160+
_name: &'static str,
161+
_len: usize,
162+
) -> Result<Self::SerializeStruct, M::Error> {
163+
Ok(self)
164+
}
165+
166+
fn serialize_struct_variant(
167+
self,
168+
_name: &'static str,
169+
_variant_index: u32,
170+
_variant: &'static str,
171+
_len: usize,
172+
) -> Result<Self::SerializeStructVariant, M::Error> {
173+
Err(serde::ser::Error::custom("expected struct"))
174+
}
175+
}
176+
177+
impl<'a, M: SerializeMap> serde::ser::SerializeStruct for FlatMapSerializer<'a, M> {
178+
type Ok = ();
179+
type Error = M::Error;
180+
181+
fn serialize_field<T: ?Sized + serde::Serialize>(
182+
&mut self,
183+
key: &'static str,
184+
value: &T,
185+
) -> Result<(), M::Error> {
186+
self.map.serialize_entry(key, value)
187+
}
188+
189+
fn end(self) -> Result<(), M::Error> {
190+
Ok(())
191+
}
192+
}

serde_more_derive/Cargo.toml

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
[package]
2+
name = "serde_more_derive"
3+
description = "Derive macro for serde_more"
4+
categories = ["encoding"]
5+
keywords = ["serde", "serialization"]
6+
version.workspace = true
7+
authors.workspace = true
8+
license.workspace = true
9+
edition.workspace = true
10+
repository.workspace = true
11+
12+
[lib]
13+
proc-macro = true
14+
15+
[dependencies]
16+
quote.workspace = true
17+
proc-macro2.workspace = true
18+
syn.workspace = true
19+
20+
[dev-dependencies]
21+
serde_more = { path = "../serde_more" }
22+
serde = { workspace = true, features = ["derive"] }
23+
serde_json = "1"

0 commit comments

Comments
 (0)