Skip to content

Commit

Permalink
feat: Auto convert macro (#22)
Browse files Browse the repository at this point in the history
* add macro easing from implementations

* update macro

* update docs

* allow function strings instead of just paths

* docs

* update dependencies

* auto from/to for (nearly) all contents. also improve asynciterator results

* change serde

* docs formatting

* update enums

* typing for attachmentkind

* Update src/warp/raygun.rs

Co-authored-by: Darius Clark <[email protected]>

* Update src/warp/multipass.rs

Co-authored-by: Darius Clark <[email protected]>

* Merge branch 'main' into auto-convert-macro

---------

Co-authored-by: Flemmli97 <[email protected]>
Co-authored-by: Darius Clark <[email protected]>
  • Loading branch information
3 people authored Jan 2, 2025
1 parent f12b0a4 commit d21e269
Show file tree
Hide file tree
Showing 8 changed files with 922 additions and 557 deletions.
10 changes: 10 additions & 0 deletions Cargo.lock

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

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ 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"}
Expand All @@ -29,6 +29,7 @@ send_wrapper = "0.6.0"
web-sys = "0.3.76"
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

0 comments on commit d21e269

Please sign in to comment.