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

Commit 3246b42

Browse files
committed
Merge branch 'release-v0.1.3' into release
2 parents ff2f1c3 + ae49029 commit 3246b42

File tree

12 files changed

+54
-59
lines changed

12 files changed

+54
-59
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ sudo: false
33
rust:
44
- nightly
55
- beta
6-
- 1.6.0
6+
- 1.8.0
77
script:
88
- (test $TRAVIS_RUST_VERSION != "nightly" || env RUST_BACKTRACE=1 cargo test --manifest-path=postgres-derive-macros/Cargo.toml)
99
- cargo test --manifest-path=postgres-derive-codegen/test/Cargo.toml

README.md

Lines changed: 3 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
Syntax extensions to automatically derive `FromSql` and `ToSql` implementations for Postgres enum,
66
domain, and composite types.
77

8-
The generated code requires rust-postgres 0.11.3 or higher and Rust 1.6.0 or higher.
8+
The generated code requires rust-postgres 0.11.3 or higher and Rust 1.8.0 or higher.
99

1010
# Usage
1111

@@ -46,7 +46,7 @@ pub enum Mood {
4646

4747
## Stable
4848

49-
Use `syntex` along with `postgres-derive-codegen` in a build script:
49+
Use `postgres-derive-codegen` in a build script:
5050

5151
Cargo.toml
5252
```toml
@@ -56,29 +56,24 @@ build = "build.rs"
5656

5757
[build-dependencies]
5858
postgres-derive-codegen = "0.1"
59-
syntex = "0.29"
6059

6160
[dependencies]
6261
postgres = "0.11.3"
6362
```
6463

6564
build.rs
6665
```rust
67-
extern crate syntex;
6866
extern crate postgres_derive_codegen;
6967

7068
use std::env;
7169
use std::path::Path;
7270

7371
pub fn main() {
7472
let out_dir = env::var_os("OUT_DIR").unwrap();
75-
let mut registry = syntex::Registry::new();
76-
postgres_derive_codegen::register(&mut registry);
77-
7873
let src = Path::new("src/types.rs.in");
7974
let dst = Path::new(&out_dir).join("types.rs");
8075

81-
registry.expand("", &src, &dst).unwrap();
76+
postgres_derive_codegen::expand(src, dst).unwrap();
8277
}
8378
```
8479

postgres-derive-codegen/Cargo.toml

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "postgres-derive-codegen"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["Steven Fackler <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "Deriving codegen support for Postgres enum, domain, and composite types"
@@ -16,8 +16,8 @@ nightly = []
1616
with-syntex = ["syntex", "syntex_syntax"]
1717

1818
[build-dependencies]
19-
syntex = { version = "0.31", optional = true }
19+
syntex = { version = "0.36", optional = true }
2020

2121
[dependencies]
22-
syntex = { version = "0.31", optional = true }
23-
syntex_syntax = { version = "0.31", optional = true }
22+
syntex = { version = "0.36", optional = true }
23+
syntex_syntax = { version = "0.36", optional = true }

postgres-derive-codegen/src/enums.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ pub fn get_variants(ctx: &mut ExtCtxt, trait_: &str, def: &EnumDef) -> Vec<(Iden
1212
VariantData::Unit(_) => {}
1313
_ => {
1414
ctx.span_err(variant.span,
15-
&format!("#[derive({})] can only be applied to C-like enums", trait_));
15+
&format!("#[derive({})] does not support non-C-like enums", trait_));
1616
continue;
1717
}
1818
}

postgres-derive-codegen/src/fromsql.rs

Lines changed: 12 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use syntax::ext::base::{Annotatable, ExtCtxt};
22
use syntax::codemap::Span;
33
use syntax::ast::{MetaItem, ItemKind, Block, VariantData, Ident, Ty};
4-
use syntax::attr::AttrMetaMethods;
54
use syntax::ptr::P;
65
use syntax::ext::build::AstBuilder;
76
use syntax::parse::token::{self, InternedString};
@@ -18,8 +17,8 @@ pub fn expand(ctx: &mut ExtCtxt,
1817
let item = match *annotatable {
1918
Annotatable::Item(ref item) => item,
2019
_ => {
21-
ctx.span_err(span,
22-
"#[derive(FromSql)] can only be applied to tuple structs and enums");
20+
ctx.span_err(span, "#[derive(FromSql)] can only be applied to structs, single field \
21+
tuple structs, and enums");
2322
return;
2423
}
2524
};
@@ -35,8 +34,8 @@ pub fn expand(ctx: &mut ExtCtxt,
3534
}
3635
ItemKind::Struct(VariantData::Tuple(ref fields, _), _) => {
3736
if fields.len() != 1 {
38-
ctx.span_err(span,
39-
"#[derive(FromSql)] can only be applied to one field tuple structs");
37+
ctx.span_err(span, "#[derive(FromSql)] can only be applied to structs, single \
38+
field tuple structs, and enums");
4039
return;
4140
}
4241
let inner = &fields[0].ty;
@@ -57,8 +56,8 @@ pub fn expand(ctx: &mut ExtCtxt,
5756
composite_from_sql_body(ctx, span, item.ident, &*fields))
5857
}
5958
_ => {
60-
ctx.span_err(span,
61-
"#[derive(FromSql)] can only be applied to tuple structs and enums");
59+
ctx.span_err(span, "#[derive(FromSql)] can only be applied to structs, single field \
60+
tuple structs, and enums");
6261
return;
6362
}
6463
};
@@ -101,11 +100,8 @@ fn enum_from_sql_body(ctx: &mut ExtCtxt,
101100
}
102101

103102
arms.push(quote_arm!(ctx, v => {
104-
let err: ::std::boxed::Box<::std::error::Error
105-
+ ::std::marker::Sync
106-
+ ::std::marker::Send>
107-
= format!("unknown variant `{}`", v).into();
108-
::std::result::Result::Err(::postgres::error::Error::Conversion(err))
103+
::std::result::Result::Err(::postgres::error::Error::Conversion(
104+
format!("unknown variant `{}`", v).into()))
109105
}));
110106

111107
let buf = token::str_to_ident("buf");
@@ -188,23 +184,17 @@ fn composite_from_sql_body(ctx: &mut ExtCtxt,
188184

189185
let num_fields = try!(read_be_i32(r));
190186
if num_fields as usize != fields.len() {
191-
let err: ::std::boxed::Box<::std::error::Error
192-
+ ::std::marker::Sync
193-
+ ::std::marker::Send>
194-
= format!("expected {} fields but saw {}", fields.len(), num_fields).into();
195-
return ::std::result::Result::Err(::postgres::error::Error::Conversion(err))
187+
return ::std::result::Result::Err(::postgres::error::Error::Conversion(
188+
format!("expected {} fields but saw {}", fields.len(), num_fields).into()));
196189
}
197190

198191
$declare_vars;
199192

200193
for field in fields {
201194
let oid = try!(read_be_i32(r)) as u32;
202195
if oid != field.type_().oid() {
203-
let err: ::std::boxed::Box<::std::error::Error
204-
+ ::std::marker::Sync
205-
+ ::std::marker::Send>
206-
= format!("expected OID {} but saw {}", field.type_().oid(), oid).into();
207-
return ::std::result::Result::Err(::postgres::error::Error::Conversion(err))
196+
return ::std::result::Result::Err(::postgres::error::Error::Conversion(
197+
format!("expected OID {} but saw {}", field.type_().oid(), oid).into()));
208198
}
209199

210200
let len = try!(read_be_i32(r));

postgres-derive-codegen/src/lib.rs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,16 @@ include!(concat!(env!("OUT_DIR"), "/lib.rs"));
1515
#[cfg(not(feature = "with-syntex"))]
1616
include!("lib.rs.in");
1717

18+
#[cfg(feature = "with-syntex")]
19+
pub fn expand<S, D>(src: S, dst: D) -> Result<(), syntex::Error>
20+
where S: AsRef<std::path::Path>,
21+
D: AsRef<std::path::Path>,
22+
{
23+
let mut registry = syntex::Registry::new();
24+
register(&mut registry);
25+
registry.expand("", src.as_ref(), dst.as_ref())
26+
}
27+
1828
#[cfg(feature = "with-syntex")]
1929
pub fn register(reg: &mut syntex::Registry) {
2030
use syntax::{ast, fold};

postgres-derive-codegen/src/tosql.rs

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use syntax::ext::base::{Annotatable, ExtCtxt};
22
use syntax::codemap::Span;
33
use syntax::ast::{MetaItem, ItemKind, Block, VariantData, Ident, Ty};
4-
use syntax::attr::AttrMetaMethods;
54
use syntax::ptr::P;
65
use syntax::ext::build::AstBuilder;
76
use syntax::parse::token::InternedString;
@@ -18,8 +17,8 @@ pub fn expand(ctx: &mut ExtCtxt,
1817
let item = match *annotatable {
1918
Annotatable::Item(ref item) => item,
2019
_ => {
21-
ctx.span_err(span,
22-
"#[derive(ToSql)] can only be applied to tuple structs and enums");
20+
ctx.span_err(span, "#[derive(ToSql)] can only be applied to structs, single field \
21+
tuple structs, and enums");
2322
return;
2423
}
2524
};
@@ -35,8 +34,8 @@ pub fn expand(ctx: &mut ExtCtxt,
3534
}
3635
ItemKind::Struct(VariantData::Tuple(ref fields, _), _) => {
3736
if fields.len() != 1 {
38-
ctx.span_err(span,
39-
"#[derive(ToSql)] can only be applied to one field tuple structs");
37+
ctx.span_err(span, "#[derive(ToSql)] can only be applied to structs, single field \
38+
tuple structs, and enums");
4039
return;
4140
}
4241
let inner = &fields[0].ty;
@@ -57,8 +56,8 @@ pub fn expand(ctx: &mut ExtCtxt,
5756
composite_to_sql_body(ctx, span, &*fields))
5857
}
5958
_ => {
60-
ctx.span_err(span,
61-
"#[derive(ToSql)] can only be applied to tuple structs and enums");
59+
ctx.span_err(span, "#[derive(ToSql)] can only be applied to structs, single field \
60+
tuple structs, and enums");
6261
return;
6362
}
6463
};
@@ -114,7 +113,7 @@ fn enum_to_sql_body(ctx: &mut ExtCtxt,
114113
let match_ = ctx.expr_match(span, match_arg, arms);
115114

116115
quote_block!(ctx, {
117-
let s: &'static str = $match_;
116+
let s = $match_;
118117
try!(::std::io::Write::write_all(out, s.as_bytes()));
119118
::std::result::Result::Ok(::postgres::types::IsNull::No)
120119
})
@@ -167,7 +166,13 @@ fn composite_to_sql_body(ctx: &mut ExtCtxt,
167166
match try!(r) {
168167
::postgres::types::IsNull::Yes => try!(write_be_i32(out, -1)),
169168
::postgres::types::IsNull::No => {
170-
try!(write_be_i32(out, buf.len() as i32));
169+
let len = if buf.len() > i32::max_value() as usize {
170+
return ::std::result::Result::Err(::postgres::error::Error::Conversion(
171+
"value too large to transmit".into()));
172+
} else {
173+
buf.len() as i32
174+
};
175+
try!(write_be_i32(out, len));
171176
try!(::std::io::Write::write_all(out, &buf));
172177
}
173178
}

postgres-derive-codegen/test/Cargo.toml

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,6 @@ build = "build.rs"
66

77
[build-dependencies]
88
postgres-derive-codegen = { path = ".." }
9-
syntex = "0.31"
109

1110
[dependencies]
1211
postgres = "0.11.1"

postgres-derive-codegen/test/build.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,16 +1,12 @@
1-
extern crate syntex;
21
extern crate postgres_derive_codegen;
32

43
use std::env;
54
use std::path::Path;
65

76
pub fn main() {
87
let out_dir = env::var_os("OUT_DIR").unwrap();
9-
let mut registry = syntex::Registry::new();
10-
postgres_derive_codegen::register(&mut registry);
11-
128
let src = Path::new("src/types.rs.in");
139
let dst = Path::new(&out_dir).join("types.rs");
1410

15-
registry.expand("", &src, &dst).unwrap();
11+
postgres_derive_codegen::expand(src, dst).unwrap();
1612
}

postgres-derive-macros/Cargo.toml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "postgres-derive-macros"
3-
version = "0.1.2"
3+
version = "0.1.3"
44
authors = ["Steven Fackler <[email protected]>"]
55
license = "MIT/Apache-2.0"
66
description = "Deriving macro support for Postgres enum, domain, and composite types"
@@ -13,7 +13,7 @@ plugin = true
1313
test = false
1414

1515
[dependencies]
16-
postgres-derive-codegen = { version = "0.1.2", path = "../postgres-derive-codegen", default-features = false, features = ["nightly"] }
16+
postgres-derive-codegen = { version = "0.1.3", path = "../postgres-derive-codegen", default-features = false, features = ["nightly"] }
1717

1818
[dev-dependencies]
1919
postgres = "0.11.3"

0 commit comments

Comments
 (0)