Skip to content
This repository was archived by the owner on Mar 11, 2025. It is now read-only.

remove solana-program from spl-type-length-value #7428

Merged
merged 2 commits into from
Oct 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
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
11 changes: 8 additions & 3 deletions Cargo.lock

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

2 changes: 1 addition & 1 deletion libraries/type-length-value-derive-test/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ edition = "2021"

[dev-dependencies]
borsh = "1.5.1"
solana-program = "2.1.0"
solana-borsh = "2.1.0"
spl-discriminator = { version = "0.3.0", path = "../discriminator" }
spl-type-length-value = { version = "0.6.0", path = "../type-length-value", features = [
"derive",
Expand Down
2 changes: 1 addition & 1 deletion libraries/type-length-value-derive-test/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
pub mod test {
use {
borsh::{BorshDeserialize, BorshSerialize},
solana_program::borsh1::{get_instance_packed_len, try_from_slice_unchecked},
solana_borsh::v1::{get_instance_packed_len, try_from_slice_unchecked},
spl_discriminator::SplDiscriminate,
spl_type_length_value::{variable_len_pack::VariableLenPack, SplBorshVariableLenPack},
};
Expand Down
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
10 changes: 5 additions & 5 deletions libraries/type-length-value/derive/src/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,16 +74,16 @@ impl From<&SplBorshVariableLenPackBuilder> for TokenStream {
let where_clause = &builder.where_clause;
quote! {
impl #generics spl_type_length_value::variable_len_pack::VariableLenPack for #ident #generics #where_clause {
fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), spl_type_length_value::solana_program::program_error::ProgramError> {
fn pack_into_slice(&self, dst: &mut [u8]) -> Result<(), spl_type_length_value::solana_program_error::ProgramError> {
borsh::to_writer(&mut dst[..], self).map_err(Into::into)
}

fn unpack_from_slice(src: &[u8]) -> Result<Self, spl_type_length_value::solana_program::program_error::ProgramError> {
solana_program::borsh1::try_from_slice_unchecked(src).map_err(Into::into)
fn unpack_from_slice(src: &[u8]) -> Result<Self, spl_type_length_value::solana_program_error::ProgramError> {
solana_borsh::v1::try_from_slice_unchecked(src).map_err(Into::into)
Copy link
Contributor Author

Choose a reason for hiding this comment

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

breaking change

}

fn get_packed_len(&self) -> Result<usize, spl_type_length_value::solana_program::program_error::ProgramError> {
solana_program::borsh1::get_instance_packed_len(self).map_err(Into::into)
fn get_packed_len(&self) -> Result<usize, spl_type_length_value::solana_program_error::ProgramError> {
solana_borsh::v1::get_instance_packed_len(self).map_err(Into::into)
}
}
}
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
4 changes: 2 additions & 2 deletions libraries/type-length-value/src/variable_len_pack.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
//! 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
/// `solana_program_pack::Pack` but specifically works for
/// variable-size types.
pub trait VariableLenPack {
/// Writes the serialized form of the instance into the given slice
Expand Down
Loading