Skip to content

Commit

Permalink
Introduces macro for RadiantMessage
Browse files Browse the repository at this point in the history
  • Loading branch information
codeneix committed Nov 5, 2023
1 parent 61ddaa9 commit 4783a35
Show file tree
Hide file tree
Showing 2 changed files with 52 additions and 18 deletions.
50 changes: 50 additions & 0 deletions crates/macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -212,6 +212,47 @@ fn combine_enum_internal(
Ok(res)
}

fn derive_message_internal(item: TokenStream2) -> syn::Result<TokenStream2> {
let item = syn::parse2::<syn::ItemEnum>(item)?;

let name = item.ident.clone();
let message_names = item
.variants
.iter()
.map(|variant| variant.ident.clone())
.collect::<Vec<_>>();
let messages = item.variants.iter().map(|variant| {
let fields = variant.fields.iter();
quote! {
#(#fields)*
}
}).collect::<Vec<_>>();

let res = quote! {
#(
impl From<#messages> for #name {
fn from(message: #messages) -> Self {
Self::#message_names(message)
}
}
)*

#(
impl TryFrom<#name> for #messages {
type Error = ();

fn try_from(message: #name) -> Result<Self, Self::Error> {
match message {
#name::#message_names(message) => Ok(message),
_ => Err(()),
}
}
}
)*
};
Ok(res)
}

#[proc_macro_derive(RadiantTessellatable)]
pub fn derive_tessellatable(item: TokenStream) -> TokenStream {
let res = match derive_tessellatable_internal(item.into()) {
Expand Down Expand Up @@ -249,3 +290,12 @@ pub fn combine_enum(attr: TokenStream, item: TokenStream) -> TokenStream {
};
res.into()
}

#[proc_macro_derive(RadiantMessage)]
pub fn derive_message(item: TokenStream) -> TokenStream {
let res = match derive_message_internal(item.into()) {
Ok(res) => res,
Err(err) => err.to_compile_error(),
};
res.into()
}
20 changes: 2 additions & 18 deletions crates/runtime/src/message.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,10 @@
use radiant_core::{RadiantSceneMessage, RadiantSceneResponse};
use radiant_macros::combine_enum;
use radiant_macros::{combine_enum, RadiantMessage};

use crate::RadiantNodeType;
use serde::{Deserialize, Serialize};

#[derive(RadiantMessage)]
#[combine_enum(radiant_core::RadiantRectangleMessage)]
#[combine_enum(radiant_image_node::RadiantImageMessage)]
#[combine_enum(radiant_text_node::RadiantTextMessage)]
Expand All @@ -24,23 +25,6 @@ pub enum RadiantResponse {
NoOp,
}

impl From<RadiantSceneMessage> for RadiantMessage {
fn from(message: RadiantSceneMessage) -> Self {
Self::SceneMessage(message)
}
}

impl TryFrom<RadiantMessage> for RadiantSceneMessage {
type Error = ();

fn try_from(message: RadiantMessage) -> Result<Self, Self::Error> {
match message {
RadiantMessage::SceneMessage(message) => Ok(message),
_ => Err(()),
}
}
}

impl From<RadiantSceneResponse<RadiantMessage, RadiantNodeType>> for RadiantResponse {
fn from(response: RadiantSceneResponse<RadiantMessage, RadiantNodeType>) -> Self {
match response {
Expand Down

0 comments on commit 4783a35

Please sign in to comment.