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

Commit 25c509a

Browse files
committed
Update codegen to use syn backend
1 parent 7bc9085 commit 25c509a

File tree

6 files changed

+79
-73
lines changed

6 files changed

+79
-73
lines changed

postgres-derive-codegen/Cargo.toml

Lines changed: 3 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,9 @@ description = "Deriving codegen support for Postgres enum, domain, and composite
77
repository = "https://github.com/sfackler/rust-postgres-derive"
88
readme = "../README.md"
99
keywords = ["database", "postgres", "postgresql", "sql"]
10-
build = "build.rs"
1110
exclude = ["test"]
1211

13-
[features]
14-
default = ["with-syntex"]
15-
nightly = []
16-
with-syntex = ["syntex", "syntex_syntax"]
17-
18-
[build-dependencies]
19-
syntex = { version = "0.44", optional = true }
20-
2112
[dependencies]
22-
syntex = { version = "0.44", optional = true }
23-
syntex_syntax = { version = "0.44", optional = true }
13+
syntex = "0.44"
14+
syntex_syntax = "0.44"
15+
postgres-derive-internals = { path = "../postgres-derive-internals" }

postgres-derive-codegen/build.rs

Lines changed: 0 additions & 27 deletions
This file was deleted.

postgres-derive-codegen/src/lib.rs

Lines changed: 60 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,13 @@
1-
#![cfg_attr(not(feature = "with-syntex"), feature(rustc_private, quote))]
2-
3-
#[cfg(feature = "with-syntex")]
41
extern crate syntex;
5-
#[cfg(feature = "with-syntex")]
62
extern crate syntex_syntax as syntax;
7-
#[cfg(not(feature = "with-syntex"))]
8-
extern crate syntax;
9-
#[cfg(not(feature = "with-syntex"))]
10-
extern crate rustc_plugin;
11-
12-
#[cfg(feature = "with-syntex")]
13-
include!(concat!(env!("OUT_DIR"), "/lib.rs"));
3+
extern crate postgres_derive_internals;
144

15-
#[cfg(not(feature = "with-syntex"))]
16-
include!("lib.rs.in");
5+
use syntax::ext::base::{Annotatable, ExtCtxt};
6+
use syntax::codemap::Span;
7+
use syntax::ast::MetaItem;
8+
use syntax::print::pprust;
9+
use syntax::parse;
1710

18-
#[cfg(feature = "with-syntex")]
1911
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
2012
where S: AsRef<std::path::Path>,
2113
D: AsRef<std::path::Path>,
@@ -25,7 +17,6 @@ pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
2517
registry.expand("", src.as_ref(), dst.as_ref())
2618
}
2719

