From 5eced4b25f6da9a7a238f298c214fc361deb0e43 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 --- .../src/deserializer/solana.rs | 39 ++++++++----------- 1 file changed, 17 insertions(+), 22 deletions(-) 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