-
Notifications
You must be signed in to change notification settings - Fork 1.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
lang: Add
Event
utility type to get events from bytes (#2897)
- Loading branch information
1 parent
c7ccbb8
commit 0be5b00
Showing
8 changed files
with
143 additions
and
1 deletion.
There are no files selected for viewing
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 |
---|---|---|
|
@@ -6,5 +6,6 @@ pub mod events; | |
pub mod internal; | ||
pub mod program; | ||
pub mod types; | ||
pub mod utils; | ||
|
||
use super::common; |
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,69 @@ | ||
use anchor_idl::types::Idl; | ||
use quote::{format_ident, quote}; | ||
|
||
use super::common::gen_discriminator; | ||
|
||
pub fn gen_utils_mod(idl: &Idl) -> proc_macro2::TokenStream { | ||
let event = gen_event(idl); | ||
|
||
quote! { | ||
/// Program utilities. | ||
pub mod utils { | ||
#event | ||
} | ||
} | ||
} | ||
|
||
fn gen_event(idl: &Idl) -> proc_macro2::TokenStream { | ||
let variants = idl | ||
.events | ||
.iter() | ||
.map(|ev| format_ident!("{}", ev.name)) | ||
.map(|name| quote! { #name(#name) }); | ||
let match_arms = idl.events.iter().map(|ev| { | ||
let disc = gen_discriminator(&ev.discriminator); | ||
let name = format_ident!("{}", ev.name); | ||
let event = quote! { | ||
#name::try_from_slice(&value[8..]) | ||
.map(Self::#name) | ||
.map_err(Into::into) | ||
}; | ||
quote! { #disc => #event } | ||
}); | ||
|
||
quote! { | ||
use super::{*, events::*}; | ||
|
||
/// An enum that includes all events of the declared program as a tuple variant. | ||
/// | ||
/// See [`Self::try_from_bytes`] to create an instance from bytes. | ||
pub enum Event { | ||
#(#variants,)* | ||
} | ||
|
||
impl Event { | ||
/// Try to create an event based on the given bytes. | ||
/// | ||
/// This method returns an error if the discriminator of the given bytes don't match | ||
/// with any of the existing events, or if the deserialization fails. | ||
pub fn try_from_bytes(bytes: &[u8]) -> Result<Self> { | ||
Self::try_from(bytes) | ||
} | ||
} | ||
|
||
impl TryFrom<&[u8]> for Event { | ||
type Error = anchor_lang::error::Error; | ||
|
||
fn try_from(value: &[u8]) -> Result<Self> { | ||
if value.len() < 8 { | ||
return Err(ProgramError::InvalidArgument.into()); | ||
} | ||
|
||
match &value[..8] { | ||
#(#match_arms,)* | ||
_ => Err(ProgramError::InvalidArgument.into()), | ||
} | ||
} | ||
} | ||
} | ||
} |
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