Jelajahi Sumber

Added an empty implementation of the protocol! macro
so protocols can be uncommented.

Matthew Carr 1 tahun lalu
induk
melakukan
f1ae473dd2

+ 12 - 0
Cargo.lock

@@ -411,6 +411,17 @@ dependencies = [
  "tempdir",
 ]
 
+[[package]]
+name = "btproto"
+version = "0.1.0"
+
+[[package]]
+name = "btproto-tests"
+version = "0.1.0"
+dependencies = [
+ "btproto",
+]
+
 [[package]]
 name = "btprovision"
 version = "0.1.0"
@@ -432,6 +443,7 @@ dependencies = [
  "anyhow",
  "btlib",
  "btlib-tests",
+ "btproto",
  "btserde",
  "bttp",
  "bytes",

+ 9 - 0
crates/btproto-tests/Cargo.toml

@@ -0,0 +1,9 @@
+[package]
+name = "btproto-tests"
+version = "0.1.0"
+edition = "2021"
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]
+btproto = { path = "../btproto" }

+ 2 - 0
crates/btproto-tests/src/lib.rs

@@ -0,0 +1,2 @@
+#[cfg(test)]
+mod tests;

+ 58 - 0
crates/btproto-tests/src/tests.rs

@@ -0,0 +1,58 @@
+use btproto::protocol;
+
+mod ping_proto {
+    use super::*;
+
+    protocol! {
+        ClientInit?Activate -> SentPing, Listening!Ping
+        ServerInit?Activate -> Listening
+        Listening?Ping -> End, SentPing!Ping::Reply
+        SentPing?Ping::Reply -> End
+    }
+
+    // This protocol definition is observed by the procedural macro as the following sequence of
+    // tokens:
+    // Ident { ident: "ClientInit", span: #0 bytes(109..119) }
+    // Punct { ch: '?', spacing: Alone, span: #0 bytes(119..120) }
+    // Ident { ident: "Activate", span: #0 bytes(120..128) }
+    // Punct { ch: '-', spacing: Joint, span: #0 bytes(129..130) }
+    // Punct { ch: '>', spacing: Alone, span: #0 bytes(130..131) }
+    // Ident { ident: "SentPing", span: #0 bytes(132..140) }
+    // Punct { ch: ',', spacing: Alone, span: #0 bytes(140..141) }
+    // Ident { ident: "Listening", span: #0 bytes(142..151) }
+    // Punct { ch: '!', spacing: Alone, span: #0 bytes(151..152) }
+    // Ident { ident: "Ping", span: #0 bytes(152..156) }
+    // Ident { ident: "ServerInit", span: #0 bytes(165..175) }
+    // Punct { ch: '?', spacing: Alone, span: #0 bytes(175..176) }
+    // Ident { ident: "Activate", span: #0 bytes(176..184) }
+    // Punct { ch: '-', spacing: Joint, span: #0 bytes(185..186) }
+    // Punct { ch: '>', spacing: Alone, span: #0 bytes(186..187) }
+    // Ident { ident: "Listening", span: #0 bytes(188..197) }
+    // Ident { ident: "Listening", span: #0 bytes(206..215) }
+    // Punct { ch: '?', spacing: Alone, span: #0 bytes(215..216) }
+    // Ident { ident: "Ping", span: #0 bytes(216..220) }
+    // Punct { ch: '-', spacing: Joint, span: #0 bytes(221..222) }
+    // Punct { ch: '>', spacing: Alone, span: #0 bytes(222..223) }
+    // Ident { ident: "End", span: #0 bytes(224..227) }
+    // Punct { ch: ',', spacing: Alone, span: #0 bytes(227..228) }
+    // Ident { ident: "SentPing", span: #0 bytes(229..237) }
+    // Punct { ch: '!', spacing: Alone, span: #0 bytes(237..238) }
+    // Ident { ident: "Ping", span: #0 bytes(238..242) }
+    // Punct { ch: ':', spacing: Joint, span: #0 bytes(242..243) }
+    // Punct { ch: ':', spacing: Alone, span: #0 bytes(243..244) }
+    // Ident { ident: "Reply", span: #0 bytes(244..249) }
+    // Ident { ident: "SentPing", span: #0 bytes(258..266) }
+    // Punct { ch: '?', spacing: Alone, span: #0 bytes(266..267) }
+    // Ident { ident: "Ping", span: #0 bytes(267..271) }
+    // Punct { ch: ':', spacing: Joint, span: #0 bytes(271..272) }
+    // Punct { ch: ':', spacing: Alone, span: #0 bytes(272..273) }
+    // Ident { ident: "Reply", span: #0 bytes(273..278) }
+    // Punct { ch: '-', spacing: Joint, span: #0 bytes(279..280) }
+    // Punct { ch: '>', spacing: Alone, span: #0 bytes(280..281) }
+    // Ident { ident: "End", span: #0 bytes(282..285) }
+
+    #[test]
+    fn traits_created() {
+        assert!(true);
+    }
+}

