From 1868b00e65a11ee106099dac8d9e40ef3a63c032 Mon Sep 17 00:00:00 2001 From: Viktor Lott Date: Wed, 11 Jan 2023 22:05:06 +0100 Subject: [PATCH] change --- src/builder.rs | 10 ++++------ src/tools.rs | 12 +++++++++--- 2 files changed, 13 insertions(+), 9 deletions(-) diff --git a/src/builder.rs b/src/builder.rs index a8c6b9c..976a7f3 100644 --- a/src/builder.rs +++ b/src/builder.rs @@ -1,7 +1,7 @@ use proc_macro::TokenStream; use quote::{quote}; -use syn::{parse_quote, parse, Data, DeriveInput}; -use tools::{modify, format_str, doc_struct, TypeDecl}; +use syn::{parse, Data, DeriveInput}; +use tools::{modify, format_code, doc_struct, TypeDecl, publicify}; #[path = "tools.rs"] mod tools; @@ -12,15 +12,14 @@ pub fn codegen(_attr: TokenStream, item: TokenStream) -> TokenStream { let mut struct_entry = ast.clone(); - struct_entry.ident = parse_quote!(ty); - struct_entry.vis = parse_quote!(pub); + publicify(&mut struct_entry); let Data::Struct(ref mut data_struct) = struct_entry.data else { panic!("Cannot destruct Struct"); }; let parent = ast.ident.to_string(); - let original = format_str(item.to_string()); + let original = format_code(item.to_string()); let mut ty_decls: Vec = Vec::new(); @@ -29,7 +28,6 @@ pub fn codegen(_attr: TokenStream, item: TokenStream) -> TokenStream { ty_decls.push(TypeDecl::new(original.as_str(), ident, &field.ty)); } - let name = ast.ident; let docs = doc_struct(parent.as_str(), original.as_str()); diff --git a/src/tools.rs b/src/tools.rs index 0dabc19..f9629a5 100644 --- a/src/tools.rs +++ b/src/tools.rs @@ -1,7 +1,7 @@ use ::rustfmt::{format_input, Input}; use proc_macro2::TokenStream as TokenStream2; use quote::{quote, ToTokens, TokenStreamExt}; -use syn::{Ident, Type, Field, parse_quote}; +use syn::{Ident, Type, Field, parse_quote, DeriveInput}; use std::{io::Sink, marker::PhantomData}; pub struct TypeDecl<'a> { @@ -15,7 +15,7 @@ impl<'a> TypeDecl<'a> { pub fn new(original: &'a str, ident: Ident, ty: &'a Type) -> Self { Self { original, - ident: ident.clone(), + ident, ty: ty.clone(), _marker: PhantomData } @@ -65,7 +65,9 @@ pub fn doc_struct(name: &str, original: &str) -> String { ) } -pub fn format_str<'a>(orig: String) -> String { + + +pub fn format_code(orig: String) -> String { format_input(Input::Text(orig), &<_>::default(), None::<&mut Sink>) .map(|res| res.1.into_iter().next()) .ok() @@ -92,6 +94,10 @@ pub fn modify(field: &mut Field, parent: &str, i: usize) -> Ident { ident } +pub fn publicify(ast: &mut DeriveInput) { + ast.ident = parse_quote!(ty); + ast.vis = parse_quote!(pub); +}