1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// Copyright 2024 MaidSafe.net limited.
//
// This SAFE Network Software is licensed to you under The General Public License (GPL), version 3.
// Unless required by applicable law or agreed to in writing, the SAFE Network Software distributed
// under the GPL Licence is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. Please review the Licences for the specific language governing
// permissions and limitations relating to use of the SAFE Network Software.

use crate::{error::Result, NetworkAddress};

use super::ChunkProof;
use bytes::Bytes;
use core::fmt;
use serde::{Deserialize, Serialize};
use sn_transfers::{MainPubkey, PaymentQuote};
use std::fmt::Debug;

/// The response to a query, containing the query result.
#[allow(clippy::large_enum_variant)]
#[derive(Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum QueryResponse {
    // ===== GetStoreCost =====
    //
    /// Response to [`GetStoreCost`]
    ///
    /// [`GetStoreCost`]: crate::messages::Query::GetStoreCost
    GetStoreCost {
        /// The store cost quote for storing the next record.
        quote: Result<PaymentQuote>,
        /// The cash_note MainPubkey to pay this node's store cost to.
        payment_address: MainPubkey,
        /// Node's Peer Address
        peer_address: NetworkAddress,
    },
    CheckNodeInProblem {
        /// Address of the peer that queried
        reporter_address: NetworkAddress,
        /// Address of the target to be queried
        target_address: NetworkAddress,
        /// Status flag indicating whether the target is in trouble
        is_in_trouble: bool,
    },
    // ===== ReplicatedRecord =====
    //
    /// Response to [`GetReplicatedRecord`]
    ///
    /// [`GetReplicatedRecord`]: crate::messages::Query::GetReplicatedRecord
    GetReplicatedRecord(Result<(NetworkAddress, Bytes)>),
    // ===== ReplicatedRecord =====
    //
    /// Response to [`GetChunkExistenceProof`]
    ///
    /// [`GetChunkExistenceProof`]: crate::messages::Query::GetChunkExistenceProof
    GetChunkExistenceProof(Result<ChunkProof>),
}

// Debug implementation for QueryResponse, to avoid printing Vec<u8>
impl Debug for QueryResponse {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            QueryResponse::GetStoreCost {
                quote,
                payment_address,
                peer_address,
            } => {
                write!(
                    f,
                    "GetStoreCost(quote: {quote:?}, from {peer_address:?} w/ payment_address: {payment_address:?})"
                )
            }
            QueryResponse::CheckNodeInProblem {
                reporter_address,
                target_address,
                is_in_trouble,
            } => {
                write!(
                    f,
                    "CheckNodeInProblem({reporter_address:?} report target {target_address:?} as {is_in_trouble:?} in problem"
                )
            }
            QueryResponse::GetReplicatedRecord(result) => match result {
                Ok((holder, data)) => {
                    write!(
                        f,
                        "GetReplicatedRecord(Ok((holder: {:?}, datalen: {:?})))",
                        holder,
                        data.len()
                    )
                }
                Err(err) => {
                    write!(f, "GetReplicatedRecord(Err({err:?}))")
                }
            },
            QueryResponse::GetChunkExistenceProof(proof) => {
                write!(f, "GetChunkExistenceProof(proof: {proof:?})")
            }
        }
    }
}

/// The response to a Cmd, containing the query result.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CmdResponse {
    //
    // ===== Replication =====
    //
    /// Response to replication cmd
    Replicate(Result<()>),
}

/// The Ok variant of a CmdResponse
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub enum CmdOk {
    StoredSuccessfully,
    DataAlreadyPresent,
}