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
// Copyright 2023 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::{storage::ChunkAddress, NetworkAddress};

use serde::{Deserialize, Serialize};

/// Data queries - retrieving data and inspecting their structure.
///
/// See the [`protocol`] module documentation for more details of the types supported by the Safe
/// Network, and their semantics.
///
/// [`protocol`]: crate
#[allow(clippy::large_enum_variant)]
#[derive(Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize, Debug)]
pub enum Query {
    /// Retrieve the cost of storing a record at the given address.
    GetStoreCost(NetworkAddress),
    /// Retrieve a [`Chunk`] at the given address.
    ///
    /// This should eventually lead to a [`GetChunk`] response.
    ///
    /// [`Chunk`]:  crate::storage::Chunk
    /// [`GetChunk`]: super::QueryResponse::GetChunk
    GetChunk(ChunkAddress),
    /// Retrieve a [`ReplicatedData`] at the given address.
    ///
    /// This should eventually lead to a [`GetReplicatedData`] response.
    ///
    /// [`ReplicatedData`]:  crate::messages::ReplicatedData
    /// [`GetReplicatedData`]: super::QueryResponse::GetReplicatedData
    GetReplicatedData {
        /// Sender of the query
        requester: NetworkAddress,
        /// Address of the data to be fetched
        address: NetworkAddress,
    },
}

impl Query {
    /// Used to send a query to the close group of the address.
    pub fn dst(&self) -> NetworkAddress {
        match self {
            Query::GetStoreCost(address) => address.clone(),
            Query::GetChunk(address) => NetworkAddress::from_chunk_address(*address),
            Query::GetReplicatedData { address, .. } => address.clone(),
        }
    }
}

impl std::fmt::Display for Query {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        match self {
            Query::GetStoreCost(address) => {
                write!(f, "Query::GetStoreCost({address:?})")
            }
            Query::GetChunk(address) => {
                write!(f, "Query::GetChunk({address:?})")
            }
            Query::GetReplicatedData { requester, address } => {
                write!(
                    f,
                    "Query::GetReplicatedData({requester:?} querying {address:?})"
                )
            }
        }
    }
}