Skip to content

Commit

Permalink
feat(ts-bindgen-build-support): usable and flexible with_web_sys_type…
Browse files Browse the repository at this point in the history
…s macro
  • Loading branch information
abrgr committed Feb 6, 2022
1 parent c59906f commit 584ca33
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 27 deletions.
22 changes: 16 additions & 6 deletions Cargo.lock

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

2 changes: 2 additions & 0 deletions ts-bindgen-build-support/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -16,3 +16,5 @@ proc-macro = true
[dependencies]
web-sys = "0.3.53"
cargo-whatfeatures = "0.9.7"
quote = "1.0"
proc-macro2 = "1.0.36"
47 changes: 27 additions & 20 deletions ts-bindgen-build-support/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,33 +1,40 @@
extern crate proc_macro;

use cargo_whatfeatures::Registry;
use proc_macro::{Ident, Span, TokenStream, Punct, Spacing, TokenTree};
use std::iter::Iterator;
use proc_macro::TokenStream;
use proc_macro2::{Ident, Span, TokenStream as TokenStream2};
use quote::quote;

#[proc_macro]
pub fn web_sys_types(_item: TokenStream) -> TokenStream {
pub fn with_web_sys_types(form: TokenStream) -> TokenStream {
let types = _web_sys_types();

types
.iter()
.map(|t| Ident::new(t, Span::mixed_site()))
.flat_map(|id| {
Box::new(
[id.into(), Punct::new(',', Spacing::Alone).into()].into_iter()
) as Box<dyn Iterator<Item = TokenTree>>
})
.collect()
let types = types.iter().map(|t| Ident::new(t, Span::call_site()));

let form: TokenStream2 = form.into();

quote! {
macro_rules! __with_web_sys_types {
($($item:ident),*) => {
#form
}
}

__with_web_sys_types!(#(#types),*);
}
.into()
}

fn _web_sys_types() -> Vec<String> {
let workspace =
Registry::from_local()
.unwrap()
.maybe_latest("web-sys")
.unwrap()
.get_features()
.unwrap();
workspace.map.values()
let workspace = Registry::from_local()
.unwrap()
.maybe_latest("web-sys")
.unwrap()
.get_features()
.unwrap();
workspace
.map
.values()
.flat_map(|features| features.features.keys())
.cloned()
.collect()
Expand Down
3 changes: 2 additions & 1 deletion ts-bindgen-gen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -21,8 +21,9 @@ serde_json = "1.0.59"
proc-macro2 = "1.0.24"
unicode-xid = "0.2.2"
heck = "0.3.3"
strum_macros = "0.21.1"
strum_macros = "0.23.0"
enum_to_enum = "0.1.0"
ts-bindgen-build-support = { version = "0.1.0", path = "../ts-bindgen-build-support" }

[dev-dependencies]
tempfile = "3.2.0"
Expand Down

0 comments on commit 584ca33

Please sign in to comment.