123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106 |
- use std::future::{ready, Ready};
- use btlib::Result;
- use btproto::protocol;
- use btrun::{CallMsg, End};
- use serde::{Deserialize, Serialize};
- #[derive(Serialize, Deserialize)]
- pub struct Ping;
- impl CallMsg for Ping {
- type Reply = ();
- }
- /// Tests that the given variable is of the given type.
- macro_rules! assert_type {
- ($var:expr, $ty:ty) => {{
- let _: $ty = $var;
- }};
- }
- #[test]
- fn minimal_syntax() {
- pub struct Msg;
- protocol! {
- named MinimalTest;
- let states = [Server];
- let client = [Client];
- Client -> End, >service(Server)!Msg;
- Server?Msg -> End;
- }
- let msg: Option<MinimalTestMsgs> = None;
- match msg {
- Some(MinimalTestMsgs::Msg(act)) => assert_type!(act, Msg),
- None => (),
- }
- struct ServerState;
- impl Server for ServerState {
- type HandleMsgFut = Ready<Result<End>>;
- fn handle_msg(self, _msg: Msg) -> Self::HandleMsgFut {
- ready(Ok(End))
- }
- }
- struct ClientState;
- impl Client for ClientState {
- type SendMsgFut = Ready<Result<End>>;
- fn send_msg(self, _msg: Msg) -> Self::SendMsgFut {
- ready(Ok(End))
- }
- }
- }
- #[test]
- fn reply() {
- protocol! {
- named ReplyTest;
- let server = [Listening];
- let client = [Client, Waiting];
- Client -> Waiting, >service(Listening)!Ping;
- Listening?Ping -> Listening, >Waiting!Ping::Reply;
- Waiting?Ping::Reply -> End;
- }
- let msg: Option<ReplyTestMsgs> = None;
- match msg {
- Some(ReplyTestMsgs::Ping(ping)) => assert_type!(ping, Ping),
- Some(ReplyTestMsgs::PingReply(reply)) => assert_type!(reply, <Ping as CallMsg>::Reply),
- None => (),
- }
- struct ListeningState;
- impl Listening for ListeningState {
- type HandlePingListening = Self;
- type HandlePingFut = Ready<Result<(Self, <Ping as CallMsg>::Reply)>>;
- fn handle_ping(self, _msg: Ping) -> Self::HandlePingFut {
- ready(Ok((self, ())))
- }
- }
- struct ClientState;
- impl Client for ClientState {
- type SendPingWaiting = WaitingState;
- type SendPingFut = Ready<Result<(WaitingState, <Ping as CallMsg>::Reply)>>;
- fn send_ping(self, _ping: Ping) -> Self::SendPingFut {
- ready(Ok((WaitingState, ())))
- }
- }
- struct WaitingState;
- // TODO: This state should not be generated, as it is never observed.
- impl Waiting for WaitingState {
- type HandlePingReplyFut = Ready<Result<End>>;
- fn handle_ping_reply(self, _msg: ()) -> Self::HandlePingReplyFut {
- ready(Ok(End))
- }
- }
- }
|