gmsol_decode/value/
data.rs1use std::{fmt, marker::PhantomData};
2
3use solana_sdk::pubkey::Pubkey;
4
5use crate::{value::utils::OwnedDataDecoder, Decode, Visitor};
6
7#[derive(Debug, Clone, Copy)]
9pub struct OwnedData<T> {
10 owner: Pubkey,
11 data: T,
12}
13
14impl<T> OwnedData<T> {
15 pub fn owner(&self) -> &Pubkey {
17 &self.owner
18 }
19
20 pub fn data(&self) -> &T {
22 &self.data
23 }
24
25 pub fn into_data(self) -> T {
27 self.data
28 }
29}
30
31impl<T: Decode> Decode for OwnedData<T> {
32 fn decode<D: crate::Decoder>(decoder: D) -> Result<Self, crate::DecodeError> {
33 struct Data<T>(PhantomData<T>);
34
35 impl<T: Decode> Visitor for Data<T> {
36 type Value = OwnedData<T>;
37
38 fn visit_owned_data(
39 self,
40 program_id: &Pubkey,
41 data: &[u8],
42 ) -> Result<Self::Value, crate::DecodeError> {
43 let data = T::decode(OwnedDataDecoder::new(program_id, data))?;
44 let program_id = *program_id;
45 Ok(OwnedData {
46 owner: program_id,
47 data,
48 })
49 }
50 }
51
52 decoder.decode_owned_data(Data::<T>(PhantomData))
53 }
54}
55
56#[derive(Clone)]
58pub struct UnknownOwnedData {
59 program_id: Pubkey,
60 data: Vec<u8>,
61}
62
63impl fmt::Debug for UnknownOwnedData {
64 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
65 use base64::prelude::*;
66 write!(
67 f,
68 "UnknownData({} => {})",
69 self.program_id,
70 BASE64_STANDARD.encode(&self.data)
71 )
72 }
73}
74
75impl Decode for UnknownOwnedData {
76 fn decode<D: crate::Decoder>(decoder: D) -> Result<Self, crate::DecodeError> {
77 struct Data;
78
79 impl Visitor for Data {
80 type Value = UnknownOwnedData;
81
82 fn visit_owned_data(
83 self,
84 program_id: &Pubkey,
85 data: &[u8],
86 ) -> Result<Self::Value, crate::DecodeError> {
87 Ok(UnknownOwnedData {
88 program_id: *program_id,
89 data: data.to_owned(),
90 })
91 }
92 }
93
94 decoder.decode_owned_data(Data)
95 }
96}