test-server.rs 925 B

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  1. use anyhow::Result;
  2. struct Adder {
  3. acc: u8,
  4. }
  5. impl cborpc::Protocol for Adder {
  6. type State = u8;
  7. fn name(&self) -> String {
  8. "adder-0".into()
  9. }
  10. fn method(&mut self, state: &mut Self::State, method_name: &str, _message: &[u8]) -> Result<cborpc::CallResponse> {
  11. match method_name {
  12. "add" => {
  13. self.acc += 1;
  14. *state = self.acc + 1;
  15. Ok(cborpc::CallResponse {
  16. success: true,
  17. message: [self.acc, *state].into(),
  18. })
  19. }
  20. _ => self.method_missing(method_name),
  21. }
  22. }
  23. }
  24. #[tokio::main]
  25. async fn main() -> Result<()> {
  26. let state = 0;
  27. let mut responder = cborpc::Responder::new(state);
  28. {
  29. responder.add_protocol(Adder { acc: 0 });
  30. }
  31. loop {
  32. match responder.answer_call(&mut tokio::io::stdin(), &mut tokio::io::stdout()).await {
  33. Ok(Some(success)) => {
  34. eprint!("Call answered, success={}", success);
  35. Ok(())
  36. }
  37. Ok(None) => Ok(()),
  38. Err(error) => Err(error),
  39. }?;
  40. }
  41. }