feat(input-server): add InputServer<D: InputDevice> with protocol, mock + UART adapters, and tests
This commit is contained in:
36
crates/servers/input-server/src/server.rs
Normal file
36
crates/servers/input-server/src/server.rs
Normal file
@@ -0,0 +1,36 @@
|
||||
use server_framework::Server;
|
||||
|
||||
use crate::input_device::InputDevice;
|
||||
use crate::protocol::{InputRequest, InputResponse};
|
||||
|
||||
pub struct InputServer<D: InputDevice> {
|
||||
device: D,
|
||||
}
|
||||
|
||||
impl<D: InputDevice> InputServer<D> {
|
||||
pub fn new(device: D) -> Self {
|
||||
Self { device }
|
||||
}
|
||||
|
||||
pub fn device(&self) -> &D {
|
||||
&self.device
|
||||
}
|
||||
}
|
||||
|
||||
impl<D: InputDevice> Server for InputServer<D> {
|
||||
type Request = InputRequest;
|
||||
type Response = InputResponse;
|
||||
|
||||
fn handle(&mut self, request: InputRequest) -> InputResponse {
|
||||
match request {
|
||||
InputRequest::PollEvent => match self.device.poll() {
|
||||
Some(event) => InputResponse::Event(event),
|
||||
None => InputResponse::NoEvent,
|
||||
},
|
||||
InputRequest::WaitEvent => match self.device.poll() {
|
||||
Some(event) => InputResponse::Event(event),
|
||||
None => InputResponse::NoEvent,
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user