Skip to content
This repository has been archived by the owner on Oct 10, 2019. It is now read-only.

Commit

Permalink
Update to new attribute whitelisting setup
Browse files Browse the repository at this point in the history
  • Loading branch information
sfackler committed Nov 15, 2016
1 parent c874cd4 commit 50d93bd
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 29 deletions.
2 changes: 0 additions & 2 deletions postgres-derive/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,9 +14,7 @@ test = false

[dependencies]
postgres-derive-internals = { version = "0.2.0", path = "../postgres-derive-internals" }
post-expansion = "0.2"
syn = "0.10"
quote = "0.3"

[dev-dependencies]
postgres = "0.12"
33 changes: 6 additions & 27 deletions postgres-derive/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,45 +2,24 @@

extern crate proc_macro;
extern crate postgres_derive_internals;
#[macro_use]
extern crate post_expansion;

extern crate syn;
extern crate quote;

use proc_macro::TokenStream;
use quote::{ToTokens, Tokens};

register_post_expansion!(PostExpansion_postgres_derive);

#[proc_macro_derive(ToSql)]
#[proc_macro_derive(ToSql, attributes(postgres))]
pub fn derive_tosql(input: TokenStream) -> TokenStream {
derive(input, postgres_derive_internals::expand_derive_tosql)
}

#[proc_macro_derive(FromSql)]
#[proc_macro_derive(FromSql, attributes(postgres))]
pub fn derive_fromsql(input: TokenStream) -> TokenStream {
derive(input, postgres_derive_internals::expand_derive_fromsql)
}

fn derive(input: TokenStream, expand: fn(&str) -> Result<String, String>) -> TokenStream {
let source = input.to_string();

let decl = expand_decl(&source);

let impl_ = match expand(&source) {
Ok(impl_) => impl_,
match expand(&input.to_string()) {
Ok(impl_) => impl_.parse().unwrap(),
Err(e) => panic!("{}", e),
};

let expanded = format!("{}\n{}", decl, impl_);
expanded.parse().unwrap()
}

fn expand_decl(source: &str) -> String {
let ast = syn::parse_macro_input(source).unwrap();
let stripped = post_expansion::strip_attrs_later(ast, &["postgres"], "postgres_derive");

let mut tokens = Tokens::new();
stripped.to_tokens(&mut tokens);
tokens.to_string()
}
}

0 comments on commit 50d93bd

Please sign in to comment.