use darling::FromMeta;
use proc_macro2::Ident;
use proc_macro2::TokenStream;
use quote::quote;
use crate::paths;
#[derive(FromMeta)]
pub(super) enum SingletonLock {
#[darling(rename = "mutex")]
Mutex,
#[darling(rename = "rwlock")]
RwLock,
}
impl Default for SingletonLock {
fn default() -> Self {
Self::Mutex
}
}
impl SingletonLock {
pub fn to_type(&self, inner: &Ident) -> TokenStream {
let tokio = paths::tokio_path();
match self {
Self::Mutex => quote! { #tokio::sync::Mutex<#inner> },
Self::RwLock => quote! { #tokio::sync::RwLock<#inner> },
}
}
pub fn to_new_lock_expr(&self, inner: &Ident) -> TokenStream {
let tokio = paths::tokio_path();
match self {
Self::Mutex => quote! { #tokio::sync::Mutex::new(#inner) },
Self::RwLock => quote! { #tokio::sync::RwLock::new(#inner) },
}
}
pub fn to_guard(&self, lock: &Ident) -> TokenStream {
match self {
Self::Mutex => quote! { #lock.lock().await },
Self::RwLock => quote! { #lock.read().await },
}
}
pub fn to_mut_guard(&self, lock: &Ident) -> TokenStream {
match self {
Self::Mutex => quote! { #lock.lock().await },
Self::RwLock => quote! { #lock.write().await },
}
}
}