Skip to content

Commit

Permalink
refactor: which to try from trait for doing struct conversion
Browse files Browse the repository at this point in the history
  • Loading branch information
kespinola committed Mar 5, 2024
1 parent 53bce90 commit 72c14cc
Showing 1 changed file with 135 additions and 100 deletions.
235 changes: 135 additions & 100 deletions plerkle_serialization/src/deserializer/solana.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::convert::TryFrom;

use crate::{
CompiledInnerInstructions as FBCompiledInnerInstructions,
CompiledInstruction as FBCompiledInstruction, InnerInstructions as FBInnerInstructions,
Expand All @@ -19,46 +21,67 @@ 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)
impl<'a> TryFrom<&FBPubkey> for Pubkey {
type Error = SolanaDeserializerError;

fn try_from(pubkey: &FBPubkey) -> SolanaDeserializeResult<Self> {
Pubkey::try_from(pubkey.0.as_slice())
.map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey)
}
}

pub fn parse_slice(data: Option<Vector<'_, u8>>) -> SolanaDeserializeResult<&[u8]> {
Ok(data.ok_or(SolanaDeserializerError::NotFound)?.bytes())
pub struct PlerkleOptionalU8Vector<'a>(pub Option<Vector<'a, u8>>);

impl<'a> TryFrom<PlerkleOptionalU8Vector<'a>> for &[u8] {
type Error = SolanaDeserializerError;

fn try_from(data: PlerkleOptionalU8Vector) -> SolanaDeserializeResult<Self> {
Ok(data.0.ok_or(SolanaDeserializerError::NotFound)?.bytes())
}
}

pub fn parse_signature(data: Option<&str>) -> SolanaDeserializeResult<Signature> {
data.ok_or(SolanaDeserializerError::NotFound)?
.parse()
.map_err(|_error| SolanaDeserializerError::DeserializationError)
pub struct PlerkleOptionalStr<'a>(pub Option<&'a str>);

impl<'a> TryFrom<PlerkleOptionalStr<'a>> for Signature {
type Error = SolanaDeserializerError;

fn try_from(data: PlerkleOptionalStr<'a>) -> SolanaDeserializeResult<Self> {
data.0
.ok_or(SolanaDeserializerError::NotFound)?
.parse::<Signature>()
.map_err(|_error| SolanaDeserializerError::DeserializationError)
}
}

pub fn parse_account_keys(
public_keys: Option<Vector<'_, FBPubkey>>,
) -> SolanaDeserializeResult<Vec<Pubkey>> {
public_keys
.ok_or(SolanaDeserializerError::NotFound)?
.iter()
.map(|key| {
Pubkey::try_from(key.0.as_slice())
.map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey)
})
.collect::<SolanaDeserializeResult<Vec<Pubkey>>>()
pub struct PlerkleOptionalPubkeys<'a>(pub Option<Vector<'a, FBPubkey>>);

impl<'a> TryFrom<PlerkleOptionalPubkeys<'a>> for Vec<Pubkey> {
type Error = SolanaDeserializerError;

fn try_from(public_keys: PlerkleOptionalPubkeys<'a>) -> SolanaDeserializeResult<Self> {
public_keys
.0
.ok_or(SolanaDeserializerError::NotFound)?
.iter()
.map(|key| {
Pubkey::try_from(key.0.as_slice())
.map_err(|_error| SolanaDeserializerError::InvalidFlatBufferKey)
})
.collect::<SolanaDeserializeResult<Vec<Pubkey>>>()
}
}

pub fn parse_compiled_instructions(
vec_cix: Option<Vector<'_, ForwardsUOffset<FBCompiledInstruction>>>,
) -> SolanaDeserializeResult<Vec<CompiledInstruction>> {
let mut message_instructions = vec![];
pub struct PlerkleCompiledInstructionVector<'a>(
pub Vector<'a, ForwardsUOffset<FBCompiledInstruction<'a>>>,
);

