Skip to content

Commit

Permalink
chore: merge pull request #1 from LiterateInk/rust
Browse files Browse the repository at this point in the history
feat: add macros crate
  • Loading branch information
Vexcited authored Jul 23, 2024
2 parents 141f78c + 9d7eedb commit 633a8e4
Show file tree
Hide file tree
Showing 4 changed files with 80 additions and 21 deletions.
38 changes: 23 additions & 15 deletions Cargo.lock

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

23 changes: 17 additions & 6 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,13 +1,23 @@
[workspace]
resolver = "2"
members = ["macros"]

[workspace.package]
license = "MIT"
version = "1.1.0"

[package]
name = "utilities"
version = "1.0.0"
name = "literateink-utilities"
version.workspace = true
authors = ["Mikkel ALMONTE--RINGAUD <[email protected]> (https://github.com/Vexcited)"]
description = "A general package containing utilities for helping building our libraries."
readme = "README.md"
repository = "https://github.com/LiterateInk/Utilities"
description = "A general crate containing utilities for helping building our libraries."
homepage = "https://docs.literate.ink/utilities"
edition = "2021"
repository = "https://github.com/LiterateInk/Utilities"
rust-version = "1.56"
license = "MIT"
license.workspace = true



[dependencies]
futures = "0.3.17"
Expand All @@ -17,3 +27,4 @@ serde-wasm-bindgen = "0.4"
wasm-bindgen = "0.2.86"
wasm-bindgen-futures = "0.4.36"
js-sys = "0.3.63"

14 changes: 14 additions & 0 deletions macros/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "literateink-utilities-macros"
version.workspace = true
edition = "2021"
repository = "https://github.com/LiterateInk/Utilities"
description = "A general crate containing macros for helping building our libraries."
license.workspace = true

[lib]
proc-macro = true

[dependencies]
quote = "1.0.36"
syn = "2.0.72"
26 changes: 26 additions & 0 deletions macros/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use syn::{DeriveInput, parse};
use proc_macro::TokenStream;
use quote::quote;

#[proc_macro_derive(TsifyAsync)]
pub fn tsify_async_macro_derive(input: TokenStream) -> TokenStream {
// Construct a representation of Rust code as a syntax tree
// that we can manipulate
let ast = parse(input).unwrap();

// Build the trait implementation
impl_tsify_async_macro(&ast)
}

fn impl_tsify_async_macro(ast: &DeriveInput) -> TokenStream {
let name = &ast.ident;
let gen = quote! {
impl From<#name> for JsValue {
fn from(value: #name) -> Self {
serde_wasm_bindgen::to_value(&value).unwrap()
}
}
};

gen.into()
}

0 comments on commit 633a8e4

Please sign in to comment.