light_token_interface/state/extensions/
compressible.rs

1use aligned_sized::aligned_sized;
2use light_compressible::compression_info::CompressionInfo;
3use light_zero_copy::{ZeroCopy, ZeroCopyMut};
4
5use crate::{AnchorDeserialize, AnchorSerialize};
6
7/// Compressible extension for token accounts.
8/// This extension contains compression configuration and timing data.
9#[derive(
10    Debug,
11    Clone,
12    Hash,
13    Copy,
14    PartialEq,
15    Eq,
16    AnchorSerialize,
17    AnchorDeserialize,
18    ZeroCopy,
19    ZeroCopyMut,
20)]
21#[repr(C)]
22#[aligned_sized]
23pub struct CompressibleExtension {
24    /// Option discriminator for decimals (0 = None, 1 = Some)
25    pub decimals_option: u8,
26    /// Token decimals (only valid when decimals_option == 1)
27    pub decimals: u8,
28    /// Whether this account is compression-only (cannot decompress)
29    pub compression_only: bool,
30    /// Whether the source account is an ATA (1 = ATA, 0 = regular account)
31    /// Used during compress_and_close to set is_ata in CompressedOnlyExtension
32    pub is_ata: u8,
33    /// Compression configuration and timing data
34    pub info: CompressionInfo,
35}
36
37impl CompressibleExtension {
38    /// Returns the decimals if present
39    pub fn decimals(&self) -> Option<u8> {
40        if self.decimals_option == 1 {
41            Some(self.decimals)
42        } else {
43            None
44        }
45    }
46
47    /// Sets the decimals
48    pub fn set_decimals(&mut self, decimals: Option<u8>) {
49        match decimals {
50            Some(d) => {
51                self.decimals_option = 1;
52                self.decimals = d;
53            }
54            None => {
55                self.decimals_option = 0;
56                self.decimals = 0;
57            }
58        }
59    }
60}
61
62// Getters on zero-copy immutable view
63impl ZCompressibleExtension<'_> {
64    /// Returns the decimals if present
65    #[inline(always)]
66    pub fn decimals(&self) -> Option<u8> {
67        if self.decimals_option == 1 {
68            Some(self.decimals)
69        } else {
70            None
71        }
72    }
73}
74
75// Getters and setters on zero-copy mutable view
76impl ZCompressibleExtensionMut<'_> {
77    /// Returns the decimals if present
78    #[inline(always)]
79    pub fn decimals(&self) -> Option<u8> {
80        if self.decimals_option == 1 {
81            Some(self.decimals)
82        } else {
83            None
84        }
85    }
86
87    /// Sets the decimals value
88    #[inline(always)]
89    pub fn set_decimals(&mut self, decimals: Option<u8>) {
90        match decimals {
91            Some(d) => {
92                self.decimals_option = 1;
93                self.decimals = d;
94            }
95            None => {
96                self.decimals_option = 0;
97                self.decimals = 0;
98            }
99        }
100    }
101
102    /// Returns whether this account is an ATA
103    #[inline(always)]
104    pub fn is_ata(&self) -> bool {
105        self.is_ata != 0
106    }
107}