Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: Auto convert macro #22

Merged
merged 15 commits into from
Jan 2, 2025
20 changes: 15 additions & 5 deletions Cargo.lock

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

9 changes: 5 additions & 4 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,24 +11,25 @@ bytes = { version = "1", features = ["serde"] }
uuid = { version = "1", features = ["serde", "v4"] }
futures = { version = "0.3", default-features = false, features = ["std"] }
serde = { version = "1.0", features = ["derive", "rc"] }
indexmap = { version = "2.2.6", features = ["serde"] }
indexmap = { version = "2.7.0", features = ["serde"] }

# warp
warp-ipfs = { git = "https://github.com/Satellite-im/Warp.git", rev = "fdb96da58174b41548992b3dcaba40a01b10ecf9"}
warp = { git = "https://github.com/Satellite-im/Warp.git", rev = "fdb96da58174b41548992b3dcaba40a01b10ecf9"}

# wasm
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-wasm = "0.2.0"
tracing-wasm = "0.2.1"
console_error_panic_hook = "0.1.7"
wasm-bindgen = "0.2"
wasm-bindgen-futures = "0.4"
serde-wasm-bindgen = "0.4"
serde-wasm-bindgen = "0.6"
wasm-streams = "0.4"
send_wrapper = "0.6.0"
web-sys = "0.3"
js-sys = "0.3"
tsify-next = "0.5.4"
macro_utils = { path = "./macro-utils" }

# examples
tiny_file_server = "0.1.5"
tiny_file_server = "0.1.5"
12 changes: 12 additions & 0 deletions macro-utils/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
[package]
name = "macro_utils"
version = "0.1.0"
edition = "2021"

[dependencies]
syn = { version = "2.0.87", features = ["derive", "full"] }
quote = "1.0.37"
proc-macro2 = "1.0.89"

[lib]
proc-macro = true
66 changes: 66 additions & 0 deletions macro-utils/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,66 @@
extern crate proc_macro2;

use proc_macro::TokenStream;

mod wasm_convert;

/// Easy type conversion between enums and structs of similar structure
///
/// Also allows specifying a function for conversion for types not implementing From already
///
/// Fields can accept an attribute with name "from" or "into" representing a conversion function.
///
/// This value can either be a path to a function or some rust code.
///
/// The path need to accept a single value of the current type and output the target type
///
/// Unnamed enum fields are referenced as f_0, f_1...
/// E.g. `"{f_0}.converter_function()"`
/// ```
/// use macro_utils::FromTo;
///
/// #[derive(Debug)]
/// pub enum A {
/// SomeValue1,
/// SomeValue2
/// }
///
/// #[derive(FromTo, PartialEq, Debug)]
/// #[from_to(A)]
/// pub enum B {
/// SomeValue1,
/// SomeValue2
/// }
///
/// assert_eq!(A::SomeValue1, B::SomeValue1.into());
///
/// pub struct S1 {
/// x: i64,
/// y: i128
/// }
///
/// #[derive(FromTo)]
/// #[from_to(A)]
/// pub struct S2 {
/// x: i64,
/// y: i128
/// }
///
/// #[derive(FromTo)]
/// #[from_to(A)]
/// pub struct S3 {
/// // Pass in a custom conversion function. If not provided will use a From implementation
/// #[from_to(from = "converter_function_from", to = "converter_function_to")]
/// // Referencing the struct itself is also possible
/// #[from_to(from = "{value}.x.converter_function()", to = "{value}.x.converter_function()")]
/// x: String,
/// #[from_to(from = "converter_function_from", to = "converter_function_to")]
/// y: String
/// }
/// ```
#[proc_macro_derive(FromTo, attributes(from_to))]
pub fn from_to(input: TokenStream) -> TokenStream {
wasm_convert::expand(input)
.unwrap_or_else(|e| e.into_compile_error().into())
.into()
}
Loading
Loading