From cbf6ca58fc20ea5c76fc3174c0ef01d4d602d255 Mon Sep 17 00:00:00 2001 From: Kyle Espinola Date: Wed, 28 Feb 2024 19:23:57 +0100 Subject: [PATCH] refactor: handle optionality outside of parsers --- Cargo.lock | 2 +- plerkle_serialization/Cargo.toml | 2 +- .../src/deserializer/solana.rs | 39 ++++++++----------- 3 files changed, 19 insertions(+), 24 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 10fbfe8..faecf60 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -2168,7 +2168,7 @@ dependencies = [ [[package]] name = "plerkle_serialization" -version = "1.7.0" +version = "1.8.0" dependencies = [ "bs58", "chrono", diff --git a/plerkle_serialization/Cargo.toml b/plerkle_serialization/Cargo.toml index 77a6102..dfdda40 100644 --- a/plerkle_serialization/Cargo.toml +++ b/plerkle_serialization/Cargo.toml @@ -1,7 +1,7 @@ [package] name = "plerkle_serialization" description = "Metaplex Flatbuffers Plerkle Serialization for Geyser plugin producer/consumer patterns." -version = "1.7.0" +version = "1.8.0" authors = ["Metaplex Developers "] repository = "https://github.com/metaplex-foundation/digital-asset-validator-plugin" license = "AGPL-3.0" diff --git a/plerkle_serialization/src/deserializer/solana.rs b/plerkle_serialization/src/deserializer/solana.rs index a0a4cc7..7a0913c 100644 --- a/plerkle_serialization/src/deserializer/solana.rs +++ b/plerkle_serialization/src/deserializer/solana.rs @@ -19,31 +19,24 @@ pub enum SolanaDeserializerError { pub type SolanaDeserializeResult = Result; -pub fn parse_pubkey(pubkey: Option<&FBPubkey>) -> SolanaDeserializeResult { - Pubkey::try_from( - pubkey - .ok_or(SolanaDeserializerError::NotFound)? - .0 - .as_slice(), - ) - .map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey) +pub fn parse_pubkey(pubkey: &FBPubkey) -> SolanaDeserializeResult { + Pubkey::try_from(pubkey.0.as_slice()) + .map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey) } -pub fn parse_slice(data: Option>) -> SolanaDeserializeResult<&[u8]> { - data.map(|data| data.bytes()) - .ok_or(SolanaDeserializerError::NotFound) +pub fn parse_slice(data: Vector<'_, u8>) -> SolanaDeserializeResult<&[u8]> { + Ok(data.bytes()) } -pub fn parse_signature(data: Option<&str>) -> SolanaDeserializeResult { - data.ok_or(SolanaDeserializerError::NotFound)? - .parse() +pub fn parse_signature(data: &str) -> SolanaDeserializeResult { + data.parse() .map_err(|_error| SolanaDeserializerError::DeserializationError) } pub fn parse_account_keys( - keys: Option>, + public_keys: Vector<'_, FBPubkey>, ) -> SolanaDeserializeResult> { - keys.ok_or(SolanaDeserializerError::NotFound)? + public_keys .iter() .map(|key| { Pubkey::try_from(key.0.as_slice()) @@ -53,10 +46,11 @@ pub fn parse_account_keys( } pub fn parse_compiled_instructions( - vec_cix: Option>>, + vec_cix: Vector<'_, ForwardsUOffset>, ) -> SolanaDeserializeResult> { let mut message_instructions = vec![]; - for cix in vec_cix.ok_or(SolanaDeserializerError::NotFound)? { + + for cix in vec_cix { message_instructions.push(CompiledInstruction { program_id_index: cix.program_id_index(), accounts: cix @@ -76,10 +70,11 @@ pub fn parse_compiled_instructions( } pub fn parse_compiled_inner_instructions( - vec_ixs: Option>>, + vec_ixs: Vector<'_, ForwardsUOffset>, ) -> SolanaDeserializeResult> { let mut meta_inner_instructions = vec![]; - for ixs in vec_ixs.ok_or(SolanaDeserializerError::NotFound)? { + + for ixs in vec_ixs { let mut instructions = vec![]; for ix in ixs .instructions() @@ -110,14 +105,14 @@ pub fn parse_compiled_inner_instructions( instructions, }) } + Ok(meta_inner_instructions) } pub fn parse_inner_instructions( - vec_ixs: Option>>, + vec_ixs: Vector<'_, ForwardsUOffset>, ) -> SolanaDeserializeResult> { vec_ixs - .ok_or(SolanaDeserializerError::NotFound)? .iter() .map(|iixs| { let instructions = iixs