safe_nd/messaging/
query.rs

1// Copyright 2019 MaidSafe.net limited.
2//
3// This SAFE Network Software is licensed to you under the MIT license <LICENSE-MIT
4// https://opensource.org/licenses/MIT> or the Modified BSD license <LICENSE-BSD
5// https://opensource.org/licenses/BSD-3-Clause>, at your option. This file may not be copied,
6// modified, or distributed except according to those terms. Please review the Licences for the
7// specific language governing permissions and limitations relating to use of the SAFE Network
8// Software.
9
10use super::{
11    auth::AuthQuery, data::DataQuery, transfer::TransferQuery, AuthorisationKind, QueryResponse,
12};
13use crate::{Error, XorName};
14use serde::{Deserialize, Serialize};
15
16/// TODO: docs
17#[allow(clippy::large_enum_variant)]
18#[derive(Hash, Eq, PartialEq, Debug, Clone, Serialize, Deserialize)]
19pub enum Query {
20    ///
21    Auth(AuthQuery),
22    ///
23    Data(DataQuery),
24    ///
25    Transfer(TransferQuery),
26}
27
28impl Query {
29    /// Returns the type of authorisation needed for the request.
30    pub fn authorisation_kind(&self) -> AuthorisationKind {
31        use Query::*;
32        match self {
33            Auth(q) => q.authorisation_kind(),
34            Data(q) => q.authorisation_kind(),
35            Transfer(q) => q.authorisation_kind(),
36        }
37    }
38
39    /// Creates a Response containing an error, with the Response variant corresponding to the
40    /// Request variant.
41    pub fn error(&self, error: Error) -> QueryResponse {
42        use Query::*;
43        match self {
44            Auth(q) => q.error(error),
45            Data(q) => q.error(error),
46            Transfer(q) => q.error(error),
47        }
48    }
49
50    /// Returns the address of the destination for `request`.
51    pub fn dst_address(&self) -> XorName {
52        use Query::*;
53        match self {
54            Auth(q) => q.dst_address(),
55            Data(q) => q.dst_address(),
56            Transfer(q) => q.dst_address(),
57        }
58    }
59}