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
mod macro_args;

use crate::macro_args::MacroArgs;
use darling::FromMeta;
use proc_macro::TokenStream;
use quote::quote;
use syn::{AttributeArgs, ItemFn};

/// _⚠ Use with caution - this macro panics on error - See `Panics` section below._
///
/// Wraps the annotated function with a blocking lock that is released when
/// the function is returned.
///
/// # Args:
/// - `name`: The name of the lock. Can be any relative / absolute path.
/// - `absolute`: Indicates whether the provided `name` should be created at the [`temp_dir`](std::env::temp_dir())
/// or as an absolute path (at the root directory). Default is `false`.
/// # Example
/// ```rust
/// use proclock_macro::proclock;
///
/// #[proclock(name = "my_lock.lock", absolute = false)]
/// fn my_locked_function() {}
/// ```
/// # Panics
/// This macro will panic if the underlying locking function call fails.
#[proc_macro_attribute]
pub fn proclock(args: TokenStream, input: TokenStream) -> TokenStream {
    let options = syn::parse_macro_input!(args as AttributeArgs);
    let function = syn::parse_macro_input!(input as ItemFn);

    let MacroArgs { name, absolute } = match MacroArgs::from_list(&options) {
        Ok(v) => v,
        Err(e) => return TokenStream::from(e.write_errors()),
    };

    let locking_code = quote! {
        use proclock_api::{lock, try_lock, LockPath};
        let lock_path = if #absolute {
            LockPath::FullPath(#name)
        } else {
            LockPath::Tmp(#name)
        };

        let _guard = lock(&lock_path).unwrap();
    };

    let ItemFn {
        attrs,
        vis,
        sig,
        block: body,
    } = &function;

    let result = quote! {
        #(#attrs)*
        #vis #sig {
            #locking_code
            #body
        }
    };

    let res: proc_macro::TokenStream = result.into();
    res
}