-
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
104 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,45 @@ | ||
use convert_case::{Case, Casing}; | ||
use itertools::Itertools; | ||
use proc_macro2::TokenStream; | ||
use quote::quote; | ||
use syn::{GenericParam, Ident}; | ||
|
||
use crate::types::msg_variant::{MsgVariant, 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 | ||
.variants() | ||
.flat_map(|variant| variant.msg_attr().handlers()) | ||
.chain(self.variants.variants().map(MsgVariant::function_name)) | ||
.unique() | ||
.enumerate() | ||
.map(|handler| { | ||
let (id, handler) = handler; | ||
let reply_id = | ||
format! {"{}_REPLY_ID", handler.to_string().to_case(Case::UpperSnake)}; | ||
let reply_id = Ident::new(&reply_id, handler.span()); | ||
let id = id as u64; | ||
|
||
quote! { | ||
pub const #reply_id : u64 = #id ; | ||
} | ||
}) | ||
} | ||
} |
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