-
Notifications
You must be signed in to change notification settings - Fork 16
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Generate unique reply_ids (#427)
- Loading branch information
Showing
8 changed files
with
126 additions
and
17 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,60 @@ | ||
use convert_case::{Case, Casing}; | ||
use itertools::Itertools; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::{GenericParam, Ident}; | ||
|
||
use crate::types::msg_variant::MsgVariants; | ||
|
||
pub struct Reply<'a> { | ||
variants: &'a MsgVariants<'a, GenericParam>, | ||
} | ||
|
||
impl<'a> Reply<'a> { | ||
pub fn new(variants: &'a MsgVariants<'a, GenericParam>) -> Self { | ||
Self { variants } | ||
} | ||
|
||
pub fn emit(&self) -> TokenStream { | ||
let unique_handlers = self.emit_reply_ids(); | ||
|
||
quote! { | ||
#(#unique_handlers)* | ||
} | ||
} | ||
|
||
fn emit_reply_ids(&self) -> impl Iterator<Item = TokenStream> + 'a { | ||
self.variants | ||
.as_reply_ids() | ||
.enumerate() | ||
.map(|(id, reply_id)| { | ||
let id = id as u64; | ||
|
||
quote! { | ||
pub const #reply_id : u64 = #id ; | ||
} | ||
}) | ||
} | ||
} | ||
|
||
trait ReplyVariants<'a> { | ||
fn as_reply_ids(&'a self) -> impl Iterator<Item = Ident> + 'a; | ||
} | ||
|
||
impl<'a> ReplyVariants<'a> for MsgVariants<'a, GenericParam> { | ||
fn as_reply_ids(&'a self) -> impl Iterator<Item = Ident> + 'a { | ||
self.variants() | ||
.flat_map(|variant| { | ||
if variant.msg_attr().handlers().is_empty() { | ||
return vec![variant.function_name()]; | ||
} | ||
variant.msg_attr().handlers().iter().collect() | ||
}) | ||
.unique() | ||
.map(|handler| { | ||
let reply_id = | ||
format! {"{}_REPLY_ID", handler.to_string().to_case(Case::UpperSnake)}; | ||
Ident::new(&reply_id, handler.span()) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters