light_sdk/interface/traits.rs
1//! Traits for decompression variant construction.
2//!
3//! These traits enable ergonomic client-side construction of `RentFreeDecompressAccount`
4//! from seeds and compressed account data.
5
6#[cfg(feature = "anchor")]
7use anchor_lang::error::Error;
8#[cfg(not(feature = "anchor"))]
9use solana_program_error::ProgramError as Error;
10
11/// Trait for seeds that can construct a compressed account variant.
12///
13/// Implemented by generated `XxxSeeds` structs (e.g., `UserRecordSeeds`).
14/// The macro generates impls that deserialize account data and verify seeds match.
15///
16/// # Example (generated code)
17/// ```ignore
18/// impl IntoVariant<RentFreeAccountVariant> for UserRecordSeeds {
19/// fn into_variant(self, data: &[u8]) -> Result<RentFreeAccountVariant, Error> {
20/// RentFreeAccountVariant::user_record(data, self)
21/// }
22/// }
23/// ```
24pub trait IntoVariant<V> {
25 /// Construct variant from compressed account data bytes and these seeds.
26 ///
27 /// # Arguments
28 /// * `data` - Raw compressed account data bytes
29 ///
30 /// # Returns
31 /// The constructed variant on success, or an error if:
32 /// - Deserialization fails
33 /// - Seed verification fails (data.* seeds don't match account data)
34 fn into_variant(self, data: &[u8]) -> Result<V, Error>;
35}
36
37/// Trait for CToken account variant types that can construct a full variant with token data.
38///
39/// Implemented by generated `TokenAccountVariant` enum.
40/// The macro generates the impl that wraps variant + token_data into `RentFreeAccountVariant`.
41///
42/// # Example (generated code)
43/// ```ignore
44/// impl IntoCTokenVariant<RentFreeAccountVariant> for TokenAccountVariant {
45/// fn into_ctoken_variant(self, token_data: TokenData) -> RentFreeAccountVariant {
46/// RentFreeAccountVariant::CTokenData(CTokenData {
47/// variant: self,
48/// token_data,
49/// })
50/// }
51/// }
52/// ```
53///
54/// Type parameter `T` is typically `light_token::compat::TokenData`.
55pub trait IntoCTokenVariant<V, T> {
56 /// Construct variant from CToken variant and token data.
57 ///
58 /// # Arguments
59 /// * `token_data` - The parsed `TokenData` from compressed account bytes
60 ///
61 /// # Returns
62 /// The constructed variant containing both CToken variant and token data
63 fn into_ctoken_variant(self, token_data: T) -> V;
64}