mirror of
https://github.com/allankoechke/turso.git
synced 2026-11-30 11:12:58 +00:00
enhance proc macro to detect the fn args and the return type with better spans
This commit is contained in:
@@ -1,17 +1,48 @@
|
||||
use std::collections::HashSet;
|
||||
use std::{collections::HashSet, ops::Deref};
|
||||
|
||||
use proc_macro::TokenStream;
|
||||
use proc_macro2::Span;
|
||||
use quote::{quote, quote_spanned, ToTokens};
|
||||
use syn::{
|
||||
parse::{Parse, ParseStream},
|
||||
parse_macro_input,
|
||||
punctuated::Punctuated,
|
||||
ItemFn, Meta, Token,
|
||||
spanned::Spanned,
|
||||
ItemFn, Meta, Pat, ReturnType, Token, Type,
|
||||
};
|
||||
|
||||
#[derive(Debug, Clone, Copy)]
|
||||
struct SpannedType<T>(T, Span);
|
||||
|
||||
impl<T> SpannedType<T> {
|
||||
fn map<U>(self, func: impl FnOnce(T) -> U) -> SpannedType<U> {
|
||||
SpannedType(func(self.0), self.1)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> Deref for SpannedType<T> {
|
||||
type Target = T;
|
||||
|
||||
fn deref(&self) -> &Self::Target {
|
||||
&self.0
|
||||
}
|
||||
}
|
||||
|
||||
impl<T: ToTokens> ToTokens for SpannedType<T> {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
let span = self.1;
|
||||
let val = &self.0;
|
||||
let out_tokens = quote_spanned! {span=>
|
||||
#val
|
||||
};
|
||||
out_tokens.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
struct Args {
|
||||
path: Option<String>,
|
||||
mvcc: bool,
|
||||
path: Option<SpannedType<String>>,
|
||||
mvcc: Option<SpannedType<()>>,
|
||||
}
|
||||
|
||||
impl Parse for Args {
|
||||
@@ -20,7 +51,7 @@ impl Parse for Args {
|
||||
let mut seen_args = HashSet::new();
|
||||
|
||||
let mut path = None;
|
||||
let mut mvcc = false;
|
||||
let mut mvcc = None;
|
||||
|
||||
let errors = args
|
||||
.into_iter()
|
||||
@@ -34,7 +65,7 @@ impl Parse for Args {
|
||||
..
|
||||
}) = &nv.value
|
||||
{
|
||||
path = Some(lit_str.value());
|
||||
path = Some(SpannedType(lit_str.value(), nv.value.span()));
|
||||
seen_args.insert(ident.unwrap().clone());
|
||||
} else {
|
||||
return Some(syn::Error::new_spanned(
|
||||
@@ -49,7 +80,7 @@ impl Parse for Args {
|
||||
Meta::Path(p) => {
|
||||
let ident = p.get_ident();
|
||||
if p.is_ident("mvcc") {
|
||||
mvcc = true;
|
||||
mvcc = Some(SpannedType((), p.span()));
|
||||
seen_args.insert(ident.unwrap().clone());
|
||||
} else {
|
||||
return Some(syn::Error::new_spanned(p, "unexpected flag"));
|
||||
@@ -75,17 +106,156 @@ impl Parse for Args {
|
||||
}
|
||||
}
|
||||
|
||||
struct DatabaseFunction {
|
||||
input: ItemFn,
|
||||
tmp_db_fn_arg: Pat,
|
||||
args: Args,
|
||||
}
|
||||
|
||||
impl DatabaseFunction {
|
||||
fn new(input: ItemFn, tmp_db_fn_arg: Pat, args: Args) -> Self {
|
||||
Self {
|
||||
input,
|
||||
tmp_db_fn_arg,
|
||||
args,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ToTokens for DatabaseFunction {
|
||||
fn to_tokens(&self, tokens: &mut proc_macro2::TokenStream) {
|
||||
let ItemFn {
|
||||
attrs,
|
||||
vis,
|
||||
sig,
|
||||
block,
|
||||
} = &self.input;
|
||||
|
||||
let fn_name = &sig.ident;
|
||||
let fn_generics = &sig.generics;
|
||||
|
||||
// Check the return type
|
||||
let is_result = is_result(&sig.output);
|
||||
|
||||
let db_path = self.args.path.clone().map_or_else(
|
||||
|| {
|
||||
let name = format!("{fn_name}.db");
|
||||
quote! {#name}
|
||||
},
|
||||
|path| path.to_token_stream(),
|
||||
);
|
||||
|
||||
let mut db_opts = quote! {
|
||||
turso_core::DatabaseOpts::new()
|
||||
.with_indexes(true)
|
||||
.with_index_method(true)
|
||||
.with_encryption(true)
|
||||
};
|
||||
|
||||
if let Some(spanned) = self
|
||||
.args
|
||||
.mvcc
|
||||
.map(|val| val.map(|_| quote! {.with_mvcc(true)}))
|
||||
{
|
||||
db_opts = quote! {
|
||||
#db_opts
|
||||
#spanned
|
||||
}
|
||||
}
|
||||
|
||||
let arg_name = &self.tmp_db_fn_arg;
|
||||
let fn_out = &sig.output;
|
||||
|
||||
eprintln!("db_path: {db_path}\ndb_opts: {db_opts}");
|
||||
|
||||
let call_func = if is_result {
|
||||
quote! {(|#arg_name| #fn_out #block)(#arg_name).unwrap();}
|
||||
} else {
|
||||
quote! {(|#arg_name| #block)(#arg_name);}
|
||||
};
|
||||
|
||||
let out = quote! {
|
||||
#[test]
|
||||
#(#attrs)*
|
||||
#vis fn #fn_name #fn_generics() {
|
||||
let #arg_name = crate::common::TempDatabase::new_with_opts(#db_path, #db_opts);
|
||||
|
||||
#call_func
|
||||
}
|
||||
};
|
||||
out.to_tokens(tokens);
|
||||
}
|
||||
}
|
||||
|
||||
pub fn test_macro_attribute(args: TokenStream, input: TokenStream) -> TokenStream {
|
||||
eprintln!("args: {args} \ninput : {input}");
|
||||
let input = parse_macro_input!(input as ItemFn);
|
||||
|
||||
let mut args = parse_macro_input!(args as Args);
|
||||
let args = parse_macro_input!(args as Args);
|
||||
|
||||
dbg!(&args);
|
||||
|
||||
quote::quote! {
|
||||
#[test]
|
||||
#input
|
||||
}
|
||||
.into()
|
||||
let tmp_db_arg = match check_fn_inputs(&input) {
|
||||
Ok(fn_arg) => fn_arg,
|
||||
Err(err) => return err.into_compile_error().into(),
|
||||
};
|
||||
|
||||
let db_function = DatabaseFunction::new(input, tmp_db_arg, args);
|
||||
|
||||
db_function.to_token_stream().into()
|
||||
}
|
||||
|
||||
fn check_fn_inputs(input: &ItemFn) -> syn::Result<Pat> {
|
||||
let msg = "Only 1 function argument can be passed and it must be of type `TempDatabase`";
|
||||
let args = &input.sig.inputs;
|
||||
if args.len() != 1 {
|
||||
return Err(syn::Error::new_spanned(&input.sig, msg));
|
||||
}
|
||||
let first = args.first().unwrap();
|
||||
match first {
|
||||
syn::FnArg::Receiver(receiver) => return Err(syn::Error::new_spanned(receiver, msg)),
|
||||
syn::FnArg::Typed(pat_type) => {
|
||||
if let Type::Path(type_path) = pat_type.ty.as_ref() {
|
||||
// Check if qself is None (not a qualified path like <T as Trait>::Type)
|
||||
if type_path.qself.is_some() {
|
||||
return Err(syn::Error::new_spanned(type_path, msg));
|
||||
}
|
||||
|
||||
// Get the last segment of the path
|
||||
// This works for both:
|
||||
// - Simple: TempDatabase
|
||||
// - Qualified: crate::TempDatabase, my_module::TempDatabase
|
||||
if !type_path
|
||||
.path
|
||||
.segments
|
||||
.last()
|
||||
.is_some_and(|segment| segment.ident == "TempDatabase")
|
||||
{
|
||||
return Err(syn::Error::new_spanned(type_path, msg));
|
||||
}
|
||||
Ok(*(pat_type.pat.clone()))
|
||||
} else {
|
||||
return Err(syn::Error::new_spanned(pat_type, msg));
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn is_result(return_type: &ReturnType) -> bool {
|
||||
match return_type {
|
||||
ReturnType::Default => false, // Returns ()
|
||||
ReturnType::Type(_, ty) => {
|
||||
// Check if the type path contains "Result"
|
||||
if let syn::Type::Path(type_path) = ty.as_ref() {
|
||||
type_path
|
||||
.path
|
||||
.segments
|
||||
.last()
|
||||
.map(|seg| seg.ident == "Result")
|
||||
.unwrap_or(false)
|
||||
} else {
|
||||
false
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -7,7 +7,6 @@ use tempfile::TempDir;
|
||||
use tracing_subscriber::{layer::SubscriberExt, util::SubscriberInitExt, EnvFilter};
|
||||
use turso_core::{Connection, Database, Row, StepResult, IO};
|
||||
|
||||
|
||||
pub struct TempDatabase {
|
||||
pub path: PathBuf,
|
||||
pub io: Arc<dyn IO + Send>,
|
||||
|
||||
@@ -6,8 +6,8 @@ use std::sync::{Arc, Mutex};
|
||||
use turso_core::{Connection, LimboError, Result, StepResult};
|
||||
|
||||
#[allow(clippy::arc_with_non_send_sync)]
|
||||
#[turso_macros::test(mvcc, path = 1)]
|
||||
fn test_wal_checkpoint_result() -> Result<()> {
|
||||
#[turso_macros::test(mvcc, path = "x.d")]
|
||||
fn test_wal_checkpoint_result(tmp: TempDatabase) -> Result<()> {
|
||||
maybe_setup_tracing();
|
||||
let tmp_db = TempDatabase::new("test_wal.db");
|
||||
let conn = tmp_db.connect_limbo();
|
||||
|
||||
Reference in New Issue
Block a user