#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum OpCode {
LineageCreate = 0x10,
LineageGet = 0x11,
LineageStimulate = 0x12,
LineageForget = 0x13,
LineageTouch = 0x14,
BondConnect = 0x20,
BondReinforce = 0x21,
BondSever = 0x22,
BondNeighbors = 0x23,
QueryConscious = 0x30,
QueryTopK = 0x31,
QueryTrauma = 0x32,
QueryPattern = 0x33,
SysPing = 0x40,
SysStats = 0x41,
SysSnapshot = 0x42,
SysRestore = 0x43,
SysFreeze = 0x44,
PhysicsTune = 0x45,
SysMoodSet = 0x46,
StreamSubscribe = 0x50,
StreamUnsubscribe = 0x51,
ResponseOk = 0xF0,
ResponseError = 0xF1,
ResponseEvent = 0xF2,
}
impl OpCode {
pub fn from_byte(byte: u8) -> Option<Self> {
match byte {
0x10 => Some(Self::LineageCreate),
0x11 => Some(Self::LineageGet),
0x12 => Some(Self::LineageStimulate),
0x13 => Some(Self::LineageForget),
0x14 => Some(Self::LineageTouch),
0x20 => Some(Self::BondConnect),
0x21 => Some(Self::BondReinforce),
0x22 => Some(Self::BondSever),
0x23 => Some(Self::BondNeighbors),
0x30 => Some(Self::QueryConscious),
0x31 => Some(Self::QueryTopK),
0x32 => Some(Self::QueryTrauma),
0x33 => Some(Self::QueryPattern),
0x40 => Some(Self::SysPing),
0x41 => Some(Self::SysStats),
0x42 => Some(Self::SysSnapshot),
0x43 => Some(Self::SysRestore),
0x44 => Some(Self::SysFreeze),
0x45 => Some(Self::PhysicsTune),
0x46 => Some(Self::SysMoodSet),
0x50 => Some(Self::StreamSubscribe),
0x51 => Some(Self::StreamUnsubscribe),
0xF0 => Some(Self::ResponseOk),
0xF1 => Some(Self::ResponseError),
0xF2 => Some(Self::ResponseEvent),
_ => None,
}
}
#[inline]
pub fn as_byte(self) -> u8 {
self as u8
}
#[inline]
pub fn is_response(self) -> bool {
(self as u8) >= 0xF0
}
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u32)]
pub enum EventMask {
LineageCreated = 1 << 0,
LineageStimulated = 1 << 1,
LineageForgotten = 1 << 2,
BondCreated = 1 << 3,
BondSevered = 1 << 4,
DecayTick = 1 << 5,
SnapshotCreated = 1 << 6,
All = 0xFFFFFFFF,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
#[repr(u8)]
pub enum PhysicsParam {
DecayMultiplier = 0x01,
TraumaThreshold = 0x02,
BondPruneThreshold = 0x03,
MinEnergyThreshold = 0x04,
}
impl PhysicsParam {
pub fn from_byte(byte: u8) -> Option<Self> {
match byte {
0x01 => Some(Self::DecayMultiplier),
0x02 => Some(Self::TraumaThreshold),
0x03 => Some(Self::BondPruneThreshold),
0x04 => Some(Self::MinEnergyThreshold),
_ => None,
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_opcode_roundtrip() {
let op = OpCode::LineageCreate;
assert_eq!(OpCode::from_byte(op.as_byte()), Some(op));
}
#[test]
fn test_opcode_ranges() {
assert!(OpCode::LineageCreate.as_byte() >= 0x10);
assert!(OpCode::LineageCreate.as_byte() < 0x20);
assert!(OpCode::BondConnect.as_byte() >= 0x20);
assert!(OpCode::BondConnect.as_byte() < 0x30);
assert!(OpCode::ResponseOk.is_response());
assert!(!OpCode::SysPing.is_response());
}
}