Skip to content

Commit

Permalink
feat: Accept async upgradeneeded closures
Browse files Browse the repository at this point in the history
Closes #30
  • Loading branch information
Alorel committed Oct 27, 2024
1 parent dac75a6 commit a522c42
Show file tree
Hide file tree
Showing 18 changed files with 737 additions and 195 deletions.
3 changes: 3 additions & 0 deletions .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -138,6 +138,9 @@ jobs:
- --features "indices cursors streams serde"
- --features "dates serde"
- --features "cursors serde"
- --features "async-upgrade"
- --features "tx-done"
- --features "async-upgrade tx-done"

done:
name: All tests
Expand Down
77 changes: 59 additions & 18 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 6 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ include = [
]

[features]
async-upgrade = []
cursors = [
"web-sys/IdbCursor",
"web-sys/IdbCursorWithValue",
Expand All @@ -47,10 +48,12 @@ serde = [
]
streams = [
"dep:futures-core",
"wasm_evt_listener/streams",
]
switch = []
tx-done = ["dep:wasm_evt_listener"]
typed-arrays = []
version-change = ["tokio/macros"]
version-change = ["tokio/macros", "dep:wasm_evt_listener"]
_serialise-deserialise-dyn = []

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
Expand Down Expand Up @@ -81,6 +84,7 @@ uuid = "1.8"
wasm-bindgen = "0.2.95"
wasm-bindgen-futures = "0.4.45"
wasm-bindgen-test = "0.3.45"
wasm_evt_listener = "0.1"
web-time = "1.1"
web-sys = "0.3.72"

Expand All @@ -105,6 +109,7 @@ thiserror = { workspace = true }
tokio = { workspace = true, features = ["sync"], default-features = false }
wasm-bindgen = { workspace = true }
wasm-bindgen-futures = { workspace = true }
wasm_evt_listener = { workspace = true, optional = true }
web-time = { workspace = true, optional = true }

[dependencies.internal_macros]
Expand Down
85 changes: 65 additions & 20 deletions internal_macros/src/generic_bounds.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,32 @@
use crate::commons::FnTarget;
use crate::TokenStream1;
use macroific::prelude::*;
use proc_macro2::Ident;
use quote::ToTokens;
use syn::punctuated::Punctuated;
use syn::{parse_quote, Error, WherePredicate};

#[inline]
pub(super) fn exec(spec: TokenStream1, target: TokenStream1) -> TokenStream1 {
match syn::parse::<Opts>(spec) {
Ok(opts) => match syn::parse::<FnTarget>(target.clone()) {
Ok(mut target) => {
opts.extend_target(&mut target);
target.into_token_stream().into()
}
Err(e) => on_err(target, e),
},
Err(e) => on_err(target, e),
}
}

macro_rules! make_opts {
($struct_name: ident => { $($($opt: ident)|+ => $predicate: ty),+ $(,)? }) => {
/// Options list
($struct_name: ident => {
$($($opt: ident)|+ => $predicate: ty),+
$(,[custom] => {
$($extra_opt: ident => $extra_ty: ty),+ $(,)?
})? $(,)?
}) => {
/// # Options list
///
/// | Option | Predicate |
/// |--------|-----------|
$($(#[doc = concat!(" | `", stringify!($opt), "` | `", stringify!($predicate), "` |")])+)+
///
/// # Extras
///
/// | Option | Type |
/// |--------|-----------|
$($(#[doc = concat!(" | `", stringify!($extra_opt), "` | `", stringify!($extra_ty), "` |")])+)+
#[derive(::macroific::attr_parse::AttributeOptions)]
pub(super) struct $struct_name {
$($($opt: ::syn::punctuated::Punctuated<proc_macro2::Ident, ::syn::Token![,]>),+),+
$($($opt: ::syn::punctuated::Punctuated<proc_macro2::Ident, ::syn::Token![,]>,)+)+
$($($extra_opt: Option<$extra_ty>,)+)?
}

impl ::syn::parse::Parse for $struct_name {
Expand All @@ -42,6 +41,9 @@ macro_rules! make_opts {
$($(if !self.$opt.is_empty() {
extend_generics(self.$opt, target, ::quote::quote!($predicate));
})+)+
$($(if let Some($extra_opt) = self.$extra_opt {
$extra_opt.extend_target(target);
})+)?
}
}
};
Expand All @@ -52,18 +54,61 @@ make_opts!(Opts => {
db_version => crate::factory::DBVersion,
blocked_cb => ::core::ops::FnOnce(crate::database::VersionChangeEvent) -> crate::Result<()> + 'static,
upgrade_cb => ::core::ops::FnOnce(crate::database::VersionChangeEvent, crate::database::Database) -> crate::Result<()> + 'static,
[custom] => {
upgrade_async_cb => UpgradeAsyncCb,
},
});

#[inline]
pub(super) fn exec(spec: TokenStream1, target: TokenStream1) -> TokenStream1 {
match syn::parse::<Opts>(spec) {
Ok(opts) => match syn::parse::<FnTarget>(target.clone()) {
Ok(mut target) => {
opts.extend_target(&mut target);
target.into_token_stream().into()
}
Err(e) => on_err(target, e),
},
Err(e) => on_err(target, e),
}
}

#[derive(ParseOption)]
struct UpgradeAsyncCb {
#[attr_opts(default = false)]
fun: Ident,

#[attr_opts(default = false)]
fut: Ident,
}

impl UpgradeAsyncCb {
fn extend_target(self, target: &mut FnTarget) {
let Self { fun, fut } = self;
let wheres = [
parse_quote!(#fun: ::core::ops::FnOnce(crate::database::VersionChangeEvent, crate::database::Database) -> #fut + 'static),
parse_quote!(#fut: ::core::future::Future<Output = crate::Result<()>> + 'static),
];

target
.generics_mut()
.make_where_clause()
.predicates
.extend::<[WherePredicate; 2]>(wheres);
}
}

fn on_err(mut target: TokenStream1, e: Error) -> TokenStream1 {
let e: TokenStream1 = e.into_compile_error().into();
target.extend(e);
target
}

fn extend_generics<T, I, P>(idents: Punctuated<I, P>, target: &mut FnTarget, ext_with: T)
fn extend_generics<T, Iter, Item>(idents: Iter, target: &mut FnTarget, ext_with: T)
where
T: ToTokens,
I: ToTokens,
Item: ToTokens,
Iter: IntoIterator<Item = Item>,
{
let iter = idents
.into_iter()
Expand Down
Loading

0 comments on commit a522c42

Please sign in to comment.