ant_protocol/messages.rs
1// Copyright 2024 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
4// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
5// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
6// KIND, either express or implied. Please review the Licences for the specific language governing
7// permissions and limitations relating to use of the SAFE Network Software.
8
9//! Data messages and their possible responses.
10mod chunk_proof;
11mod cmd;
12mod connection_info;
13mod node_id;
14mod query;
15mod response;
16
17pub use self::{
18 chunk_proof::{ChunkProof, Nonce},
19 cmd::Cmd,
20 connection_info::ConnectionInfo,
21 node_id::NodeId,
22 query::Query,
23 response::{CmdResponse, QueryResponse},
24};
25
26use super::NetworkAddress;
27
28use serde::{Deserialize, Serialize};
29
30/// A request to peers in the network
31#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
32pub enum Request {
33 /// A cmd sent to peers. Cmds are writes, i.e. can cause mutation.
34 Cmd(Cmd),
35 /// A query sent to peers. Queries are read-only.
36 Query(Query),
37}
38
39/// A response to peers in the network.
40#[allow(clippy::large_enum_variant)]
41#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
42pub enum Response {
43 /// The response to a cmd.
44 Cmd(CmdResponse),
45 /// The response to a query.
46 Query(QueryResponse),
47}
48
49impl Request {
50 /// Used to send a request to the close group of the address.
51 pub fn dst(&self) -> NetworkAddress {
52 match self {
53 Request::Cmd(cmd) => cmd.dst(),
54 Request::Query(query) => query.dst(),
55 }
56 }
57}
58
59impl std::fmt::Display for Response {
60 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
61 write!(f, "{self:?}")
62 }
63}