feat(server-framework): add Server trait, Encode/Decode, async executor, syscall wrappers, and mock arch for host testing

This commit is contained in:
2026-10-18 02:55:22 +02:00
parent 5c29c0845d
commit c14fe50c23
12 changed files with 303 additions and 23 deletions

View File

@@ -0,0 +1,10 @@
#[cfg(test)]
mod tests;
pub trait Encode {
fn encode(&self) -> [usize; 4];
}
pub trait Decode: Sized {
fn decode(words: [usize; 4]) -> Option<Self>;
}

View File

@@ -0,0 +1,72 @@
use super::*;
#[derive(Debug, PartialEq)]
enum TestRequest {
Ping,
Add(u32, u32),
}
impl Encode for TestRequest {
fn encode(&self) -> [usize; 4] {
match self {
TestRequest::Ping => [0, 0, 0, 0],
TestRequest::Add(a, b) => [1, *a as usize, *b as usize, 0],
}
}
}
impl Decode for TestRequest {
fn decode(words: [usize; 4]) -> Option<Self> {
match words[0] {
0 => Some(TestRequest::Ping),
1 => Some(TestRequest::Add(words[1] as u32, words[2] as u32)),
_ => None,
}
}
}
#[derive(Debug, PartialEq)]
enum TestResponse {
Pong,
Sum(u32),
}
impl Encode for TestResponse {
fn encode(&self) -> [usize; 4] {
match self {
TestResponse::Pong => [0, 0, 0, 0],
TestResponse::Sum(v) => [1, *v as usize, 0, 0],
}
}
}
impl Decode for TestResponse {
fn decode(words: [usize; 4]) -> Option<Self> {
match words[0] {
0 => Some(TestResponse::Pong),
1 => Some(TestResponse::Sum(words[1] as u32)),
_ => None,
}
}
}
#[test]
fn encode_decode_roundtrip() {
let req = TestRequest::Add(3, 7);
let words = req.encode();
let decoded = TestRequest::decode(words).unwrap();
assert_eq!(decoded, TestRequest::Add(3, 7));
}
#[test]
fn decode_invalid_returns_none() {
assert!(TestRequest::decode([99, 0, 0, 0]).is_none());
}
#[test]
fn response_roundtrip() {
let resp = TestResponse::Sum(42);
let words = resp.encode();
let decoded = TestResponse::decode(words).unwrap();
assert_eq!(decoded, TestResponse::Sum(42));
}