orma_derive/
lib.rs

1#![doc(
2    html_logo_url = "https://raw.githubusercontent.com/RedSoftwareSystems/rust-orma/master/orma.svg?sanitize=true"
3)]
4//! This crate provides derive macros for orma.
5//!
6//! ## Example
7//!
8//! ```edition2018
9//!  use orma::*;
10//!  use serde::{Serialize, Deserialize};
11//!
12//!  #[orma_derive::orma_obj(table = "table_name")]
13//!  #[derive(Serialize, Deserialize)]
14//!  struct TestData {
15//!      field_1: String,
16//!      field_2: String,
17//!      some_other_filed: String,
18//!  }
19//!
20//! ```
21//!
22extern crate proc_macro;
23
24mod dbdata;
25mod test_impl;
26
27use syn::{parse_macro_input, AttributeArgs, DeriveInput};
28
29/// This crate provides derive macros for orma.
30///
31/// ## Example
32///
33/// ```edition2018
34///  use serde::{Serialize, Deserialize};
35///
36///  #[orma_derive::orma_obj(table = "table_name")]
37///  #[derive(Serialize, Deserialize)]
38///  struct TestData {
39///      field_1: String,
40///      field_2: String,
41///      some_other_filed: String,
42///  }
43///
44/// ```
45///
46#[proc_macro_attribute]
47pub fn orma_obj(
48    args: proc_macro::TokenStream,
49    input: proc_macro::TokenStream,
50) -> proc_macro::TokenStream {
51    // Parse tokens
52    let args = parse_macro_input!(args as AttributeArgs);
53    let mut input: DeriveInput = parse_macro_input!(input);
54    let gen = dbdata::impl_orma(&args, &mut input);
55
56    // println!(
57    //     r#"
58    // --------------------- RESULT CODE ---------------------
59    // {}
60    // -------------------------------------------------------"#,
61    //     gen
62    // );
63    // Return the generated impl
64    gen.into()
65}
66
67/// This macro produces an async test.
68///
69/// For each argument there is a function with the same name that provides the value for that argument and receives as input
70/// the name of the test function (as &str)
71///
72/// # Example:
73/// ```edition2018
74/// fn connection(input: &str) -> &str {
75///     input
76/// }
77///
78/// #[orma_derive::test]
79/// async fn test_orma_test(connection: &str) {
80///     assert_eq!(data, "test_orma_test");
81/// }
82/// ```
83#[proc_macro_attribute]
84pub fn test(
85    _args: proc_macro::TokenStream,
86    body: proc_macro::TokenStream,
87) -> proc_macro::TokenStream {
88    // Parse tokens
89    let ast = syn::parse(body).unwrap();
90
91    // Build impl
92    let gen = test_impl::test_impl(ast);
93
94    // println!(
95    //     r#"
96    // --------------------- RESULT CODE ---------------------
97    // {}
98    // -------------------------------------------------------"#,
99    //     gen
100    // );
101    // Return the generated impl
102
103    gen.into()
104}