From 446167fd7a946c16ffbd3b09cee5600d16164117 Mon Sep 17 00:00:00 2001 From: ebrightfield Date: Tue, 2 Jul 2024 09:22:40 -0400 Subject: [PATCH] adds feature-gated serde traits to TransactionMetadata (#77) * adds feature-gated serde traits to TransactionMetadata * update changelog and serde version --------- Co-authored-by: eric <> --- CHANGELOG.md | 1 + Cargo.toml | 3 ++- src/types.rs | 3 +++ src/utils/mod.rs | 2 ++ src/utils/serde_with_str.rs | 24 ++++++++++++++++++++++++ 5 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 src/utils/serde_with_str.rs diff --git a/CHANGELOG.md b/CHANGELOG.md index 36c596a..ef12fe6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -9,6 +9,7 @@ - Add `error.rs` and new `LiteSVMError` type ([#62](https://github.com/LiteSVM/litesvm/pull/62)). - Add more logging for users to make debugging errors easier ([#62](https://github.com/LiteSVM/litesvm/pull/62)). - Add `inner_instructions` to `TransactionMetadata` ([#75](https://github.com/LiteSVM/litesvm/pull/75)). +- Add feature-flagged `serde` traits to `TransactionMetadata` ([#77](https://github.com/LiteSVM/litesvm/pull/77)). ### Changed diff --git a/Cargo.toml b/Cargo.toml index 463435d..069f8fb 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -25,6 +25,7 @@ log = "0.4.21" solana-vote-program = "~1.18" solana-stake-program = "~1.18" solana-config-program = "~1.18" +serde = { version = "1.0", optional = true } [dev-dependencies] spl-token = "3.5.0" @@ -33,7 +34,7 @@ criterion = "0.5" tokio = "1.35" test-log = "0.2.15" solana-config-program = "~1.18" -serde = "1.0.197" +serde = "1.0" [features] internal-test = [] diff --git a/src/types.rs b/src/types.rs index b842305..f6e2b7e 100644 --- a/src/types.rs +++ b/src/types.rs @@ -8,7 +8,9 @@ use solana_sdk::{ }; #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct TransactionMetadata { + #[cfg_attr(feature = "serde", serde(with = "crate::utils::serde_with_str"))] pub signature: Signature, pub logs: Vec, pub inner_instructions: InnerInstructionsList, @@ -17,6 +19,7 @@ pub struct TransactionMetadata { } #[derive(Debug, Clone)] +#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))] pub struct FailedTransactionMetadata { pub err: TransactionError, pub meta: TransactionMetadata, diff --git a/src/utils/mod.rs b/src/utils/mod.rs index ac4404f..c877532 100644 --- a/src/utils/mod.rs +++ b/src/utils/mod.rs @@ -7,6 +7,8 @@ use solana_sdk::{ pub mod inner_instructions; pub mod rent; +#[cfg(feature = "serde")] +pub mod serde_with_str; /// Create a blockhash from the given bytes pub fn create_blockhash(bytes: &[u8]) -> Hash { diff --git a/src/utils/serde_with_str.rs b/src/utils/serde_with_str.rs new file mode 100644 index 0000000..ff4b67d --- /dev/null +++ b/src/utils/serde_with_str.rs @@ -0,0 +1,24 @@ +use { + serde::{de, Deserializer, Serializer}, + serde::{Deserialize, Serialize}, + std::str::FromStr, +}; + +pub fn serialize(t: &T, serializer: S) -> Result +where + T: ToString, + S: Serializer, +{ + t.to_string().serialize(serializer) +} + +pub fn deserialize<'de, T, D>(deserializer: D) -> Result +where + T: FromStr, + D: Deserializer<'de>, + ::Err: std::fmt::Debug, +{ + let s: String = String::deserialize(deserializer)?; + s.parse() + .map_err(|e| de::Error::custom(format!("Parse error: {e:?}"))) +}