+ 11 - 0
crates/btproto/Cargo.toml

@@ -0,0 +1,11 @@
+[package]
+name = "btproto"
+version = "0.1.0"
+edition = "2021"
+
+[lib]
+proc-macro = true
+
+# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
+
+[dependencies]

+ 7 - 0
crates/btproto/src/lib.rs

@@ -0,0 +1,7 @@
+extern crate proc_macro;
+use proc_macro::TokenStream;
+
+#[proc_macro]
+pub fn protocol(_tokens: TokenStream) -> TokenStream {
+    TokenStream::new()
+}

+ 1 - 0
crates/btrun/Cargo.toml

@@ -9,6 +9,7 @@ edition = "2021"
 btlib = { path = "../btlib" }
 bttp = { path = "../bttp" }
 btserde = { path = "../btserde" }
+btproto = { path = "../btproto" }
 tokio = { version = "1.23.0", features = ["rt-multi-thread"] }
 futures = "0.3.25"
 serde = { version = "^1.0.136", features = ["derive"] }

+ 18 - 17
crates/btrun/src/lib.rs

@@ -671,6 +671,7 @@ mod tests {
         log::BuilderExt,
     };
     use btlib_tests::TEST_STORE;
+    use btproto::protocol;
     use btserde::to_vec;
     use bttp::BlockAddr;
     use ctor::ctor;
@@ -806,12 +807,12 @@ mod tests {
     // The following code is a proof-of-concept for what types should be generated for a
     // simple ping-pong protocol:
     //
-    // protocol! {
-    //     ClientInit?Activate -> SentPing, Listening!Ping
-    //     ServerInit?Activate -> Listening
-    //     Listening?Ping -> End, SentPing!Ping::Reply
-    //     SentPing?Ping::Reply -> End
-    // }
+    protocol! {
+        ClientInit?Activate -> SentPing, Listening!Ping
+        ServerInit?Activate -> Listening
+        Listening?Ping -> End, SentPing!Ping::Reply
+        SentPing?Ping::Reply -> End
+    }
     //
     // In words, the protocol is described as follows.
     // 1. The ClientInit state receives the Activate message. It returns the SentPing state and a
@@ -1013,17 +1014,17 @@ mod tests {
     // Here's another protocol example. This is the Customer and Travel Agency protocol used as an
     // example in the survey paper "Behavioral Types in Programming Languages."
     //
-    // protocol! {
-    //     CustomerInit?Activate -> Choosing
-    //     AgencyInit?Activate -> Listening
-    //     Choosing?Choice -> Choosing, Listening!Query|Accept|Reject
-    //     Listening?Query -> Listening, Choosing!Query::Reply
-    //     Choosing?Query::Reply -> Choosing
-    //     Listening?Accept -> End, Choosing!Accept::Reply
-    //     Choosing?Accept::Reply -> End
-    //     Listening?Reject -> End, Choosing!Reject:Reply
-    //     Choosing?Reject::Reply -> End
-    // }
+    protocol! {
+        CustomerInit?Activate -> Choosing
+        AgencyInit?Activate -> Listening
+        Choosing?Choice -> Choosing, Listening!Query|Accept|Reject
+        Listening?Query -> Listening, Choosing!Query::Reply
+        Choosing?Query::Reply -> Choosing
+        Listening?Accept -> End, Choosing!Accept::Reply
+        Choosing?Accept::Reply -> End
+        Listening?Reject -> End, Choosing!Reject:Reply
+        Choosing?Reject::Reply -> End
+    }
     //
     // The Choice message is from the runtime itself. It represents receiving input from a user.
 }