Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 7 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

9 changes: 7 additions & 2 deletions libraries/type-length-value/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,11 +13,16 @@ derive = ["dep:spl-type-length-value-derive"]

[dependencies]
bytemuck = { version = "1.19.0", features = ["derive"] }
solana-program = "2.1.0"
num-derive = "0.4"
num-traits = "0.2"
solana-account-info = "2.1.0"
solana-decode-error = "2.1.0"
solana-msg = "2.1.0"
solana-program-error = "2.1.0"
spl-discriminator = { version = "0.3.0", path = "../discriminator" }
spl-program-error = { version = "0.5.0", path = "../program-error" }
spl-type-length-value-derive = { version = "0.1", path = "./derive", optional = true }
spl-pod = { version = "0.4.0", path = "../pod" }
thiserror = "1.0"

[lib]
crate-type = ["cdylib", "lib"]
Expand Down
10 changes: 4 additions & 6 deletions libraries/type-length-value/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ let account_size = TlvStateMut::get_base_len()
+ TlvStateMut::get_base_len()
+ std::mem::size_of::<MyOtherPodValue>();

// Buffer likely comes from a Solana `solana_program::account_info::AccountInfo`,
// Buffer likely comes from a Solana `solana_account_info::AccountInfo`,
// but this example just uses a vector.
let mut buffer = vec![0; account_size];

Expand Down Expand Up @@ -146,10 +146,8 @@ trait on your type.
```rust
use {
borsh::{BorshDeserialize, BorshSerialize},
solana_program::{
borsh1::{get_instance_packed_len, try_from_slice_unchecked},
program_error::ProgramError,
},
solana_borsh::v1::{get_instance_packed_len, try_from_slice_unchecked},
solana_program_error::ProgramError,
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
spl_type_length_value::{
state::{TlvState, TlvStateMut},
Expand Down Expand Up @@ -181,7 +179,7 @@ let initial_data = "This is a pretty cool test!";
let tlv_size = 4 + initial_data.len();
let account_size = TlvStateMut::get_base_len() + tlv_size;

// Buffer likely comes from a Solana `solana_program::account_info::AccountInfo`,
// Buffer likely comes from a Solana `solana_account_info::AccountInfo`,
// but this example just uses a vector.
let mut buffer = vec![0; account_size];
let mut state = TlvStateMut::unpack(&mut buffer).unwrap();
Expand Down
44 changes: 40 additions & 4 deletions libraries/type-length-value/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,50 @@
//! Error types

use spl_program_error::*;
use {
solana_decode_error::DecodeError,
solana_msg::msg,
solana_program_error::{PrintProgramError, ProgramError},
};

/// Errors that may be returned by the Token program.
#[spl_program_error(hash_error_code_start = 1_202_666_432)]
#[repr(u32)]
#[derive(Clone, Debug, Eq, thiserror::Error, num_derive::FromPrimitive, PartialEq)]
pub enum TlvError {
/// Type not found in TLV data
#[error("Type not found in TLV data")]
TypeNotFound,
TypeNotFound = 1_202_666_432,
/// Type already exists in TLV data
#[error("Type already exists in TLV data")]
TypeAlreadyExists,
}

impl From<TlvError> for ProgramError {
fn from(e: TlvError) -> Self {
ProgramError::Custom(e as u32)
}
}

impl<T> DecodeError<T> for TlvError {
fn type_of() -> &'static str {
"TlvError"
}
}

impl PrintProgramError for TlvError {
fn print<E>(&self)
where
E: 'static
+ std::error::Error
+ DecodeError<E>
+ PrintProgramError
+ num_traits::FromPrimitive,
{
match self {
TlvError::TypeNotFound => {
msg!("Type not found in TLV data")
}
TlvError::TypeAlreadyExists => {
msg!("Type already exists in TLV data")
}
}
}
}
2 changes: 1 addition & 1 deletion libraries/type-length-value/src/length.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
//! Module for the length portion of a Type-Length-Value structure
use {
bytemuck::{Pod, Zeroable},
solana_program::program_error::ProgramError,
solana_program_error::ProgramError,
spl_pod::primitives::PodU32,
};

Expand Down
2 changes: 1 addition & 1 deletion libraries/type-length-value/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub mod variable_len_pack;

// Export current sdk types for downstream users building with a different sdk
// version
pub use solana_program;
Copy link
Contributor Author

@kevinheavey kevinheavey Oct 31, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

breaking change

// Expose derive macro on feature flag
#[cfg(feature = "derive")]
pub use spl_type_length_value_derive::SplBorshVariableLenPack;
pub use {solana_account_info, solana_decode_error, solana_program_error};
3 changes: 2 additions & 1 deletion libraries/type-length-value/src/state.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@
use {
crate::{error::TlvError, length::Length, variable_len_pack::VariableLenPack},
bytemuck::Pod,
solana_program::{account_info::AccountInfo, program_error::ProgramError},
solana_account_info::AccountInfo,
solana_program_error::ProgramError,
spl_discriminator::{ArrayDiscriminator, SplDiscriminate},
spl_pod::bytemuck::{pod_from_bytes, pod_from_bytes_mut},
std::{cmp::Ordering, mem::size_of},
Expand Down
2 changes: 1 addition & 1 deletion libraries/type-length-value/src/variable_len_pack.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
//! The [`VariableLenPack`] serialization trait.

use solana_program::program_error::ProgramError;
use solana_program_error::ProgramError;

/// Trait that mimics a lot of the functionality of
/// `solana_program::program_pack::Pack` but specifically works for
Expand Down
Loading