Skip to content

Commit a0a44b9

Browse files
committed
change path for postgres_resource in example/hello_database, make some changes to hello_database/main.rs and simplify some codegen
1 parent ee21866 commit a0a44b9

File tree

3 files changed

+85
-78
lines changed

3 files changed

+85
-78
lines changed

examples/hello_database/Cargo.toml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,8 @@ edition = "2018"
77
[dependencies]
88
diesel = { version = "1.3.3", features = ["postgres", "chrono", "uuid"] }
99
uuid = { version = "0.6", features = ["serde", "v4"] }
10-
postgres-resource = { git = "https://github.com/technetos/postgres-resource" }
10+
#postgres-resource = { git = "https://github.com/technetos/postgres-resource" }
11+
postgres-resource = { path = "../../" }
1112
serde_derive = "1.0.27"
1213
serde = "1.0"
1314
serde_json = "1.0"

examples/hello_database/src/main.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,10 @@ struct World {
2121
}
2222

2323
fn main() {
24+
println!("{:?}", get_world_by_name("Mars"));
25+
}
26+
27+
fn create_planets() {
2428
println!("{:#?}", create_world("Mercury").expect("already exists"));
2529
println!("{:#?}", create_world("Venus").expect("already exists"));
2630
println!("{:#?}", create_world("Earth").expect("already exists"));
@@ -35,3 +39,8 @@ fn create_world(name: &str) -> Result<WorldWithId, ()> {
3539

3640
return Ok(modelWithId);
3741
}
42+
43+
fn get_world_by_name(string: &'static str) -> Result<WorldWithId, &'static str> {
44+
let model = WorldController.get_one(Box::new(worlds::name.eq(string))).map_err(|_| "not found")?;
45+
Ok(model)
46+
}

postgres_resource_derive/src/lib.rs

Lines changed: 74 additions & 77 deletions
Original file line numberDiff line numberDiff line change
@@ -11,13 +11,22 @@ extern crate quote;
1111
type TokenStream = proc_macro::TokenStream;
1212

1313
use proc_macro2::Span;
14-
use syn::Ident;
1514
use syn::{
1615
parse::{Parse, ParseStream, Result},
1716
punctuated::Punctuated,
18-
token, Attribute,
17+
token, Attribute, Ident,
1918
};
2019

20+
trait IdentExt {
21+
fn append(&self, string: &str) -> Ident;
22+
}
23+
24+
impl IdentExt for syn::Ident {
25+
fn append(&self, string: &str) -> Ident {
26+
Ident::new(&format!("{}{}", self, string), self.span())
27+
}
28+
}
29+
2130
#[derive(Debug)]
2231
struct Struct {
2332
pub attrs: Vec<Attribute>,
@@ -67,14 +76,18 @@ impl Field {
6776
}
6877
}
6978

70-
fn optional(&self) -> bool {
71-
let mut ret = false;
79+
fn ty(&self) -> proc_macro2::TokenStream {
80+
let ty = &self.ty;
81+
82+
let mut ty_tokens = quote!(#ty);
83+
7284
self.walk_attrs(&mut |ref ident| {
7385
if *ident == "optional" {
74-
ret = true;
86+
ty_tokens = quote!(Option<#ty>);
7587
}
7688
});
77-
ret
89+
90+
ty_tokens
7891
}
7992

8093
fn fk(&self) -> bool {
@@ -124,6 +137,18 @@ struct Schema {
124137
pub schema: syn::Path,
125138
}
126139

140+
impl Schema {
141+
fn sql_type(&self) -> proc_macro2::TokenStream {
142+
let schema = &self.schema;
143+
quote!(#schema::SqlType)
144+
}
145+
146+
fn table(&self) -> proc_macro2::TokenStream {
147+
let schema = &self.schema;
148+
quote!(#schema::table)
149+
}
150+
}
151+
127152
struct Table {
128153
pub table_token: Ident,
129154
pub assignment_token: Token![=],
@@ -166,110 +191,82 @@ struct Parsed {
166191
}
167192

168193
impl Parsed {
169-
fn gen_model_with_id_fields(&self) -> Vec<proc_macro2::TokenStream> {
194+
fn model_with_id_fields(&self) -> Vec<proc_macro2::TokenStream> {
170195
let mut fields = Vec::new();
171196
fields.push(quote!(pub id: i32));
172197

173198
let model_name = &self.input.ident;
174-
175199
let model_name_lower = Ident::new(&self.input.camel_to_snake(), Span::call_site());
176200

177-
let model_with_id_model_field = quote!(pub #model_name_lower: #model_name);
178-
179-
fields.push(model_with_id_model_field);
201+
fields.push(quote!(pub #model_name_lower: #model_name));
180202

181203
self.input.fields.iter().for_each(|field| {
182204
if field.fk() {
183-
let ty = &field.ty;
205+
let ty = field.ty();
184206
let name = &field.name;
185-
186-
if field.optional() {
187-
fields.push(quote!(pub #name: Option<#ty>));
188-
} else {
189-
fields.push(quote!(pub #name: #ty));
190-
}
207+
fields.push(quote!(pub #name: #ty));
191208
}
192209
});
193210
fields
194211
}
195212

196-
fn gen_model_with_id_ident(&self) -> Ident {
197-
Ident::new(&format!("{}WithId", self.input.ident), Span::call_site())
213+
fn model_with_id_ident(&self) -> Ident {
214+
self.input.ident.append("WithId")
198215
}
199216

200-
fn gen_model_with_id(&self) -> proc_macro2::TokenStream {
201-
let model_with_id = self.gen_model_with_id_ident();
202-
let table_name = &self.attr.table.table;
203-
let model_with_id_fields = self.gen_model_with_id_fields();
217+
fn model_with_id(&self) -> proc_macro2::TokenStream {
218+
let model_with_id = self.model_with_id_ident();
219+
let table = &self.attr.table.table;
220+
let fields = self.model_with_id_fields();
204221

205222
quote_spanned! {Span::call_site()=>
206223
#[derive(Serialize, Deserialize, FromSqlRow, Associations, Identifiable, Debug, PartialEq)]
207-
#[table_name = #table_name]
208-
224+
#[table_name = #table]
209225
pub struct #model_with_id {
210-
#(#model_with_id_fields,)*
226+
#(#fields,)*
211227
}
212228
}
213229
}
214230

215-
fn gen_model_fields(&self) -> Vec<proc_macro2::TokenStream> {
231+
fn model_fields(&self) -> Vec<proc_macro2::TokenStream> {
216232
let mut fields = Vec::new();
217233
self.input.fields.iter().for_each(|field| {
218234
if !field.fk() {
219-
let ty = &field.ty;
235+
let ty = field.ty();
220236
let name = &field.name;
221-
222-
if field.optional() {
223-
fields.push(quote!(pub #name: Option<#ty>));
224-
} else {
225-
fields.push(quote!(pub #name: #ty));
226-
}
237+
fields.push(quote!(pub #name: #ty));
227238
}
228239
});
229240
fields
230241
}
231242

232-
fn gen_model(&self) -> proc_macro2::TokenStream {
233-
let model_name = &self.input.ident;
234-
let model_fields = self.gen_model_fields();
235-
let table_name = &self.attr.table.table;
243+
fn model(&self) -> proc_macro2::TokenStream {
244+
let model = &self.input.ident;
245+
let fields = self.model_fields();
246+
let table = &self.attr.table.table;
236247

237248
quote_spanned! {Span::call_site()=>
238249
#[derive(Serialize, Deserialize, FromSqlRow, Insertable, AsChangeset, Debug, PartialEq)]
239-
#[table_name = #table_name]
240-
pub struct #model_name {
241-
#(#model_fields,)*
250+
#[table_name = #table]
251+
pub struct #model {
252+
#(#fields,)*
242253
}
243254
}
244255
}
245256

246-
fn gen_table(&self) -> proc_macro2::TokenStream {
247-
let schema = &self.attr.schema.schema;
248-
quote_spanned!(Span::call_site()=> #schema::table)
249-
}
250-
251-
fn gen_sql_type(&self) -> proc_macro2::TokenStream {
252-
let schema = &self.attr.schema.schema;
253-
quote_spanned!(Span::call_site()=> #schema::SqlType)
254-
}
255-
256-
fn gen_queryable_row(&self) -> proc_macro2::TokenStream {
257+
fn queryable_row(&self) -> proc_macro2::TokenStream {
257258
let mut fields = Vec::new();
258259
fields.push(quote!(i32));
259260

260261
self.input.fields.iter().for_each(|field| {
261-
let ty = &field.ty;
262-
if field.optional() {
263-
fields.push(quote!(Option<#ty>));
264-
} else {
265-
fields.push(quote!(#ty));
266-
}
262+
let ty = field.ty();
263+
fields.push(quote!(#ty));
267264
});
268265

269-
quote_spanned!(Span::call_site()=> type Row = (#(#fields,)*);)
266+
quote!(type Row = (#(#fields,)*);)
270267
}
271268

272-
fn gen_queryable_inner_fields(&self) -> Vec<proc_macro2::TokenStream> {
269+
fn queryable_inner_fields(&self) -> Vec<proc_macro2::TokenStream> {
273270
let mut fields = Vec::new();
274271
let mut inner_fields = Vec::new();
275272

@@ -309,9 +306,9 @@ impl Parsed {
309306
fields
310307
}
311308

312-
fn gen_queryable_fields(&self) -> proc_macro2::TokenStream {
313-
let fields = self.gen_queryable_inner_fields();
314-
let model_with_id = self.gen_model_with_id_ident();
309+
fn queryable_fields(&self) -> proc_macro2::TokenStream {
310+
let fields = self.queryable_inner_fields();
311+
let model_with_id = self.model_with_id_ident();
315312

316313
quote! {
317314
#model_with_id {
@@ -320,11 +317,11 @@ impl Parsed {
320317
}
321318
}
322319

323-
fn gen_queryable_impl(&self) -> proc_macro2::TokenStream {
324-
let fields = self.gen_queryable_fields();
325-
let model_with_id = self.gen_model_with_id_ident();
326-
let sql_type = self.gen_sql_type();
327-
let row = self.gen_queryable_row();
320+
fn queryable_impl(&self) -> proc_macro2::TokenStream {
321+
let fields = self.queryable_fields();
322+
let model_with_id = self.model_with_id_ident();
323+
let sql_type = self.attr.schema.sql_type();
324+
let row = self.queryable_row();
328325

329326
quote_spanned! {Span::call_site()=>
330327
impl diesel::Queryable<#sql_type, diesel::pg::Pg> for #model_with_id {
@@ -338,12 +335,12 @@ impl Parsed {
338335

339336
fn gen_resource_controller(&self) -> proc_macro2::TokenStream {
340337
let model = &self.input.ident;
341-
let model_with_id = self.gen_model_with_id_ident();
342-
let controller = Ident::new(&format!("{}Controller", &self.input.ident), Span::call_site());
343-
let table = self.gen_table();
344-
let sql_type = self.gen_sql_type();
338+
let model_with_id = self.model_with_id_ident();
339+
let controller = &self.input.ident.append("Controller");
340+
let table = self.attr.schema.table();
341+
let sql_type = self.attr.schema.sql_type();
345342

346-
quote_spanned! {Span::call_site()=>
343+
quote! {
347344
pub struct #controller;
348345

349346
impl ResourceDB for #controller {}
@@ -499,10 +496,10 @@ pub fn resource(attr: TokenStream, input: TokenStream) -> TokenStream {
499496

500497
let parsed = Parsed { attr: parsed_attr, input: parsed_struct };
501498

502-
let model_with_id = parsed.gen_model_with_id();
503-
let model = parsed.gen_model();
499+
let model_with_id = parsed.model_with_id();
500+
let model = parsed.model();
504501
let resource_controller = parsed.gen_resource_controller();
505-
let queryable = parsed.gen_queryable_impl();
502+
let queryable = parsed.queryable_impl();
506503

507504
let generated = quote_spanned! {Span::call_site()=>
508505
#model_with_id

0 commit comments

Comments
 (0)