1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
#![recursion_limit = "256"]
extern crate proc_macro;
extern crate proc_macro2;
extern crate syn;
#[macro_use]
extern crate quote;
extern crate heck;

use heck::SnakeCase;
use proc_macro::TokenStream;
use proc_macro2::Span;
use syn::{DeriveInput, Ident};

#[proc_macro_derive(AsJsonb)]
pub fn hello_macro_derive(input: TokenStream) -> TokenStream {
    let ast: DeriveInput = syn::parse(input).unwrap();
    let name = &ast.ident;
    let scope = Ident::new(
        &format!("{}_as_jsonb", name).to_snake_case(),
        Span::call_site(),
    );
    let proxy = Ident::new(&format!("{}ValueProxy", name), Span::call_site());
    let gen = quote! {
        mod #scope {
            use super::*;
            use diesel::sql_types::Jsonb;
            use std::io::Write;
            use diesel::pg::Pg;
            use diesel::serialize::{self, IsNull, Output, ToSql};
            use diesel::deserialize::{self, FromSql};

            #[derive(FromSqlRow, AsExpression)]
            #[diesel(foreign_derive)]
            #[sql_type = "Jsonb"]
            struct #proxy(#name);

            impl FromSql<Jsonb, Pg> for #name {
                fn from_sql(bytes: Option<&[u8]>) -> deserialize::Result<Self> {
                    let bytes = not_none!(bytes);
                    if bytes[0] != 1 {
                        return Err("Unsupported JSONB encoding version".into());
                    }
                    serde_json::from_slice(&bytes[1..]).map_err(Into::into)
                }
            }

            impl ToSql<Jsonb, Pg> for #name {
                fn to_sql<W: Write>(&self, out: &mut Output<W, Pg>) -> serialize::Result {
                    try!(out.write_all(&[1]));
                    serde_json::to_writer(out, self)
                        .map(|_| IsNull::No)
                        .map_err(Into::into)
                }
            }
        }
    };
    gen.into()
}