Skip to content

Commit

Permalink
refactor: handle optionality outside of parsers
Browse files Browse the repository at this point in the history
  • Loading branch information
kespinola committed Feb 28, 2024
1 parent 9b5cacd commit 5eced4b
Showing 1 changed file with 17 additions and 22 deletions.
39 changes: 17 additions & 22 deletions plerkle_serialization/src/deserializer/solana.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,31 +19,24 @@ pub enum SolanaDeserializerError {

pub type SolanaDeserializeResult<T> = Result<T, SolanaDeserializerError>;

pub fn parse_pubkey(pubkey: Option<&FBPubkey>) -> SolanaDeserializeResult<Pubkey> {
Pubkey::try_from(
pubkey
.ok_or(SolanaDeserializerError::NotFound)?
.0
.as_slice(),
)
.map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey)
pub fn parse_pubkey(pubkey: &FBPubkey) -> SolanaDeserializeResult<Pubkey> {
Pubkey::try_from(pubkey.0.as_slice())
.map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey)
}

pub fn parse_slice(data: Option<Vector<'_, u8>>) -> 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<Signature> {
data.ok_or(SolanaDeserializerError::NotFound)?
.parse()
pub fn parse_signature(data: &str) -> SolanaDeserializeResult<Signature> {
data.parse()
.map_err(|_error| SolanaDeserializerError::DeserializationError)
}

pub fn parse_account_keys(
keys: Option<Vector<'_, FBPubkey>>,
public_keys: Vector<'_, FBPubkey>,
) -> SolanaDeserializeResult<Vec<Pubkey>> {
keys.ok_or(SolanaDeserializerError::NotFound)?
public_keys
.iter()
.map(|key| {
Pubkey::try_from(key.0.as_slice())
Expand All @@ -53,10 +46,11 @@ pub fn parse_account_keys(
}

pub fn parse_compiled_instructions(
vec_cix: Option<Vector<'_, ForwardsUOffset<FBCompiledInstruction>>>,
vec_cix: Vector<'_, ForwardsUOffset<FBCompiledInstruction>>,
) -> SolanaDeserializeResult<Vec<CompiledInstruction>> {
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
Expand All @@ -76,10 +70,11 @@ pub fn parse_compiled_instructions(
}

pub fn parse_compiled_inner_instructions(
vec_ixs: Option<Vector<'_, ForwardsUOffset<FBCompiledInnerInstructions>>>,
vec_ixs: Vector<'_, ForwardsUOffset<FBCompiledInnerInstructions>>,
) -> SolanaDeserializeResult<Vec<InnerInstructions>> {
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()
Expand Down Expand Up @@ -110,14 +105,14 @@ pub fn parse_compiled_inner_instructions(
instructions,
})
}

Ok(meta_inner_instructions)
}

pub fn parse_inner_instructions(
vec_ixs: Option<Vector<'_, ForwardsUOffset<FBInnerInstructions>>>,
vec_ixs: Vector<'_, ForwardsUOffset<FBInnerInstructions>>,
) -> SolanaDeserializeResult<Vec<InnerInstructions>> {
vec_ixs
.ok_or(SolanaDeserializerError::NotFound)?
.iter()
.map(|iixs| {
let instructions = iixs
Expand Down

0 comments on commit 5eced4b

Please sign in to comment.