use crate::structures::util::read_i32;
#[derive(Debug, Clone, Copy, Default)]
pub struct GoMethod {
pub name: i32,
pub mtyp: i32,
pub ifn: i32,
pub tfn: i32,
}
impl GoMethod {
pub const SIZE: usize = 16;
pub fn parse(data: &[u8]) -> Option<Self> {
if data.len() < Self::SIZE {
return None;
}
Some(Self {
name: read_i32(data, 0)?,
mtyp: read_i32(data, 4)?,
ifn: read_i32(data, 8)?,
tfn: read_i32(data, 12)?,
})
}
}
#[derive(Debug, Clone, Copy, Default)]
pub struct GoImethod {
pub name: i32,
pub typ: i32,
}
impl GoImethod {
pub const SIZE: usize = 8;
pub fn parse(data: &[u8]) -> Option<Self> {
if data.len() < Self::SIZE {
return None;
}
Some(Self {
name: read_i32(data, 0)?,
typ: read_i32(data, 4)?,
})
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn parse_gomethod_valid() {
let mut buf = vec![0u8; 16];
buf[0..4].copy_from_slice(&10i32.to_le_bytes());
buf[4..8].copy_from_slice(&20i32.to_le_bytes());
buf[8..12].copy_from_slice(&(-30i32).to_le_bytes());
buf[12..16].copy_from_slice(&40i32.to_le_bytes());
let m = GoMethod::parse(&buf).unwrap();
assert_eq!(m.name, 10);
assert_eq!(m.mtyp, 20);
assert_eq!(m.ifn, -30);
assert_eq!(m.tfn, 40);
}
#[test]
fn parse_gomethod_too_short() {
let buf = vec![0u8; 15];
assert!(GoMethod::parse(&buf).is_none());
}
#[test]
fn parse_goimethod_valid() {
let mut buf = vec![0u8; 8];
buf[0..4].copy_from_slice(&100i32.to_le_bytes());
buf[4..8].copy_from_slice(&(-200i32).to_le_bytes());
let im = GoImethod::parse(&buf).unwrap();
assert_eq!(im.name, 100);
assert_eq!(im.typ, -200);
}
#[test]
fn parse_goimethod_too_short() {
let buf = vec![0u8; 7];
assert!(GoImethod::parse(&buf).is_none());
}
}