Skip to main content

asimov_cli/commands/
protocol.rs

1// This is free and unencumbered software released into the public domain.
2
3use asimov_id::Id;
4use asimov_protocol::Topic;
5use clientele::{StandardOptions, crates::clap::Subcommand};
6use core::error::Error;
7
8#[derive(Debug, Subcommand)]
9pub enum ProtocolCommand {
10    /// Accept and monitor inbound peer traffic
11    #[clap(aliases = ["monitor", "pong"])]
12    Accept {},
13
14    /// Ping a peer node directly
15    Ping {
16        /// The handle to ping
17        handle: Id,
18
19        /// A peer ticket for bootstrapping
20        #[arg(long)]
21        ticket: Option<String>,
22    },
23
24    /// Connect to a peer node directly
25    #[clap(alias = "hello")]
26    Connect {
27        /// The handle to connect to
28        handle: Id,
29
30        /// A peer ticket for bootstrapping
31        #[arg(long)]
32        ticket: Option<String>,
33    },
34
35    /// Publish a message to a gossip topic
36    #[clap(aliases = ["pub", "send"])]
37    Publish {
38        /// The topic to publish to
39        topic: Topic,
40
41        /// The message to publish
42        message: String,
43
44        /// A peer ticket for bootstrapping
45        #[arg(long)]
46        ticket: Option<String>,
47    },
48
49    /// Resolve a handle into a set of peer IDs
50    #[clap(aliases = ["lookup"])]
51    Resolve {
52        /// The handle to resolve
53        handle: Id,
54    },
55
56    /// Subscribe to messages on a gossip topic
57    #[clap(aliases = ["sub", "recv"])]
58    Subscribe {
59        /// The topic to publish to
60        topic: Topic,
61
62        /// A peer ticket for bootstrapping
63        #[arg(long)]
64        ticket: Option<String>,
65    },
66}
67
68impl ProtocolCommand {
69    pub async fn run(&self, flags: &StandardOptions) -> Result<(), Box<dyn Error>> {
70        use ProtocolCommand::*;
71        match self {
72            Accept {} => accept(flags).await,
73            Ping { handle, ticket } => ping(handle, ticket, flags).await,
74            Connect { handle, ticket } => connect(handle, ticket, flags).await,
75            Publish {
76                topic,
77                message,
78                ticket,
79            } => publish(topic, message, ticket, flags).await,
80            Resolve { handle } => resolve(handle, flags).await,
81            Subscribe { topic, ticket } => subscribe(topic, ticket, flags).await,
82        }
83    }
84}
85
86mod accept;
87pub use accept::*;
88
89mod connect;
90pub use connect::*;
91
92mod ping;
93pub use ping::*;
94
95mod publish;
96pub use publish::*;
97
98mod resolve;
99pub use resolve::*;
100
101mod subscribe;
102pub use subscribe::*;