safe_nd/messaging/
map.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::{AuthorisationKind, CmdError, DataAuthKind, QueryResponse};
11use crate::{
12    Error, Map, MapAddress as Address, MapEntryActions as Changes,
13    MapPermissionSet as PermissionSet, PublicKey, XorName,
14};
15use serde::{Deserialize, Serialize};
16use std::fmt;
17
18/// TODO: docs
19#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
20pub enum MapRead {
21    /// Get Map.
22    Get(Address),
23    /// Get Map value.
24    GetValue {
25        /// Map address.
26        address: Address,
27        /// Key to get.
28        key: Vec<u8>,
29    },
30    /// Get Map shell.
31    GetShell(Address),
32    /// Get Map version.
33    GetVersion(Address),
34    /// List Map entries.
35    ListEntries(Address),
36    /// List Map keys.
37    ListKeys(Address),
38    /// List Map values.
39    ListValues(Address),
40    /// List Map permissions.
41    ListPermissions(Address),
42    /// Get Map permissions for a user.
43    ListUserPermissions {
44        /// Map address.
45        address: Address,
46        /// User to get permissions for.
47        user: PublicKey,
48    },
49}
50
51/// TODO: docs
52#[allow(clippy::large_enum_variant)]
53#[derive(Hash, Eq, PartialEq, PartialOrd, Clone, Serialize, Deserialize)]
54pub enum MapWrite {
55    /// Create new Map.
56    New(Map),
57    /// Delete instance.
58    Delete(Address),
59    /// Edit entries.
60    Edit {
61        /// Map address.
62        address: Address,
63        /// Changes to apply.
64        changes: Changes,
65    },
66    /// Delete user permissions.
67    DelUserPermissions {
68        /// Map address.
69        address: Address,
70        /// User to delete permissions for.
71        user: PublicKey,
72        /// Version to delete.
73        version: u64,
74    },
75    /// Set user permissions.
76    SetUserPermissions {
77        /// Map address.
78        address: Address,
79        /// User to set permissions for.
80        user: PublicKey,
81        /// New permissions.
82        permissions: PermissionSet,
83        /// Version to set.
84        version: u64,
85    },
86}
87
88impl MapRead {
89    /// Creates a Response containing an error, with the Response variant corresponding to the
90    /// Request variant.
91    pub fn error(&self, error: Error) -> QueryResponse {
92        use MapRead::*;
93        match *self {
94            Get(_) => QueryResponse::GetMap(Err(error)),
95            GetValue { .. } => QueryResponse::GetMapValue(Err(error)),
96            GetShell(_) => QueryResponse::GetMapShell(Err(error)),
97            GetVersion(_) => QueryResponse::GetMapVersion(Err(error)),
98            ListEntries(_) => QueryResponse::ListMapEntries(Err(error)),
99            ListKeys(_) => QueryResponse::ListMapKeys(Err(error)),
100            ListValues(_) => QueryResponse::ListMapValues(Err(error)),
101            ListPermissions(_) => QueryResponse::ListMapPermissions(Err(error)),
102            ListUserPermissions { .. } => QueryResponse::ListMapUserPermissions(Err(error)),
103        }
104    }
105
106    /// Returns the type of authorisation needed for the request.
107    pub fn authorisation_kind(&self) -> AuthorisationKind {
108        use MapRead::*;
109        match *self {
110            Get(_)
111            | GetValue { .. }
112            | GetShell(_)
113            | GetVersion(_)
114            | ListEntries(_)
115            | ListKeys(_)
116            | ListValues(_)
117            | ListPermissions(_)
118            | ListUserPermissions { .. } => AuthorisationKind::Data(DataAuthKind::PrivateRead),
119        }
120    }
121
122    /// Returns the address of the destination for request.
123    pub fn dst_address(&self) -> XorName {
124        use MapRead::*;
125        match self {
126            Get(ref address)
127            | GetValue { ref address, .. }
128            | GetShell(ref address)
129            | GetVersion(ref address)
130            | ListEntries(ref address)
131            | ListKeys(ref address)
132            | ListValues(ref address)
133            | ListPermissions(ref address)
134            | ListUserPermissions { ref address, .. } => *address.name(),
135        }
136    }
137}
138
139impl fmt::Debug for MapRead {
140    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
141        use MapRead::*;
142        write!(
143            formatter,
144            "Request::{}",
145            match *self {
146                Get(_) => "GetMap",
147                GetValue { .. } => "GetMapValue",
148                GetShell(_) => "GetMapShell",
149                GetVersion(_) => "GetMapVersion",
150                ListEntries(_) => "ListMapEntries",
151                ListKeys(_) => "ListMapKeys",
152                ListValues(_) => "ListMapValues",
153                ListPermissions(_) => "ListMapPermissions",
154                ListUserPermissions { .. } => "ListMapUserPermissions",
155            }
156        )
157    }
158}
159
160impl MapWrite {
161    /// Creates a Response containing an error, with the Response variant corresponding to the
162    /// Request variant.
163    pub fn error(&self, error: Error) -> CmdError {
164        CmdError::Data(error)
165    }
166
167    /// Returns the type of authorisation needed for the request.
168    pub fn authorisation_kind(&self) -> AuthorisationKind {
169        AuthorisationKind::Data(DataAuthKind::Write)
170    }
171
172    /// Returns the address of the destination for request.
173    pub fn dst_address(&self) -> XorName {
174        use MapWrite::*;
175        match self {
176            New(ref data) => *data.name(),
177            Delete(ref address)
178            | SetUserPermissions { ref address, .. }
179            | DelUserPermissions { ref address, .. }
180            | Edit { ref address, .. } => *address.name(),
181        }
182    }
183}
184
185impl fmt::Debug for MapWrite {
186    fn fmt(&self, formatter: &mut fmt::Formatter) -> fmt::Result {
187        use MapWrite::*;
188        write!(
189            formatter,
190            "Request::{}",
191            match *self {
192                New(_) => "NewMap",
193                Delete(_) => "DeleteMap",
194                SetUserPermissions { .. } => "SetMapUserPermissions",
195                DelUserPermissions { .. } => "DelMapUserPermissions",
196                Edit { .. } => "EditMap",
197            }
198        )
199    }
200}