|
| 1 | +use steel::*; |
| 2 | + |
| 3 | +#[repr(u8)] |
| 4 | +#[derive(Clone, Copy, Debug, Eq, PartialEq, TryFromPrimitive)] |
| 5 | +pub enum ProcessingInstructionsInstruction { |
| 6 | + GoToThePark = 0, |
| 7 | +} |
| 8 | + |
| 9 | +#[repr(C)] |
| 10 | +#[derive(Clone, Copy, Debug, Pod, Zeroable, PartialEq)] |
| 11 | +pub struct GoToTheParkData { |
| 12 | + pub name: [u8; 64], |
| 13 | + pub height: [u8; 8], |
| 14 | +} |
| 15 | + |
| 16 | +impl_to_bytes!(GoToTheParkData); |
| 17 | + |
| 18 | +fn string_to_bytes(s: &str) -> [u8; 64] { |
| 19 | + let mut bytes = [0; 64]; |
| 20 | + let s_bytes = s.as_bytes(); |
| 21 | + let len = s_bytes.len().min(64); |
| 22 | + bytes[..len].copy_from_slice(&s_bytes[..len]); |
| 23 | + bytes |
| 24 | +} |
| 25 | + |
| 26 | +impl GoToTheParkData { |
| 27 | + pub fn new(name: String, height: u64) -> Self { |
| 28 | + Self { |
| 29 | + name: string_to_bytes(&name), |
| 30 | + height: height.to_le_bytes(), |
| 31 | + } |
| 32 | + } |
| 33 | + |
| 34 | + pub fn try_from_bytes(data: &[u8]) -> Result<&Self, ProgramError> { |
| 35 | + bytemuck::try_from_bytes(data).or(Err(ProgramError::InvalidInstructionData)) |
| 36 | + } |
| 37 | + |
| 38 | + pub fn name(&self) -> String { |
| 39 | + String::from_utf8_lossy(&self.name) |
| 40 | + .trim_end_matches(char::from(0)) |
| 41 | + .to_string() |
| 42 | + } |
| 43 | + |
| 44 | + pub fn height(&self) -> u64 { |
| 45 | + u64::from_le_bytes(self.height) |
| 46 | + } |
| 47 | +} |
| 48 | + |
| 49 | +#[repr(C)] |
| 50 | +#[derive(Clone, Copy, Debug, Pod, Zeroable)] |
| 51 | +pub struct GoToThePark { |
| 52 | + pub data: GoToTheParkData, |
| 53 | +} |
| 54 | + |
| 55 | +instruction!(ProcessingInstructionsInstruction, GoToThePark); |
0 commit comments