Skip to content

Commit 8e9b23b

Browse files
committed
Implement DatabaseConnection, change Table to Schema
1 parent c17c7f2 commit 8e9b23b

File tree

2 files changed

+24
-5
lines changed

2 files changed

+24
-5
lines changed

postgres_resource_derive/src/builder.rs

+20
Original file line numberDiff line numberDiff line change
@@ -41,3 +41,23 @@ impl<'i> Builder<'i> for Schema {
4141
Ok(quote!(crate::schema::#model))
4242
}
4343
}
44+
45+
struct DefaultDatabaseConnection;
46+
47+
impl<'i> Builder<'i> for DefaultDatabaseConnection {
48+
fn build(self, _: &'i Input) -> Result<proc_macro2::TokenStream> {
49+
Ok(quote!(&self.connection()))
50+
}
51+
}
52+
53+
pub struct DatabaseConnection;
54+
55+
impl<'i> Builder<'i> for DatabaseConnection {
56+
fn build(self, input: &'i Input) -> Result<proc_macro2::TokenStream> {
57+
if let Some(ref env_var) = input.parsed_struct.attrs.db_conn {
58+
Ok(quote!(&self.connection_string(#env_var)))
59+
} else {
60+
DefaultDatabaseConnection.build(input)
61+
}
62+
}
63+
}

postgres_resource_derive/src/lib.rs

+4-5
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,12 @@ impl Input {
7575
}
7676

7777
fn gen_controller(&self) -> Result<proc_macro2::TokenStream> {
78-
let model_with_id = self.parsed_struct.model_name_with_id();
79-
let model = self.parsed_struct.inner_model_name();
80-
let controller = self.parsed_struct.controller_name();
81-
8278
let schema = Schema.build(&self)?;
79+
let connection = DatabaseConnection.build(&self)?;
8380

84-
let connection = quote!(&self.connection());
81+
let model = self.parsed_struct.inner_model_name();
82+
let model_with_id = self.parsed_struct.model_name_with_id();
83+
let controller = self.parsed_struct.controller_name();
8584

8685
Ok(quote! {
8786
pub struct #controller;

0 commit comments

Comments
 (0)