ora_client_macros/
lib.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
//! Macros for the ora-client crate.

use proc_macro::TokenStream;

use quote::{quote, ToTokens};
use syn::{parse_macro_input, DeriveInput, Expr};

use darling::FromDeriveInput;

#[derive(FromDeriveInput)]
#[darling(attributes(ora))]
struct JobType {
    #[darling(default = default_output)]
    output: syn::Type,
    #[darling(default)]
    retries: Option<Expr>,
    #[darling(default)]
    timeout: Option<String>,

    #[darling(default)]
    default_labels: Option<syn::Expr>,

    ident: syn::Ident,
}

fn default_output() -> syn::Type {
    syn::parse_quote! { () }
}

impl ToTokens for JobType {
    fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
        let output = &self.output;
        let ident_str = self.ident.to_string();

        let retry_policy = match &self.retries {
            Some(retries) => quote! {
                fn default_retry_policy() -> ora_client::job_definition::RetryPolicy {
                    ora_client::job_definition::RetryPolicy {
                        retries: #retries,
                    }
                }
            },
            None => quote! {},
        };

        let timeout_policy = match &self.timeout {
            Some(timeout) => {
                let timeout_seconds = humantime::parse_duration(timeout).unwrap().as_secs();

                quote! {
                    fn default_timeout_policy() -> ora_client::job_definition::TimeoutPolicy {
                        ora_client::job_definition::TimeoutPolicy {
                            timeout: Some(std::time::Duration::from_secs(#timeout_seconds as _)),
                            base_time: ora_client::job_definition::TimeoutBaseTime::StartTime,
                        }
                    }
                }
            }
            None => quote! {},
        };

        let default_labels = match &self.default_labels {
            Some(default_labels) => quote! {
                fn default_labels() -> std::collections::HashMap<String, String> {
                    #default_labels
                }
            },
            None => quote! {},
        };

        let ident = &self.ident;
        tokens.extend(quote! {
            impl ora_client::JobType for #ident {
                type Output = #output;

                fn id() -> &'static str {
                    #ident_str
                }

                #retry_policy
                #timeout_policy
                #default_labels
            }
        });
    }
}

/// Derive macro for the `JobType` trait.
#[allow(clippy::missing_panics_doc)]
#[proc_macro_derive(JobType, attributes(ora))]
pub fn job_type_derive(input: TokenStream) -> TokenStream {
    let input = parse_macro_input!(input as DeriveInput);

    let job_type = JobType::from_derive_input(&input).unwrap();

    quote! {#job_type}.into()
}