impl<'a> TryFrom<PlerkleCompiledInstructionVector<'a>> for Vec<CompiledInstruction> {
type Error = SolanaDeserializerError;

if let Some(vec_cix) = vec_cix {
for cix in vec_cix {
fn try_from(vec_cix: PlerkleCompiledInstructionVector<'a>) -> SolanaDeserializeResult<Self> {
let mut message_instructions = vec![];

for cix in vec_cix.0 {
message_instructions.push(CompiledInstruction {
program_id_index: cix.program_id_index(),
accounts: cix
Expand All @@ -73,84 +96,96 @@ pub fn parse_compiled_instructions(
.to_vec(),
})
}
}

Ok(message_instructions)
Ok(message_instructions)
}
}

pub fn parse_compiled_inner_instructions(
vec_ixs: Vector<'_, ForwardsUOffset<FBCompiledInnerInstructions>>,
) -> SolanaDeserializeResult<Vec<InnerInstructions>> {
let mut meta_inner_instructions = vec![];
pub struct PlerkleCompiledInnerInstructionVector<'a>(
pub Vector<'a, ForwardsUOffset<FBCompiledInnerInstructions<'a>>>,
);
impl<'a> TryFrom<PlerkleCompiledInnerInstructionVector<'a>> for Vec<InnerInstructions> {
type Error = SolanaDeserializerError;

for ixs in vec_ixs {
let mut instructions = vec![];
for ix in ixs
.instructions()
.ok_or(SolanaDeserializerError::NotFound)?
{
let cix = ix
.compiled_instruction()
.ok_or(SolanaDeserializerError::NotFound)?;
instructions.push(InnerInstruction {
instruction: 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(),
},
stack_height: Some(ix.stack_height() as u32),
});
fn try_from(
vec_ixs: PlerkleCompiledInnerInstructionVector<'a>,
) -> SolanaDeserializeResult<Self> {
let mut meta_inner_instructions = vec![];

for ixs in vec_ixs.0 {
let mut instructions = vec![];
for ix in ixs
.instructions()
.ok_or(SolanaDeserializerError::NotFound)?
{
let cix = ix
.compiled_instruction()
.ok_or(SolanaDeserializerError::NotFound)?;
instructions.push(InnerInstruction {
instruction: 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(),
},
stack_height: Some(ix.stack_height() as u32),
});
}
meta_inner_instructions.push(InnerInstructions {
index: ixs.index(),
instructions,
})
}
meta_inner_instructions.push(InnerInstructions {
index: ixs.index(),
instructions,
})
}

Ok(meta_inner_instructions)
Ok(meta_inner_instructions)
}
}

pub fn parse_inner_instructions(
vec_ixs: Vector<'_, ForwardsUOffset<FBInnerInstructions>>,
) -> SolanaDeserializeResult<Vec<InnerInstructions>> {
vec_ixs
.iter()
.map(|iixs| {
let instructions = iixs
.instructions()
.ok_or(SolanaDeserializerError::NotFound)?
.iter()
.map(|cix| {
Ok(InnerInstruction {
instruction: 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(),
},
stack_height: Some(0),
pub struct PlerkleInnerInstructions<'a>(pub Vector<'a, ForwardsUOffset<FBInnerInstructions<'a>>>);

impl<'a> TryFrom<PlerkleInnerInstructions<'a>> for Vec<InnerInstructions> {
type Error = SolanaDeserializerError;

fn try_from(vec_ixs: PlerkleInnerInstructions<'a>) -> SolanaDeserializeResult<Self> {
vec_ixs
.0
.iter()
.map(|iixs| {
let instructions = iixs
.instructions()
.ok_or(SolanaDeserializerError::NotFound)?
.iter()
.map(|cix| {
Ok(InnerInstruction {
instruction: 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(),
},
stack_height: Some(0),
})
})
.collect::<SolanaDeserializeResult<Vec<InnerInstruction>>>()?;
Ok(InnerInstructions {
index: iixs.index(),
instructions,
})
.collect::<SolanaDeserializeResult<Vec<InnerInstruction>>>()?;
Ok(InnerInstructions {
index: iixs.index(),
instructions,
})
})
.collect::<SolanaDeserializeResult<Vec<InnerInstructions>>>()
.collect::<SolanaDeserializeResult<Vec<InnerInstructions>>>()
}
}

0 comments on commit 72c14cc

Please sign in to comment.