|
5 | 5 | Syntax extensions to automatically derive `FromSql` and `ToSql` implementations for Postgres enum,
|
6 | 6 | domain, and composite types.
|
7 | 7 |
|
| 8 | +The generated code requires rust-postgres 0.11.3 or higher and Rust 1.6.0 or higher. |
| 9 | + |
| 10 | +# Usage |
| 11 | + |
| 12 | +postgres-derive can be used both as a syntax extension with a nightly build of the compiler, or |
| 13 | +as a code generator with stable builds. |
| 14 | + |
| 15 | +## Nightlies |
| 16 | + |
| 17 | +Simply depend on the `postgres-derive-macros` crate and register it as a plugin: |
| 18 | + |
| 19 | + |
| 20 | +Cargo.toml |
| 21 | +```toml |
| 22 | +# ... |
| 23 | + |
| 24 | +[dependencies] |
| 25 | +postgres-derive-macros = "0.1" |
| 26 | +postgres = "0.11.3" |
| 27 | +``` |
| 28 | + |
| 29 | +lib.rs |
| 30 | +```rust |
| 31 | +#![feature(plugin, custom_derive)] |
| 32 | +#![plugin(postgres_derive_macros)] |
| 33 | + |
| 34 | +#[macro_use] |
| 35 | +extern crate postgres; |
| 36 | + |
| 37 | +#[derive(Debug, ToSql, FromSql)] |
| 38 | +pub enum Mood { |
| 39 | + Sad, |
| 40 | + Ok, |
| 41 | + Happy, |
| 42 | +} |
| 43 | + |
| 44 | +// ... |
| 45 | +``` |
| 46 | + |
| 47 | +## Stable |
| 48 | + |
| 49 | +Use `syntex` along with `postgres-derive-codegen` in a build script: |
| 50 | + |
| 51 | +Cargo.toml |
| 52 | +```toml |
| 53 | +[package] |
| 54 | +# ... |
| 55 | +build = "build.rs" |
| 56 | + |
| 57 | +[build-dependencies] |
| 58 | +postgres-derive-codegen = "0.1" |
| 59 | +syntex = "0.29" |
| 60 | + |
| 61 | +[dependencies] |
| 62 | +postgres = "0.11.3" |
| 63 | +``` |
| 64 | + |
| 65 | +build.rs |
| 66 | +```rust |
| 67 | +extern crate syntex; |
| 68 | +extern crate postgres_derive_codegen; |
| 69 | + |
| 70 | +use std::env; |
| 71 | +use std::path::Path; |
| 72 | + |
| 73 | +pub fn main() { |
| 74 | + let out_dir = env::var_os("OUT_DIR").unwrap(); |
| 75 | + let mut registry = syntex::Registry::new(); |
| 76 | + postgres_derive_codegen::register(&mut registry); |
| 77 | + |
| 78 | + let src = Path::new("src/types.rs.in"); |
| 79 | + let dst = Path::new(&out_dir).join("types.rs"); |
| 80 | + |
| 81 | + registry.expand("", &src, &dst).unwrap(); |
| 82 | +} |
| 83 | +``` |
| 84 | + |
| 85 | +types.rs.in |
| 86 | +```rust |
| 87 | +#[derive(Debug, ToSql, FromSql)] |
| 88 | +pub enum Mood { |
| 89 | + Sad, |
| 90 | + Ok, |
| 91 | + Happy, |
| 92 | +} |
| 93 | +``` |
| 94 | + |
| 95 | +lib.rs |
| 96 | +```rust |
| 97 | +#[macro_use] |
| 98 | +extern crate postgres; |
| 99 | + |
| 100 | +include!(concat!(env!("OUT_DIR"), "/types.rs")); |
| 101 | + |
| 102 | +// ... |
| 103 | +``` |
| 104 | + |
| 105 | +# Types |
| 106 | + |
8 | 107 | ## Enums
|
9 | 108 |
|
10 | 109 | Postgres enums correspond to C-like enums in Rust:
|
|
0 commit comments