diff --git a/CHANGELOG.md b/CHANGELOG.md index 7abc7794..3d550627 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -6,6 +6,7 @@ - Extract solders-primitives into its own crate [(#24)](https://github.com/kevinheavey/solders/pull/24) - Don't leak custom error types in solders-traits; use ValueError instead [(#26)](https://github.com/kevinheavey/solders/pull/26) - Improve macro hygiene [(#27)](https://github.com/kevinheavey/solders/pull/27) and [(#28)]([(#27)](https://github.com/kevinheavey/solders/pull/27)) +- Add EnumIntoPy derive macro [(#29)](https://github.com/kevinheavey/solders/pull/29) ## [0.10.0] - 2022-10-31 diff --git a/macros/src/lib.rs b/macros/src/lib.rs index f816094c..8a0b9552 100644 --- a/macros/src/lib.rs +++ b/macros/src/lib.rs @@ -280,3 +280,33 @@ pub fn enum_original_mapping(original: TokenStream, item: TokenStream) -> TokenS new_stream.extend(from_impl); TokenStream::from(new_stream) } + +/// Impl IntoPy for an ADT where each variant is a newtype. +/// +/// # Example +/// +/// ```rust +/// use solders_macros::EnumIntoPy; +/// +/// #[derive(PartialEq, Debug, EnumIntoPy)] +/// pub enum Foo { +/// A(u8), +/// B(u8) +/// } +/// +#[proc_macro_derive(EnumIntoPy)] +pub fn enum_into_py(item: TokenStream) -> TokenStream { + let ast = parse_macro_input!(item as ItemEnum); + let enum_name = ast.ident; + let variant_names: Vec = ast.variants.into_iter().map(|v| v.ident).collect(); + let into_py_impl = quote! { + impl IntoPy for #enum_name { + fn into_py(self, py: Python<'_>) -> PyObject { + match self { + #(Self::#variant_names(x) => x.into_py(py)),*, + } + } + } + }; + into_py_impl.to_token_stream().into() +} diff --git a/primitives/src/message.rs b/primitives/src/message.rs index df19e120..1bcce1e2 100644 --- a/primitives/src/message.rs +++ b/primitives/src/message.rs @@ -17,7 +17,7 @@ use solana_sdk::{ }, pubkey::Pubkey as PubkeyOriginal, }; -use solders_macros::{common_methods, richcmp_eq_only}; +use solders_macros::{common_methods, richcmp_eq_only, EnumIntoPy}; use solders_traits::{ handle_py_err, impl_display, py_from_bytes_general_via_bincode, pybytes_general_via_bincode, CommonMethods, PyBytesGeneral, PyErrWrapper, RichcmpEqualityOnly, @@ -820,22 +820,13 @@ impl MessageV0 { } } -#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, FromPyObject)] +#[derive(Debug, PartialEq, Eq, Clone, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(from = "VersionedMessageOriginal", into = "VersionedMessageOriginal")] pub enum VersionedMessage { Legacy(Message), V0(MessageV0), } -impl IntoPy for VersionedMessage { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Legacy(m) => m.into_py(py), - Self::V0(m) => m.into_py(py), - } - } -} - impl From for VersionedMessage { fn from(v: VersionedMessageOriginal) -> Self { match v { diff --git a/primitives/src/transaction.rs b/primitives/src/transaction.rs index a144ddf5..b9c14792 100644 --- a/primitives/src/transaction.rs +++ b/primitives/src/transaction.rs @@ -12,7 +12,7 @@ use solana_sdk::{ VersionedTransaction as VersionedTransactionOriginal, }, }; -use solders_macros::{common_methods, richcmp_eq_only}; +use solders_macros::{common_methods, richcmp_eq_only, EnumIntoPy}; use solders_traits::{ handle_py_err, impl_display, py_from_bytes_general_via_bincode, pybytes_general_via_bincode, CommonMethods, RichcmpEqualityOnly, @@ -713,22 +713,13 @@ impl From for Legacy { } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum TransactionVersion { Legacy(Legacy), Number(u8), } -impl IntoPy for TransactionVersion { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Legacy(v) => v.into_py(py), - Self::Number(v) => v.into_py(py), - } - } -} - impl From for TransactionVersionOriginal { fn from(v: TransactionVersion) -> Self { match v { diff --git a/src/rpc/config.rs b/src/rpc/config.rs index a5ec2692..77066431 100644 --- a/src/rpc/config.rs +++ b/src/rpc/config.rs @@ -4,7 +4,7 @@ use crate::rpc::tmp_config as rpc_config; use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use solana_sdk::commitment_config::CommitmentLevel as CommitmentLevelOriginal; -use solders_macros::{common_methods, richcmp_eq_only}; +use solders_macros::{common_methods, richcmp_eq_only, EnumIntoPy}; use solders_primitives::{hash::Hash as SolderHash, pubkey::Pubkey, signature::Signature}; use solders_traits::{ impl_display, py_from_bytes_general_via_cbor, pybytes_general_via_cbor, CommonMethods, @@ -874,7 +874,7 @@ impl RpcTransactionLogsFilterMentions { impl RichcmpEqualityOnly for RpcTransactionLogsFilterMentions {} -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] pub enum TransactionLogsFilterWrapper { Plain(RpcTransactionLogsFilter), Mentions(RpcTransactionLogsFilterMentions), @@ -916,15 +916,6 @@ impl From for TransactionLogsFilterWrapper } } -impl IntoPy for TransactionLogsFilterWrapper { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Plain(f) => f.into_py(py), - Self::Mentions(m) => m.into_py(py), - } - } -} - pyclass_boilerplate!( /// Configuration object for ``logsSubscribe``. /// @@ -1009,7 +1000,7 @@ impl RpcTokenAccountsFilterProgramId { impl RichcmpEqualityOnly for RpcTokenAccountsFilterProgramId {} -#[derive(FromPyObject, Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] +#[derive(FromPyObject, Clone, Debug, PartialEq, Eq, Serialize, Deserialize, EnumIntoPy)] pub enum RpcTokenAccountsFilterWrapper { Mint(RpcTokenAccountsFilterMint), ProgramId(RpcTokenAccountsFilterProgramId), @@ -1037,15 +1028,6 @@ impl From for RpcTokenAccountsFilterWrapper } } -impl IntoPy for RpcTokenAccountsFilterWrapper { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - RpcTokenAccountsFilterWrapper::Mint(m) => m.into_py(py), - RpcTokenAccountsFilterWrapper::ProgramId(m) => m.into_py(py), - } - } -} - pyclass_boilerplate_with_default!( /// Configuration object for ``signatureSubscribe``. /// @@ -1130,7 +1112,7 @@ impl RpcBlockSubscribeFilterMentions { impl RichcmpEqualityOnly for RpcBlockSubscribeFilterMentions {} -#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, EnumIntoPy)] pub enum RpcBlockSubscribeFilterWrapper { All(RpcBlockSubscribeFilter), MentionsAccountOrProgram(RpcBlockSubscribeFilterMentions), @@ -1164,15 +1146,6 @@ impl From for RpcBlockSubscribeFilterWrappe } } -impl IntoPy for RpcBlockSubscribeFilterWrapper { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::All(m) => m.into_py(py), - Self::MentionsAccountOrProgram(m) => m.into_py(py), - } - } -} - pyclass_boilerplate_with_default!( /// Configuration object for ``blockSubscribe``. /// diff --git a/src/rpc/errors.rs b/src/rpc/errors.rs index e0fb1d1d..17cbaa59 100644 --- a/src/rpc/errors.rs +++ b/src/rpc/errors.rs @@ -1,13 +1,10 @@ -use crate::{ - transaction_status::{transaction_status_boilerplate, TransactionErrorType}, - -}; -use solders_traits::{CommonMethods, RichcmpEqualityOnly,}; +use crate::transaction_status::{transaction_status_boilerplate, TransactionErrorType}; use derive_more::{From, Into}; use pyo3::{prelude::*, types::PyTuple, PyTypeInfo}; use serde::{Deserialize, Serialize}; use solana_sdk::slot_history::Slot; -use solders_macros::{common_methods, richcmp_eq_only}; +use solders_macros::{common_methods, richcmp_eq_only, EnumIntoPy}; +use solders_traits::{CommonMethods, RichcmpEqualityOnly}; use std::fmt::Display; use super::responses::RpcSimulateTransactionResult; @@ -335,7 +332,7 @@ impl UnsupportedTransactionVersion { error_message!(UnsupportedTransactionVersionMessage); -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] #[serde(untagged)] pub enum RpcCustomError { Fieldless(RpcCustomErrorFieldless), @@ -359,26 +356,6 @@ error_message!(MethodNotFoundMessage); error_message!(InvalidParamsMessage); error_message!(InternalErrorMessage); -impl IntoPy for RpcCustomError { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::BlockCleanedUp(x) => x.into_py(py), - Self::SendTransactionPreflightFailure(x) => x.into_py(py), - Self::BlockNotAvailable(x) => x.into_py(py), - Self::NodeUnhealthy(x) => x.into_py(py), - Self::TransactionPrecompileVerificationFailure(x) => x.into_py(py), - Self::SlotSkipped(x) => x.into_py(py), - Self::LongTermStorageSlotSkipped(x) => x.into_py(py), - Self::KeyExcludedFromSecondaryIndex(x) => x.into_py(py), - Self::ScanError(x) => x.into_py(py), - Self::BlockStatusNotAvailableYet(x) => x.into_py(py), - Self::MinContextSlotNotReached(x) => x.into_py(py), - Self::UnsupportedTransactionVersion(x) => x.into_py(py), - Self::Fieldless(x) => x.into_py(py), - } - } -} - pub(crate) fn create_errors_mod(py: Python<'_>) -> PyResult<&PyModule> { let m = PyModule::new(py, "errors")?; m.add_class::()?; diff --git a/src/rpc/filter.rs b/src/rpc/filter.rs index 9fe119ad..6317db85 100644 --- a/src/rpc/filter.rs +++ b/src/rpc/filter.rs @@ -6,29 +6,20 @@ use pyo3::prelude::*; use serde::{Deserialize, Serialize}; use derive_more::{From, Into}; -use solders_macros::{common_methods, enum_original_mapping, richcmp_eq_only}; +use solders_macros::{common_methods, enum_original_mapping, richcmp_eq_only, EnumIntoPy}; use solders_traits::{ impl_display, py_from_bytes_general_via_bincode, pybytes_general_via_bincode, CommonMethods, RichcmpEqualityOnly, }; -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, FromPyObject)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum MemcmpEncodedBytes { Base58(String), Bytes(Vec), } -impl IntoPy for MemcmpEncodedBytes { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Base58(s) => s.into_py(py), - Self::Bytes(v) => v.into_py(py), - } - } -} - impl From for MemcmpEncodedBytesOriginal { fn from(m: MemcmpEncodedBytes) -> Self { match m { @@ -107,22 +98,13 @@ impl Memcmp { impl RichcmpEqualityOnly for Memcmp {} impl CommonMethods<'_> for Memcmp {} -#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, FromPyObject)] +#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase")] pub enum RpcFilterType { DataSize(u64), Memcmp(Memcmp), } -impl IntoPy for RpcFilterType { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - RpcFilterType::DataSize(num) => num.into_py(py), - RpcFilterType::Memcmp(mem) => mem.into_py(py), - } - } -} - impl From for RpcFilterTypeOriginal { fn from(r: RpcFilterType) -> Self { match r { diff --git a/src/rpc/requests.rs b/src/rpc/requests.rs index 4f751c04..7ca69456 100644 --- a/src/rpc/requests.rs +++ b/src/rpc/requests.rs @@ -16,7 +16,7 @@ use serde_with::{serde_as, skip_serializing_none, DisplayFromStr, FromInto}; use solana_sdk::{ message::Message as MessageOriginal, transaction::Transaction as TransactionOriginal, }; -use solders_macros::{common_methods, richcmp_eq_only, rpc_id_getter}; +use solders_macros::{common_methods, richcmp_eq_only, rpc_id_getter, EnumIntoPy}; use crate::{Signature, SolderHash}; @@ -2709,19 +2709,11 @@ zero_param_req_def!(VoteSubscribe); macro_rules ! pyunion { ($name:ident, $($variant:ident),+) => { - #[derive(FromPyObject, Clone, Debug, PartialEq, Serialize, Deserialize)] + #[derive(FromPyObject, Clone, Debug, PartialEq, Serialize, Deserialize, EnumIntoPy)] #[serde(tag = "method", rename_all = "camelCase")] pub enum $name { $($variant($variant),)+ } - - impl IntoPy for $name { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - $(Self::$variant(x) => x.into_py(py),)+ - } - } - } } } diff --git a/src/rpc/responses.rs b/src/rpc/responses.rs index 238fa0f3..a45b2d71 100644 --- a/src/rpc/responses.rs +++ b/src/rpc/responses.rs @@ -64,7 +64,7 @@ use solana_sdk::{ transaction_context::TransactionReturnData as TransactionReturnDataOriginal, }; use solders_macros::{ - common_methods, common_methods_rpc_resp, enum_original_mapping, richcmp_eq_only, + common_methods, common_methods_rpc_resp, enum_original_mapping, richcmp_eq_only, EnumIntoPy }; use solders_primitives::{pubkey::Pubkey, signature::Signature}; use solders_traits::{ @@ -249,7 +249,7 @@ macro_rules! contextless_resp_no_eq { }; } -#[derive(FromPyObject, Clone, Debug, PartialEq, Eq)] +#[derive(FromPyObject, Clone, Debug, PartialEq, Eq, EnumIntoPy)] pub enum RPCError { Fieldless(RpcCustomErrorFieldless), BlockCleanedUpMessage(BlockCleanedUpMessage), @@ -304,31 +304,6 @@ impl RPCError { } } -impl IntoPy for RPCError { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Fieldless(x) => x.into_py(py), - Self::BlockCleanedUpMessage(x) => x.into_py(py), - Self::SendTransactionPreflightFailureMessage(x) => x.into_py(py), - Self::BlockNotAvailableMessage(x) => x.into_py(py), - Self::NodeUnhealthyMessage(x) => x.into_py(py), - Self::TransactionPrecompileVerificationFailureMessage(x) => x.into_py(py), - Self::SlotSkippedMessage(x) => x.into_py(py), - Self::LongTermStorageSlotSkippedMessage(x) => x.into_py(py), - Self::KeyExcludedFromSecondaryIndexMessage(x) => x.into_py(py), - Self::ScanErrorMessage(x) => x.into_py(py), - Self::BlockStatusNotAvailableYetMessage(x) => x.into_py(py), - Self::MinContextSlotNotReachedMessage(x) => x.into_py(py), - Self::UnsupportedTransactionVersionMessage(x) => x.into_py(py), - Self::ParseErrorMessage(x) => x.into_py(py), - Self::InvalidRequestMessage(x) => x.into_py(py), - Self::MethodNotFoundMessage(x) => x.into_py(py), - Self::InvalidParamsMessage(x) => x.into_py(py), - Self::InternalErrorMessage(x) => x.into_py(py), - } - } -} - impl<'de> serde::Deserialize<'de> for RPCError { fn deserialize>(d: D) -> Result { let value = Value::deserialize(d)?; @@ -674,7 +649,7 @@ impl IntoPy for Notification { } } -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, EnumIntoPy)] #[serde(untagged)] pub enum WebsocketMessage { Notification(Notification), @@ -682,16 +657,6 @@ pub enum WebsocketMessage { SubscriptionError(SubscriptionError), } -impl IntoPy for WebsocketMessage { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Notification(x) => x.into_py(py), - Self::SubscriptionResult(x) => x.into_py(py), - Self::SubscriptionError(x) => x.into_py(py), - } - } -} - #[serde_as] #[derive(Serialize, Deserialize, Clone, Debug, PartialEq)] pub struct WebsocketMessages(#[serde_as(deserialize_as = "OneOrMany<_>")] Vec); @@ -1608,22 +1573,13 @@ contextful_resp_eq!( "Vec>>" ); -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, FromPyObject)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, FromPyObject, EnumIntoPy)] #[serde(untagged)] pub enum AccountMaybeJSON { Binary(Account), Parsed(AccountJSON), } -impl IntoPy for AccountMaybeJSON { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Parsed(x) => x.into_py(py), - Self::Binary(x) => x.into_py(py), - } - } -} - impl From for AccountMaybeJSON { fn from(a: Account) -> Self { Self::Binary(a) @@ -1747,22 +1703,13 @@ contextless_resp_eq!( clone ); -#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, FromPyObject)] +#[derive(Serialize, Deserialize, Clone, Debug, PartialEq, Eq, FromPyObject, EnumIntoPy)] #[serde(untagged)] pub enum RpcKeyedAccountMaybeJSON { Binary(RpcKeyedAccount), Parsed(RpcKeyedAccountJsonParsed), } -impl IntoPy for RpcKeyedAccountMaybeJSON { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Parsed(x) => x.into_py(py), - Self::Binary(x) => x.into_py(py), - } - } -} - contextful_resp_eq!( GetProgramAccountsWithContextMaybeJsonParsedResp, Vec @@ -2472,7 +2419,7 @@ impl From for SlotUpdateOriginal { } } -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] #[serde(rename_all = "camelCase", tag = "type")] pub enum SlotUpdate { FirstShredReceived(SlotUpdateFirstShredReceived), @@ -2544,20 +2491,6 @@ impl From for SlotUpdate { } } -impl IntoPy for SlotUpdate { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::FirstShredReceived(x) => x.into_py(py), - Self::Completed(x) => x.into_py(py), - Self::CreatedBank(x) => x.into_py(py), - Self::Frozen(x) => x.into_py(py), - Self::Dead(x) => x.into_py(py), - Self::OptimisticConfirmation(x) => x.into_py(py), - Self::Root(x) => x.into_py(py), - } - } -} - #[derive(Debug, PartialEq, Eq, Serialize, Deserialize, Clone, From, Into)] #[pyclass(module = "solders.rpc.responses", subclass)] pub struct RpcVote(RpcVoteOriginal); @@ -2613,22 +2546,13 @@ pub enum BlockStoreError { BlockStoreError, } -#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, EnumIntoPy)] #[serde(untagged)] pub enum RpcBlockUpdateError { BlockStoreError(BlockStoreError), UnsupportedTransactionVersion(UnsupportedTransactionVersion), } -impl IntoPy for RpcBlockUpdateError { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::BlockStoreError(x) => x.into_py(py), - Self::UnsupportedTransactionVersion(x) => x.into_py(py), - } - } -} - impl From for RpcBlockUpdateErrorOriginal { fn from(e: RpcBlockUpdateError) -> Self { match e { @@ -2768,38 +2692,20 @@ notification_contextless!(SlotUpdateNotification, SlotUpdate); notification_contextless!(RootNotification, u64); notification_contextless!(VoteNotification, RpcVote); -#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, EnumIntoPy)] #[serde(untagged)] pub enum AccountNotificationType { JsonParsed(AccountNotificationJsonParsed), Binary(AccountNotification), } -impl IntoPy for AccountNotificationType { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Binary(x) => x.into_py(py), - Self::JsonParsed(x) => x.into_py(py), - } - } -} - -#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq)] +#[derive(FromPyObject, Serialize, Deserialize, Clone, Debug, PartialEq, Eq, EnumIntoPy)] #[serde(untagged)] pub enum ProgramNotificationType { Binary(ProgramNotification), JsonParsed(ProgramNotificationJsonParsed), } -impl IntoPy for ProgramNotificationType { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Binary(x) => x.into_py(py), - Self::JsonParsed(x) => x.into_py(py), - } - } -} - contextless_resp_eq!(SubscriptionResp, u64); macro_rules ! pyunion_resp { diff --git a/src/transaction_status.rs b/src/transaction_status.rs index 70f1314b..e95626ad 100644 --- a/src/transaction_status.rs +++ b/src/transaction_status.rs @@ -45,7 +45,7 @@ use solana_sdk::{ slot_history::Slot, transaction::TransactionError as TransactionErrorOriginal, transaction_context::TransactionReturnData as TransactionReturnDataOriginal, }; -use solders_macros::{common_methods, enum_original_mapping, richcmp_eq_only}; +use solders_macros::{common_methods, enum_original_mapping, richcmp_eq_only, EnumIntoPy}; use solders_primitives::transaction::{TransactionVersion, VersionedTransaction}; macro_rules! transaction_status_boilerplate { @@ -363,22 +363,13 @@ impl UiPartiallyDecodedInstruction { } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum UiParsedInstruction { Parsed(ParsedInstruction), PartiallyDecoded(UiPartiallyDecodedInstruction), } -impl IntoPy for UiParsedInstruction { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Parsed(m) => m.into_py(py), - Self::PartiallyDecoded(m) => m.into_py(py), - } - } -} - impl From for UiParsedInstructionOriginal { fn from(ix: UiParsedInstruction) -> Self { match ix { @@ -398,22 +389,13 @@ impl From for UiParsedInstruction { } /// A duplicate representation of an Instruction for pretty JSON serialization -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum UiInstruction { Compiled(UiCompiledInstruction), Parsed(UiParsedInstruction), } -impl IntoPy for UiInstruction { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Compiled(m) => m.into_py(py), - Self::Parsed(m) => m.into_py(py), - } - } -} - impl From for UiInstructionOriginal { fn from(ix: UiInstruction) -> Self { match ix { @@ -494,7 +476,7 @@ impl UiParsedMessage { } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum UiMessage { Parsed(UiParsedMessage), @@ -519,15 +501,6 @@ impl From for UiMessageOriginal { } } -impl IntoPy for UiMessage { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Parsed(p) => p.into_py(py), - Self::Raw(r) => r.into_py(py), - } - } -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, From, Into)] #[pyclass(module = "solders.transaction_status", subclass)] pub struct UiTransaction(UiTransactionOriginal); @@ -562,7 +535,7 @@ impl UiTransaction { } } -#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject)] +#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, FromPyObject, EnumIntoPy)] #[serde(rename_all = "camelCase", untagged)] pub enum EncodedVersionedTransaction { Binary(VersionedTransaction), @@ -612,15 +585,6 @@ pub enum EncodedTransaction { Json(UiTransaction), } -impl IntoPy for EncodedVersionedTransaction { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Binary(b) => b.into_py(py), - Self::Json(u) => u.into_py(py), - } - } -} - impl From for EncodedTransaction { fn from(e: EncodedTransactionOriginal) -> Self { match e { @@ -1049,22 +1013,13 @@ pub enum InstructionErrorFieldless { MaxAccountsExceeded, } -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] pub enum InstructionErrorTagged { Custom(InstructionErrorCustom), BorshIoError(InstructionErrorBorshIO), } -impl IntoPy for InstructionErrorTagged { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Custom(x) => x.into_py(py), - Self::BorshIoError(x) => x.into_py(py), - } - } -} - -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] #[serde(untagged)] pub enum InstructionErrorType { Fieldless(InstructionErrorFieldless), @@ -1323,15 +1278,6 @@ impl From for InstructionErrorType { } } -impl IntoPy for InstructionErrorType { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Fieldless(f) => f.into_py(py), - Self::Tagged(t) => t.into_py(py), - } - } -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, From, Into)] #[pyclass(module = "solders.transaction_status", subclass)] pub struct TransactionErrorInstructionError(pub (u8, InstructionErrorType)); @@ -1429,24 +1375,14 @@ pub enum TransactionErrorFieldless { WouldExceedAccountDataTotalLimit, } -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] pub enum TransactionErrorTypeTagged { InstructionError(TransactionErrorInstructionError), DuplicateInstruction(TransactionErrorDuplicateInstruction), InsufficientFundsForRent(TransactionErrorInsufficientFundsForRent), } -impl IntoPy for TransactionErrorTypeTagged { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::InstructionError(e) => e.into_py(py), - Self::DuplicateInstruction(d) => d.into_py(py), - Self::InsufficientFundsForRent(i) => i.into_py(py), - } - } -} - -#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug)] +#[derive(FromPyObject, Clone, PartialEq, Eq, Serialize, Deserialize, Debug, EnumIntoPy)] #[serde(untagged)] pub enum TransactionErrorType { Fieldless(TransactionErrorFieldless), @@ -1643,15 +1579,6 @@ impl From for TransactionErrorType { } } -impl IntoPy for TransactionErrorType { - fn into_py(self, py: Python<'_>) -> PyObject { - match self { - Self::Fieldless(f) => f.into_py(py), - Self::Tagged(t) => t.into_py(py), - } - } -} - #[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize, From, Into)] #[pyclass(module = "solders.transaction_status", subclass)] pub struct Reward(RewardOriginal);