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
26// Re-export merkle types from ant-evm
27pub use ant_evm::merkle_payments;
28
29use super::NetworkAddress;
30
31use serde::{Deserialize, Serialize};
32
33/// A request to peers in the network
34#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
35pub enum Request {
36    /// A cmd sent to peers. Cmds are writes, i.e. can cause mutation.
37    Cmd(Cmd),
38    /// A query sent to peers. Queries are read-only.
39    Query(Query),
40}
41
42/// A response to peers in the network.
43#[allow(clippy::large_enum_variant)]
44#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
45pub enum Response {
46    /// The response to a cmd.
47    Cmd(CmdResponse),
48    /// The response to a query.
49    Query(QueryResponse),
50}
51
52impl Request {
53    /// Used to send a request to the close group of the address.
54    pub fn dst(&self) -> NetworkAddress {
55        match self {
56            Request::Cmd(cmd) => cmd.dst(),
57            Request::Query(query) => query.dst(),
58        }
59    }
60}
61
62impl std::fmt::Display for Response {
63    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
64        write!(f, "{self:?}")
65    }
66}
67
68impl std::fmt::Display for Request {
69    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
70        write!(f, "{self:?}")
71    }
72}