plexus_substrate/activations/echo/
activation.rs1use super::types::EchoEvent;
14use async_stream::stream;
15use futures::Stream;
16use std::time::Duration;
17
18#[derive(Clone)]
20pub struct Echo;
21
22impl Echo {
23 pub fn new() -> Self {
24 Echo
25 }
26}
27
28impl Default for Echo {
29 fn default() -> Self {
30 Self::new()
31 }
32}
33
34#[plexus_macros::activation(namespace = "echo",
40version = "1.0.0",
41description = "Echo messages back - demonstrates plexus-macros usage", crate_path = "plexus_core")]
42impl Echo {
43 #[plexus_macros::method(description = "Echo a message back the specified number of times",
45 params(
46 message = "The message to echo",
47 count = "Number of times to repeat (default: 1)"
48 ))]
49 async fn echo(
50 &self,
51 message: String,
52 count: u32,
53 ) -> impl Stream<Item = EchoEvent> + Send + 'static {
54 let count = if count == 0 { 1 } else { count };
55 stream! {
56 for i in 0..count {
57 if i > 0 {
58 tokio::time::sleep(Duration::from_millis(500)).await;
59 }
60 yield EchoEvent::Echo {
61 message: message.clone(),
62 count: i + 1,
63 };
64 }
65 }
66 }
67
68 #[plexus_macros::method(description = "Echo a message once",
70 params(message = "The message to echo"))]
71 async fn once(&self, message: String) -> impl Stream<Item = EchoEvent> + Send + 'static {
72 stream! {
73 yield EchoEvent::Echo {
74 message,
75 count: 1,
76 };
77 }
78 }
79
80 #[plexus_macros::method(description = "Ping — returns a Pong response")]
82 async fn ping(&self) -> impl Stream<Item = EchoEvent> + Send + 'static {
83 stream! {
84 yield EchoEvent::Pong;
85 }
86 }
87}