safe_nd/messaging/
data.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    account::{AccountRead, AccountWrite},
12    blob::{BlobRead, BlobWrite},
13    map::{MapRead, MapWrite},
14    sequence::{SequenceRead, SequenceWrite},
15    AuthorisationKind, CmdError, QueryResponse,
16};
17use crate::{Error, XorName};
18use serde::{Deserialize, Serialize};
19use std::fmt;
20
21/// TODO: docs
22#[allow(clippy::large_enum_variant)]
23#[derive(Hash, Eq, PartialEq, Clone, Serialize, Deserialize)]
24pub enum DataCmd {
25    /// TODO: docs
26    Blob(BlobWrite),
27    /// TODO: docs
28    Map(MapWrite),
29    /// TODO: docs
30    Sequence(SequenceWrite),
31    /// Use this only while we don't
32    /// have Authenticator as its own app.
33    Account(AccountWrite), // <- "LoginPacket"
34}
35
36impl DataCmd {
37    /// Creates a Response containing an error, with the Response variant corresponding to the
38    /// cuest variant.
39    pub fn error(&self, error: Error) -> CmdError {
40        use DataCmd::*;
41        match self {
42            Blob(c) => c.error(error),
43            Map(c) => c.error(error),
44            Sequence(c) => c.error(error),
45            Account(c) => c.error(error),
46        }
47    }
48    /// Returns the type of authorisation needed for the cuest.
49    pub fn authorisation_kind(&self) -> AuthorisationKind {
50        use DataCmd::*;
51        match self {
52            Blob(c) => c.authorisation_kind(),
53            Map(c) => c.authorisation_kind(),
54            Sequence(c) => c.authorisation_kind(),
55            Account(c) => c.authorisation_kind(),
56        }
57    }
58
59    /// Returns the address of the destination for `cuest`.
60    pub fn dst_address(&self) -> XorName {
61        use DataCmd::*;
62        match self {
63            Blob(c) => c.dst_address(),
64            Map(c) => c.dst_address(),
65            Sequence(c) => c.dst_address(),
66            Account(c) => c.dst_address(),
67        }
68    }
69}
70
71impl fmt::Debug for DataCmd {
72    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
73        use DataCmd::*;
74        match self {
75            Blob(c) => write!(formatter, "{:?}", c),
76            Map(c) => write!(formatter, "{:?}", c),
77            Sequence(c) => write!(formatter, "{:?}", c),
78            Account(c) => write!(formatter, "{:?}", c),
79        }
80    }
81}
82
83/// TODO: docs
84#[allow(clippy::large_enum_variant)]
85#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
86pub enum DataQuery {
87    /// TODO: docs
88    Blob(BlobRead),
89    /// TODO: docs
90    Map(MapRead),
91    /// TODO: docs
92    Sequence(SequenceRead),
93    /// Use this only while we don't
94    /// have Authenticator as its own app.
95    Account(AccountRead), // <- "LoginPacket"
96}
97
98impl DataQuery {
99    /// Creates a Response containing an error, with the Response variant corresponding to the
100    /// Request variant.
101    pub fn error(&self, error: Error) -> QueryResponse {
102        use DataQuery::*;
103        match self {
104            Blob(q) => q.error(error),
105            Map(q) => q.error(error),
106            Sequence(q) => q.error(error),
107            Account(q) => q.error(error),
108        }
109    }
110
111    /// Returns the type of authorisation needed for the request.
112    pub fn authorisation_kind(&self) -> AuthorisationKind {
113        use DataQuery::*;
114        match self {
115            Blob(q) => q.authorisation_kind(),
116            Map(q) => q.authorisation_kind(),
117            Sequence(q) => q.authorisation_kind(),
118            Account(q) => q.authorisation_kind(),
119        }
120    }
121
122    /// Returns the address of the destination for `request`.
123    pub fn dst_address(&self) -> XorName {
124        use DataQuery::*;
125        match self {
126            Blob(q) => q.dst_address(),
127            Map(q) => q.dst_address(),
128            Sequence(q) => q.dst_address(),
129            Account(q) => q.dst_address(),
130        }
131    }
132}
133
134impl fmt::Debug for DataQuery {
135    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
136        use DataQuery::*;
137        match self {
138            Blob(q) => write!(formatter, "{:?}", q),
139            Map(q) => write!(formatter, "{:?}", q),
140            Sequence(q) => write!(formatter, "{:?}", q),
141            Account(q) => write!(formatter, "{:?}", q),
142        }
143    }
144}