From 53bce9048ac0bbaa43246185edf785ab16b5c07c Mon Sep 17 00:00:00 2001 From: Kyle Espinola Date: Thu, 29 Feb 2024 18:03:33 +0100 Subject: [PATCH] refactor: move back to handling option --- .../src/deserializer/solana.rs | 55 +++++++++++-------- 1 file changed, 32 insertions(+), 23 deletions(-) diff --git a/plerkle_serialization/src/deserializer/solana.rs b/plerkle_serialization/src/deserializer/solana.rs index 7a0913c..ec2f6ea 100644 --- a/plerkle_serialization/src/deserializer/solana.rs +++ b/plerkle_serialization/src/deserializer/solana.rs @@ -19,24 +19,31 @@ pub enum SolanaDeserializerError { pub type SolanaDeserializeResult = Result; -pub fn parse_pubkey(pubkey: &FBPubkey) -> SolanaDeserializeResult { - Pubkey::try_from(pubkey.0.as_slice()) - .map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey) +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_slice(data: Vector<'_, u8>) -> SolanaDeserializeResult<&[u8]> { - Ok(data.bytes()) +pub fn parse_slice(data: Option>) -> SolanaDeserializeResult<&[u8]> { + Ok(data.ok_or(SolanaDeserializerError::NotFound)?.bytes()) } -pub fn parse_signature(data: &str) -> SolanaDeserializeResult { - data.parse() +pub fn parse_signature(data: Option<&str>) -> SolanaDeserializeResult { + data.ok_or(SolanaDeserializerError::NotFound)? + .parse() .map_err(|_error| SolanaDeserializerError::DeserializationError) } pub fn parse_account_keys( - public_keys: Vector<'_, FBPubkey>, + public_keys: Option>, ) -> SolanaDeserializeResult> { public_keys + .ok_or(SolanaDeserializerError::NotFound)? .iter() .map(|key| { Pubkey::try_from(key.0.as_slice()) @@ -46,24 +53,26 @@ pub fn parse_account_keys( } pub fn parse_compiled_instructions( - vec_cix: Vector<'_, ForwardsUOffset>, + vec_cix: Option>>, ) -> SolanaDeserializeResult> { let mut message_instructions = vec![]; - for cix in vec_cix { - message_instructions.push(CompiledInstruction { - program_id_index: cix.program_id_index(), - accounts: cix - .accounts() - .ok_or(SolanaDeserializerError::NotFound)? - .bytes() - .to_vec(), - data: cix - .data() - .ok_or(SolanaDeserializerError::NotFound)? - .bytes() - .to_vec(), - }) + if let Some(vec_cix) = vec_cix { + for cix in vec_cix { + message_instructions.push(CompiledInstruction { + program_id_index: cix.program_id_index(), + accounts: cix + .accounts() + .ok_or(SolanaDeserializerError::NotFound)? + .bytes() + .to_vec(), + data: cix + .data() + .ok_or(SolanaDeserializerError::NotFound)? + .bytes() + .to_vec(), + }) + } } Ok(message_instructions)