28-
#[cfg(feature = "with-syntex")]
2920
pub fn register(reg: &mut syntex::Registry) {
3021
use syntax::{ast, fold};
3122

@@ -53,22 +44,64 @@ pub fn register(reg: &mut syntex::Registry) {
5344
reg.add_attr("feature(custom_derive)");
5445
reg.add_attr("feature(custom_attribute)");
5546

56-
reg.add_decorator("derive_ToSql", tosql::expand);
57-
reg.add_decorator("derive_FromSql", fromsql::expand);
47+
reg.add_decorator("derive_ToSql", expand_tosql);
48+
reg.add_decorator("derive_FromSql", expand_fromsql);
5849

5950
reg.add_post_expansion_pass(strip_attributes);
6051
}
6152

62-
#[cfg(not(feature = "with-syntex"))]
63-
pub fn register(registry: &mut rustc_plugin::Registry) {
64-
use syntax::ext::base::SyntaxExtension;
65-
use syntax::feature_gate::AttributeType;
66-
use syntax::parse::token;
53+
fn expand_tosql(ctx: &mut ExtCtxt,
54+
span: Span,
55+
_: &MetaItem,
56+
annotatable: &Annotatable,
57+
push: &mut FnMut(Annotatable)) {
58+
expand_inner(ctx,
59+
span,
60+
"ToSql",
61+
annotatable,
62+
push,
63+
postgres_derive_internals::expand_derive_tosql);
64+
}
6765

68-
registry.register_syntax_extension(token::intern("derive_ToSql"),
69-
SyntaxExtension::MultiDecorator(Box::new(tosql::expand)));
70-
registry.register_syntax_extension(token::intern("derive_FromSql"),
71-
SyntaxExtension::MultiDecorator(Box::new(fromsql::expand)));
66+
fn expand_fromsql(ctx: &mut ExtCtxt,
67+
span: Span,
68+
_: &MetaItem,
69+
annotatable: &Annotatable,
70+
push: &mut FnMut(Annotatable)) {
71+
expand_inner(ctx,
72+
span,
73+
"FromSql",
74+
annotatable,
75+
push,
76+
postgres_derive_internals::expand_derive_fromsql);
77+
}
7278

73-
registry.register_attribute("postgres".to_owned(), AttributeType::Normal);
79+
fn expand_inner(ctx: &mut ExtCtxt,
80+
span: Span,
81+
trait_: &str,
82+
annotatable: &Annotatable,
83+
push: &mut FnMut(Annotatable),
84+
expand: fn(&str) -> Result<String, String>) {
85+
let item = match *annotatable {
86+
Annotatable::Item(ref item) => item,
87+
_ => {
88+
ctx.span_err(span, &format!("#[derive({})] can only be applied to structs, single field \
89+
tuple structs, and enums", trait_));
90+
return;
91+
}
92+
};
93+
94+
let item = pprust::item_to_string(item);
95+
match expand(&item) {
96+
Ok(source) => {
97+
let mut parser = parse::new_parser_from_source_str(&ctx.parse_sess,
98+
ctx.cfg.clone(),
99+
"<macro src>".to_owned(),
100+
source);
101+
while let Some(item) = parser.parse_item().unwrap() {
102+
push(Annotatable::Item(item));
103+
}
104+
}
105+
Err(err) => ctx.span_err(span, &err),
106+
}
74107
}

postgres-derive-codegen/test/Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,4 +8,4 @@ build = "build.rs"
88
postgres-derive-codegen = { path = ".." }
99

1010
[dependencies]
11-
postgres = "0.11.1"
11+
postgres = { git = "https://github.com/sfackler/rust-postgres" }

postgres-derive-codegen/test/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
#[macro_use]
22
extern crate postgres;
33

4-
use postgres::{Connection, SslMode};
4+
use postgres::{Connection, TlsMode};
55
use postgres::types::{FromSql, ToSql};
66
use std::fmt;
77

@@ -23,7 +23,7 @@ pub fn test_type<T, S>(conn: &Connection, sql_type: &str, checks: &[(T, S)])
2323

2424
#[test]
2525
fn domain() {
26-
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
26+
let conn = Connection::connect("postgres://postgres@localhost", TlsMode::None).unwrap();
2727
conn.execute("CREATE DOMAIN pg_temp.session_id AS bytea CHECK(octet_length(VALUE) = 16);", &[])
2828
.unwrap();
2929

@@ -33,7 +33,7 @@ fn domain() {
3333

3434
#[test]
3535
fn enum_() {
36-
let conn = Connection::connect("postgres://postgres@localhost", SslMode::None).unwrap();
36+
let conn = Connection::connect("postgres://postgres@localhost", TlsMode::None).unwrap();
3737
conn.execute("CREATE TYPE pg_temp.mood AS ENUM ('sad', 'ok', 'happy')", &[]).unwrap();
3838

3939
test_type(&conn,

postgres-derive-internals/src/lib.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,6 @@ use syn::{MacroInput, MetaItem, Body, VariantData};
55
use quote::{Tokens, ToTokens};
66

77
use overrides::Overrides;
8-
use fromsql::expand_derive_fromsql;
9-
use tosql::expand_derive_tosql;
108

119
mod accepts;
1210
mod composites;
@@ -15,17 +13,27 @@ mod fromsql;
1513
mod overrides;
1614
mod tosql;
1715

16+
pub fn expand_derive_tosql(source: &str) -> Result<String, String> {
17+
let input = try!(syn::parse_macro_input(source));
18+
tosql::expand_derive_tosql(&input)
19+
}
20+
21+
pub fn expand_derive_fromsql(source: &str) -> Result<String, String> {
22+
let input = try!(syn::parse_macro_input(source));
23+
fromsql::expand_derive_fromsql(&input)
24+
}
25+
1826
pub fn expand_derive(source: &str) -> Result<String, String> {
1927
let mut input = try!(syn::parse_macro_input(source));
2028
let (tosql, fromsql) = strip_derives(&mut input);
2129

2230
let tosql = if tosql {
23-
try!(expand_derive_tosql(&input))
31+
try!(tosql::expand_derive_tosql(&input))
2432
} else {
2533
"".to_owned()
2634
};
2735
let fromsql = if fromsql {
28-
try!(expand_derive_fromsql(&input))
36+
try!(fromsql::expand_derive_fromsql(&input))
2937
} else {
3038
"".to_owned()
3139
};

0 commit comments

Comments
 (0)