Skip to content

Commit

Permalink
change
Browse files Browse the repository at this point in the history
  • Loading branch information
viktorlott committed Jan 11, 2023
1 parent 4a9f2d7 commit 1868b00
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 9 deletions.
10 changes: 4 additions & 6 deletions src/builder.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand All @@ -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<TypeDecl> = Vec::new();

Expand All @@ -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());

Expand Down
12 changes: 9 additions & 3 deletions src/tools.rs
Original file line number Diff line number Diff line change
@@ -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> {
Expand All @@ -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
}
Expand Down Expand Up @@ -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()
Expand All @@ -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);
}



0 comments on commit 1868b00

Please sign in to comment.