use server_framework::{Decode, Encode}; #[derive(Debug, Clone, Copy, PartialEq, Eq)] pub struct AppId(u16); impl AppId { pub const fn new(id: u16) -> Self { Self(id) } pub const fn raw(&self) -> u16 { self.0 } } #[derive(Debug, PartialEq)] pub enum SessionRequest { Launch { app: AppId }, Kill { app: AppId }, ListApps, ActiveApp, } #[derive(Debug, PartialEq)] pub enum SessionResponse { Launched { app: AppId }, Killed, AppList { count: usize }, Active { app: AppId }, NotFound, Error, } impl Encode for SessionRequest { fn encode(&self) -> [usize; 4] { match self { SessionRequest::Launch { app } => [0, app.raw() as usize, 0, 0], SessionRequest::Kill { app } => [1, app.raw() as usize, 0, 0], SessionRequest::ListApps => [2, 0, 0, 0], SessionRequest::ActiveApp => [3, 0, 0, 0], } } } impl Decode for SessionRequest { fn decode(words: [usize; 4]) -> Option { match words[0] { 0 => Some(SessionRequest::Launch { app: AppId::new(words[1] as u16) }), 1 => Some(SessionRequest::Kill { app: AppId::new(words[1] as u16) }), 2 => Some(SessionRequest::ListApps), 3 => Some(SessionRequest::ActiveApp), _ => None, } } } impl Encode for SessionResponse { fn encode(&self) -> [usize; 4] { match self { SessionResponse::Launched { app } => [0, app.raw() as usize, 0, 0], SessionResponse::Killed => [1, 0, 0, 0], SessionResponse::AppList { count } => [2, *count, 0, 0], SessionResponse::Active { app } => [3, app.raw() as usize, 0, 0], SessionResponse::NotFound => [4, 0, 0, 0], SessionResponse::Error => [5, 0, 0, 0], } } } impl Decode for SessionResponse { fn decode(words: [usize; 4]) -> Option { match words[0] { 0 => Some(SessionResponse::Launched { app: AppId::new(words[1] as u16) }), 1 => Some(SessionResponse::Killed), 2 => Some(SessionResponse::AppList { count: words[1] }), 3 => Some(SessionResponse::Active { app: AppId::new(words[1] as u16) }), 4 => Some(SessionResponse::NotFound), 5 => Some(SessionResponse::Error), _ => None, } } }