aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
-rw-r--r--Cargo.toml5
-rw-r--r--src/lib.rs27
2 files changed, 21 insertions, 11 deletions
diff --git a/Cargo.toml b/Cargo.toml
index d34787f..e56cb25 100644
--- a/Cargo.toml
+++ b/Cargo.toml
@@ -5,4 +5,9 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+[lib]
+proc-macro = true
+
[dependencies]
+quote = "1.0"
+syn = { version = "2.0.16", features = ["full"] }
diff --git a/src/lib.rs b/src/lib.rs
index 7d12d9a..93eca2a 100644
--- a/src/lib.rs
+++ b/src/lib.rs
@@ -1,14 +1,19 @@
-pub fn add(left: usize, right: usize) -> usize {
- left + right
-}
+use proc_macro::TokenStream;
+use quote::quote;
+use syn::{parse, ItemStruct};
-#[cfg(test)]
-mod tests {
- use super::*;
+#[proc_macro_derive(Serialize)]
+pub fn derive_serialize(item: TokenStream) -> TokenStream {
+ let ast: ItemStruct = parse(item).unwrap();
+ let name = ast.ident;
- #[test]
- fn it_works() {
- let result = add(2, 2);
- assert_eq!(result, 4);
- }
+ quote!(
+ impl Serialize for #name {
+ fn serialize<W: Write>(&self, w: W) -> io::Result<()> {
+ w.write_all(&[42])?;
+ Ok(())
+ }
+ }
+ )
+ .into()
}