bitque/crates/derive_db_execute/src/lib.rs

319 lines
8.7 KiB
Rust
Raw Normal View History

mod parse_attr;
mod utils;
extern crate proc_macro;
2021-04-16 15:20:25 +02:00
use std::iter::Peekable;
use proc_macro::token_stream::IntoIter;
use proc_macro::{TokenStream, TokenTree};
use crate::parse_attr::Attributes;
2023-04-02 22:34:12 +02:00
#[cfg(test)]
pub mod schema;
fn parse_meta(mut it: Peekable<IntoIter>) -> (Peekable<IntoIter>, Option<Attributes>) {
let mut attrs: Option<Attributes> = None;
while let Some(token) = it.peek() {
match token {
// lookup for attr
TokenTree::Punct(p) if p.as_char() == '#' => {
let res = parse_attr::parse(it);
it = res.0;
attrs = res.1;
}
TokenTree::Ident(_) => {
break;
}
_ => {
eprintln!("skip token {:#?}", token);
it.next();
}
};
}
(it, attrs)
}
2021-01-19 21:57:48 +01:00
///
///
///
2023-04-01 22:31:57 +02:00
/// ## Example:
///
2023-04-02 22:34:12 +02:00
/// ```
/// use derive_db_execute::Execute;
2023-04-01 22:31:57 +02:00
///
2023-04-02 22:34:12 +02:00
/// pub struct Issue {
/// pub id: i32,
/// pub name: String,
/// }
2021-01-19 21:57:48 +01:00
///
2023-04-02 22:34:12 +02:00
/// #[derive(Execute)]
/// #[db_exec(schema = "issues", result = "Issue", find = "issues.find(msg.id)")]
/// pub struct FindOne {
/// pub id: i32,
/// }
2021-01-19 21:57:48 +01:00
///
2023-04-02 22:34:12 +02:00
/// #[derive(Execute)]
/// #[db_exec(schema = "issues", result = "Issue", load = "issues")]
/// pub struct LoadAll;
2021-01-19 21:57:48 +01:00
///
2023-04-02 22:34:12 +02:00
/// #[derive(Execute)]
/// #[db_exec(schema = "issues", result = "usize", destroy = "diesel::delete(issues.find(msg.id))")]
/// pub struct DeleteOne {
/// pub id: i32
/// }
2021-01-19 21:57:48 +01:00
///
2023-04-02 22:34:12 +02:00
/// #[derive(Execute)]
/// #[db_exec(schema = "issues", result = "Issue", destroy = "diesel::insert_into(issues).values(name.eq(msg.name))")]
/// pub struct CreateOne {
/// pub name: String
/// }
2021-01-19 21:57:48 +01:00
///
2023-04-02 22:34:12 +02:00
/// #[derive(Execute)]
/// #[db_exec(schema = "issues", result = "Issue", destroy = "diesel::update(issues.find(msg.id)).set(name.eq(msg.name))")]
/// pub struct UpdateOne {
/// pub id: i32,
/// pub name: String
/// }
2021-01-19 21:57:48 +01:00
/// ```
#[proc_macro_derive(Execute, attributes(db_exec))]
pub fn derive_enum_iter(item: TokenStream) -> TokenStream {
let mut it = item.into_iter().peekable();
let res = parse_meta(it);
it = res.0;
let attrs = res.1.expect("Result meta attribute is required");
let result = attrs
.result
.expect("Meta attribute `result` is required. Try add db_exec(result = \"foo\")");
let schema = attrs
.schema
.expect("Meta attribute `schema` is required. Try add db_exec(schema = \"foo\")");
it = utils::skip_pub(it);
it = utils::skip_struct(it);
let name = it
.next()
.expect("Expect to struct name but nothing was found")
.to_string();
let action_result = if attrs.load.is_some() {
format!("Vec<{}>", result)
} else if attrs.destroy.is_some() {
"usize".to_string()
} else {
result.clone()
};
let query = if let Some(q) = attrs.find {
build_find_exec(&name, &result, &schema, &q, &action_result)
} else if let Some(q) = attrs.load {
build_load_exec(&name, &result, &schema, &q, &action_result)
} else if let Some(q) = attrs.update {
build_update_exec(&name, &result, &schema, &q, &action_result)
} else if let Some(q) = attrs.destroy {
build_destroy_exec(&name, &result, &schema, &q, &action_result)
} else if let Some(q) = attrs.create {
build_create_exec(&name, &result, &schema, &q, &action_result)
} else {
"".to_string()
};
let code = format!(
r#"
impl actix::Message for {name} {{
type Result = Result<{action_result}, crate::DatabaseError>;
}}
impl actix::Handler<{name}> for crate::DbExecutor {{
type Result = Result<{action_result}, crate::DatabaseError>;
fn handle(&mut self, msg: {name}, _ctx: &mut Self::Context) -> Self::Result {{
2023-04-01 22:31:57 +02:00
let mut conn = crate::db_pool!(self);
2023-04-01 22:31:57 +02:00
msg.execute(&mut conn)
}}
}}
{query}
"#,
name = name,
query = query,
action_result = action_result
);
code.parse().unwrap()
}
fn build_create_exec(
name: &str,
resource: &str,
schema: &str,
query: &str,
action_result: &str,
) -> String {
format!(
r#"
impl {name} {{
pub fn execute(
self,
2023-04-01 22:31:57 +02:00
conn: &mut crate::DbPooledConn,
) -> Result<{action_result}, crate::DatabaseError> {{
2023-04-01 22:31:57 +02:00
conn.transaction(|conn| {{
use crate::schema::{schema}::dsl::*;
let msg = self;
2023-04-01 22:31:57 +02:00
crate::q!({query}).get_result(conn)
}}).map_err(|e| {{
::tracing::error!("{{:?}}", e);
crate::DatabaseError::GenericFailure(
crate::OperationError::Create,
crate::ResourceKind::{resource},
)
}})
}}
}}
"#,
name = name,
schema = schema,
query = query,
resource = resource,
action_result = action_result
)
}
fn build_find_exec(
name: &str,
resource: &str,
schema: &str,
query: &str,
action_result: &str,
) -> String {
format!(
r#"
impl {name} {{
pub fn execute(
self,
2023-04-01 22:31:57 +02:00
conn: &mut crate::DbPooledConn,
) -> Result<{action_result}, crate::DatabaseError> {{
use crate::schema::{schema}::dsl::*;
let msg = self;
crate::q!({query}).first(conn).map_err(|e| {{
2023-03-31 23:25:20 +02:00
::tracing::error!("{{:?}}", e);
crate::DatabaseError::GenericFailure(
crate::OperationError::LoadSingle,
crate::ResourceKind::{resource},
)
}})
}}
}}
"#,
name = name,
schema = schema,
query = query,
resource = resource,
action_result = action_result
)
}
fn build_load_exec(
name: &str,
resource: &str,
schema: &str,
query: &str,
action_result: &str,
) -> String {
format!(
r#"
impl {name} {{
pub fn execute(
self,
2023-04-01 22:31:57 +02:00
conn: &mut crate::DbPooledConn,
) -> Result<{action_result}, crate::DatabaseError> {{
use crate::schema::{schema}::dsl::*;
let msg = self;
crate::q!({query}).load(conn).map_err(|e| {{
2023-03-31 23:25:20 +02:00
::tracing::error!("{{:?}}", e);
crate::DatabaseError::GenericFailure(
crate::OperationError::LoadCollection,
crate::ResourceKind::{resource},
)
}})
}}
}}
"#,
name = name,
schema = schema,
query = query,
resource = resource,
action_result = action_result
)
}
fn build_update_exec(
name: &str,
resource: &str,
schema: &str,
query: &str,
action_result: &str,
) -> String {
format!(
r#"
impl {name} {{
pub fn execute(
self,
2023-04-01 22:31:57 +02:00
conn: &mut crate::DbPooledConn,
) -> Result<{action_result}, crate::DatabaseError> {{
use crate::schema::{schema}::dsl::*;
let msg = self;
crate::q!({query}).get_result(conn).map_err(|e| {{
2023-03-31 23:25:20 +02:00
::tracing::error!("{{:?}}", e);
crate::DatabaseError::GenericFailure(
crate::OperationError::Update,
crate::ResourceKind::{resource},
)
}})
}}
}}
"#,
name = name,
schema = schema,
query = query,
resource = resource,
action_result = action_result
)
}
fn build_destroy_exec(
name: &str,
resource: &str,
schema: &str,
query: &str,
action_result: &str,
) -> String {
format!(
r#"
impl {name} {{
pub fn execute(
self,
2023-04-01 22:31:57 +02:00
conn: &mut crate::DbPooledConn,
) -> Result<{action_result}, crate::DatabaseError> {{
use crate::schema::{schema}::dsl::*;
let msg = self;
crate::q!({query}).execute(conn).map_err(|e| {{
2023-03-31 23:25:20 +02:00
::tracing::error!("{{:?}}", e);
crate::DatabaseError::GenericFailure(
crate::OperationError::Delete,
crate::ResourceKind::{resource},
)
}})
}}
}}
"#,
name = name,
schema = schema,
query = query,
resource = resource,
action_result = action_result
)
}