Skip to content

Commit 9fb4465

Browse files
committed
add DBVAR macro to optionally specify the env var that specifies the database path
1 parent 6fb58fc commit 9fb4465

File tree

2 files changed

+38
-13
lines changed

2 files changed

+38
-13
lines changed

postgres_resource_derive/src/lib.rs

Lines changed: 33 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -66,13 +66,7 @@ struct Field {
6666
impl Field {
6767
fn walk_attrs(&self, callback: &mut FnMut(&Ident)) {
6868
if let Some(ref field_attrs) = self.attr {
69-
field_attrs.iter().for_each(|a| {
70-
let Attribute { path, .. } = a;
71-
let syn::Path { segments, .. } = path;
72-
let syn::PathSegment { ident, .. } = &segments[0];
73-
74-
callback(ident);
75-
})
69+
field_attrs.iter().for_each(|a| callback(&a.path.segments[0].ident))
7670
}
7771
}
7872

@@ -191,6 +185,32 @@ struct Parsed {
191185
}
192186

193187
impl Parsed {
188+
fn db_connection(&self) -> proc_macro2::TokenStream {
189+
let mut connection = quote!(&self.connection());
190+
let mut var: Option<&Ident> = None;
191+
let mut url: Option<proc_macro2::Literal> = None;
192+
193+
self.input.attrs.iter().for_each(|a| {
194+
let ident = &a.path.segments[0].ident;
195+
if ident == "DBVAR" {
196+
var = Some(ident);
197+
}
198+
&a.tts.clone().into_iter().for_each(|token| {
199+
if let proc_macro2::TokenTree::Literal(lit) = token {
200+
url = Some(lit);
201+
}
202+
});
203+
});
204+
205+
if let Some(env_var) = var {
206+
if let Some(db_url) = url {
207+
connection = quote!(&self.connection_string(#db_url));
208+
}
209+
}
210+
211+
connection
212+
}
213+
194214
fn model_with_id_fields(&self) -> Vec<proc_macro2::TokenStream> {
195215
let mut fields = Vec::new();
196216
fields.push(quote!(pub id: i32));
@@ -339,6 +359,7 @@ impl Parsed {
339359
let controller = &self.input.ident.append("Controller");
340360
let table = self.attr.schema.table();
341361
let sql_type = self.attr.schema.sql_type();
362+
let connection = self.db_connection();
342363

343364
quote! {
344365
pub struct #controller;
@@ -365,30 +386,30 @@ impl Parsed {
365386
fn create(&self, model: &Self::Model) -> Result<Self::ModelWithId, Error> {
366387
Ok(insert_into(#table)
367388
.values(model)
368-
.get_result(&self.connection())?)
389+
.get_result(#connection)?)
369390
}
370391

371392
fn get_one(&self, by: Expr<#table>) -> Result<Self::ModelWithId, Error> {
372393
Ok(#table
373394
.filter(by)
374-
.get_result::<Self::ModelWithId>(&self.connection())?)
395+
.get_result::<Self::ModelWithId>(#connection)?)
375396
}
376397

377398
fn get_all(&self, by: Expr<#table>) -> Result<Vec<Self::ModelWithId>, Error> {
378399
Ok(#table
379400
.filter(by)
380-
.get_results::<Self::ModelWithId>(&self.connection())?)
401+
.get_results::<Self::ModelWithId>(#connection)?)
381402
}
382403

383404
fn update(&self, model: &Self::Model, by: Expr<#table>) -> Result<Self::ModelWithId, Error> {
384405
Ok(update(#table)
385406
.filter(by)
386407
.set(model)
387-
.get_result::<Self::ModelWithId>(&self.connection())?)
408+
.get_result::<Self::ModelWithId>(#connection)?)
388409
}
389410

390411
fn delete(&self, by: Expr<#table>) -> Result<usize, Error> {
391-
Ok(delete(#table).filter(by).execute(&self.connection())?)
412+
Ok(delete(#table).filter(by).execute(#connection)?)
392413
}
393414
}
394415
}

src/lib.rs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,11 @@ use diesel::{
1212

1313
pub trait ResourceDB {
1414
fn connection(&self) -> PgConnection {
15-
let env_var = std::env::var("DATABASE_URL").expect("DATABASE_URL not set");
15+
self.connection_string("DATABASE_URL")
16+
}
17+
18+
fn connection_string(&self, string: &str) -> PgConnection {
19+
let env_var = std::env::var(&string).expect(&format!("{} not set", &string));
1620
PgConnection::establish(&env_var[..]).expect("Unable to establish connection to database")
1721
}
1822
}

0 commit comments

Comments
 (0)