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 node_id;
13mod query;
14mod response;
15
16pub use self::{
17    chunk_proof::{ChunkProof, Nonce},
18    cmd::Cmd,
19    node_id::NodeId,
20    query::Query,
21    response::{CmdResponse, QueryResponse},
22};
23
24use super::NetworkAddress;
25
26use serde::{Deserialize, Serialize};
27
28/// A request to peers in the network
29#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
30pub enum Request {
31    /// A cmd sent to peers. Cmds are writes, i.e. can cause mutation.
32    Cmd(Cmd),
33    /// A query sent to peers. Queries are read-only.
34    Query(Query),
35}
36
37/// A response to peers in the network.
38#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
39pub enum Response {
40    /// The response to a cmd.
41    Cmd(CmdResponse),
42    /// The response to a query.
43    Query(QueryResponse),
44}
45
46impl Request {
47    /// Used to send a request to the close group of the address.
48    pub fn dst(&self) -> NetworkAddress {
49        match self {
50            Request::Cmd(cmd) => cmd.dst(),
51            Request::Query(query) => query.dst(),
52        }
53    }
54}
55
56impl std::fmt::Display for Response {
57    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
58        write!(f, "{self:?}")
59    }
60}