|
1 | 1 | //! Provides helpers to build the `#[canyon_macros::canyon]` procedural like attribute macro |
2 | 2 |
|
3 | | -use proc_macro::TokenStream as TokenStream1; |
4 | | -use proc_macro2::{Ident, TokenStream}; |
5 | | - |
| 3 | +use proc_macro2::TokenStream; |
6 | 4 | use quote::quote; |
7 | | - |
| 5 | +use canyon_connection::CANYON_TOKIO_RUNTIME; |
8 | 6 | use canyon_migrations::{CM_QUERIES_TO_EXECUTE, QUERIES_TO_EXECUTE}; |
9 | | -use syn::{Lit, NestedMeta}; |
10 | | - |
11 | | -#[derive(Debug)] |
12 | | -/// Utilery struct for wrapping the content and result of parsing the attributes on the `canyon` macro |
13 | | -pub struct CanyonMacroAttributes { |
14 | | - pub allowed_migrations: bool, |
15 | | - pub error: Option<TokenStream1>, |
16 | | -} |
17 | | - |
18 | | -/// Parses the [`syn::NestedMeta::Meta`] or [`syn::NestedMeta::Lit`] attached to the `canyon` macro |
19 | | -pub fn parse_canyon_macro_attributes(_meta: &Vec<NestedMeta>) -> CanyonMacroAttributes { |
20 | | - let mut res = CanyonMacroAttributes { |
21 | | - allowed_migrations: false, |
22 | | - error: None, |
23 | | - }; |
24 | | - |
25 | | - for nested_meta in _meta { |
26 | | - match nested_meta { |
27 | | - syn::NestedMeta::Meta(m) => determine_allowed_attributes(m, &mut res), |
28 | | - syn::NestedMeta::Lit(lit) => match lit { |
29 | | - syn::Lit::Str(ref l) => { |
30 | | - res.error = Some(report_literals_not_allowed(&l.value(), lit)) |
31 | | - } |
32 | | - syn::Lit::ByteStr(ref l) => { |
33 | | - res.error = Some(report_literals_not_allowed( |
34 | | - &String::from_utf8_lossy(&l.value()), |
35 | | - lit, |
36 | | - )) |
37 | | - } |
38 | | - syn::Lit::Byte(ref l) => { |
39 | | - res.error = Some(report_literals_not_allowed(&l.value().to_string(), lit)) |
40 | | - } |
41 | | - syn::Lit::Char(ref l) => { |
42 | | - res.error = Some(report_literals_not_allowed(&l.value().to_string(), lit)) |
43 | | - } |
44 | | - syn::Lit::Int(ref l) => { |
45 | | - res.error = Some(report_literals_not_allowed(&l.to_string(), lit)) |
46 | | - } |
47 | | - syn::Lit::Float(ref l) => { |
48 | | - res.error = Some(report_literals_not_allowed(&l.to_string(), lit)) |
49 | | - } |
50 | | - syn::Lit::Bool(ref l) => { |
51 | | - res.error = Some(report_literals_not_allowed(&l.value().to_string(), lit)) |
52 | | - } |
53 | | - syn::Lit::Verbatim(ref l) => { |
54 | | - res.error = Some(report_literals_not_allowed(&l.to_string(), lit)) |
55 | | - } |
56 | | - }, |
57 | | - } |
58 | | - } |
| 7 | +use canyon_migrations::migrations::handler::Migrations; |
59 | 8 |
|
60 | | - res |
61 | | -} |
62 | | - |
63 | | -/// Determines whenever a [`syn::NestedMeta::Meta`] it's classified as a valid argument of the `canyon` macro |
64 | | -fn determine_allowed_attributes(meta: &syn::Meta, cma: &mut CanyonMacroAttributes) { |
65 | | - const ALLOWED_ATTRS: [&str; 1] = ["enable_migrations"]; |
66 | | - |
67 | | - let attr_ident = meta.path().get_ident().unwrap(); |
68 | | - let attr_ident_str = attr_ident.to_string(); |
69 | | - |
70 | | - if attr_ident_str.as_str() == "enable_migrations" { |
71 | | - cma.allowed_migrations = true; |
72 | | - } else { |
73 | | - let error = syn::Error::new_spanned( |
74 | | - Ident::new(&attr_ident_str, attr_ident.span()), |
75 | | - format!( |
76 | | - "No `{attr_ident_str}` arguments allowed in the `Canyon` macro attributes.\n\ |
77 | | - Allowed ones are: {ALLOWED_ATTRS:?}" |
78 | | - ), |
79 | | - ) |
80 | | - .into_compile_error(); |
81 | | - cma.error = Some( |
82 | | - quote! { |
83 | | - #error |
84 | | - fn main() {} |
85 | | - } |
86 | | - .into(), |
87 | | - ) |
88 | | - } |
89 | | -} |
90 | | - |
91 | | -/// Creates a custom error for report not allowed literals on the attribute |
92 | | -/// args of the `canyon` proc macro |
93 | | -fn report_literals_not_allowed(ident: &str, s: &Lit) -> TokenStream1 { |
94 | | - let error = syn::Error::new_spanned( |
95 | | - Ident::new(ident, s.span()), |
96 | | - "No literals allowed in the `Canyon` macro", |
97 | | - ) |
98 | | - .into_compile_error(); |
| 9 | +#[cfg(feature = "migrations")] |
| 10 | +pub fn main_with_queries() -> TokenStream { |
| 11 | + CANYON_TOKIO_RUNTIME.block_on(async { |
| 12 | + canyon_connection::init_connections_cache().await; |
| 13 | + Migrations::migrate().await; |
| 14 | + }); |
99 | 15 |
|
| 16 | + // The queries to execute at runtime in the managed state |
| 17 | + let mut queries_tokens: Vec<TokenStream> = Vec::new(); |
| 18 | + wire_queries_to_execute(&mut queries_tokens); |
100 | 19 | quote! { |
101 | | - #error |
102 | | - fn main() {} |
| 20 | + { |
| 21 | + #(#queries_tokens)* |
| 22 | + } |
103 | 23 | } |
104 | | - .into() |
105 | 24 | } |
106 | 25 |
|
107 | 26 | /// Creates a TokenScream that is used to load the data generated at compile-time |
108 | 27 | /// by the `CanyonManaged` macros again on the queries register |
109 | | -pub fn wire_queries_to_execute(canyon_manager_tokens: &mut Vec<TokenStream>) { |
| 28 | +#[cfg(feature = "migrations")] |
| 29 | +fn wire_queries_to_execute(canyon_manager_tokens: &mut Vec<TokenStream>) { |
110 | 30 | let cm_data = CM_QUERIES_TO_EXECUTE.lock().unwrap(); |
111 | 31 | let data = QUERIES_TO_EXECUTE.lock().unwrap(); |
112 | 32 |
|
|
0 commit comments